Excel provides the flexibility to copy and paste Excel sheets as per your requirements. You can copy a sheet to another by cloning the rows from one sheet to another. To do so, you can use the following code. In this example, we load a file named InputFile.xls, copy its content using the Clone method and paste it into another file named OutputFile.xls.
C# |
Copy Code
|
---|---|
c1XLBook1.Load("InputFile.xls"); XLSheet sheet1 = c1XLBook1.Sheets[0]; XLSheet sheet2 = c1XLBook1.Sheets[1]; for (int r = 0; r < sheet1.Rows.Count; r++) { XLRow row = sheet1.Rows[r].Clone(); sheet2.Rows.Insert(r, row); } c1XLBook1.Save("OutputFile.xls", FileFormat.OpenXml); Process.Start("OutputFile.xls"); |
Additionally, you can also copy the print area defined within one sheet to another by using NamedRanges property of the C1XLBook class. To do so, use the following code. Here, we first create a named range, Print_Area, in the InputFile.xls and paste the created named range in another file named OutputFile.xls.
C# |
Copy Code
|
---|---|
c1XLBook1.Load("InputFile.xls"); XLSheet sheet1 = c1XLBook1.Sheets[0]; XLSheet sheet2 = c1XLBook1.Sheets[1]; XLSheet sheet = c1XLBook1.Sheets[0]; XLCellRange range = new XLCellRange(sheet, 7, 9, 0, 2); c1XLBook1.NamedRanges.Add("Print_Area", range); XLNamedRange nrPA = c1XLBook1.NamedRanges[0]; foreach (XLNamedRange nr in c1XLBook1.NamedRanges) { if (nr.Name.StartsWith("Print_Area")) { nrPA = nr; } } c1XLBook1.NamedRanges.Add("Print_Area", new XLCellRange(sheet2, nrPA.CellRange.RowFrom, nrPA.CellRange.RowTo, nrPA.CellRange.ColumnFrom, nrPA.CellRange.ColumnTo)); c1XLBook1.Save("OutputFile.xls", FileFormat.OpenXml); |