ComponentOne Excel for .NET
In This Topic
    Copying a Sheet
    In This Topic

    To copy a sheet (including its print area) to another Excel sheet, use the following code:

    C1XLBook1.Load("..\..\InputFile.xlsx")
        Dim sheet1 As XLSheet = C1XLBook1.Sheets(0)
        Dim sheet2 As XLSheet = C1XLBook1.Sheets(1)
    
        For r As Integer = 0 To sheet1.Rows.Count - 1
            Dim row As XLRow = sheet1.Rows(r).Clone()
            sheet2.Rows.Insert(r, row)
        Next
    
        Dim nrPA As XLNamedRange = C1XLBook1.NamedRanges(0)
    
        For Each nr As XLNamedRange In C1XLBook1.NamedRanges
    
            If nr.Name.StartsWith("Print_Area") Then
                nrPA = nr
            End If
        Next
        C1XLBook1.NamedRanges.Add("Print_Area", New XLCellRange(sheet2, nrPA.CellRange.RowFrom,
            nrPA.CellRange.RowTo, nrPA.CellRange.ColumnFrom, nrPA.CellRange.ColumnTo))
        C1XLBook1.Save("..\..\OutputFile.xlsx", FileFormat.OpenXml)
    
    C1XLBook1.Load("..\\..\\InputFile.xlsx");
    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);
    }
    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.xlsx", FileFormat.OpenXml);