ComponentOne Excel for .NET
In This Topic
    Copying Rows from One Book to Another
    In This Topic

    To copy the rows of a sheet from one book to a second book, complete the following steps:

    1. Load an existing book:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim wb As New C1XLBook()
      wb.Load("C:\test.xls")
      

      To write code in C#

      C#
      Copy Code
      C1XLBook wb = new C1XLBook();
      wb.Load(@"C:\test.xls");
      
    2. Create a new XLSheet:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim xb As New C1XLBook()
      xb.Sheets.Add("Test")
      

      To write code in C#

      C#
      Copy Code
      C1XLBook xb = new C1XLBook();
      xb.Sheets.Add("Test");
      
    3. Copy each row from the sheet of the existing book to new XLSheet:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim source As XLSheet = wb.Sheets(0)
      Dim dest As XLSheet = xb.Sheets("Test")
      Dim row As Integer, col As Integer
          For row = 0 To source.Rows.Count - 1
            For col = 0 To source.Columns.Count - 1
                dest(row, col).Value = source(row, col).Value
            Next col
          Next row
      

      To write code in C#

      C#
      Copy Code
      XLSheet source = wb.Sheets[0];
      XLSheet dest = xb.Sheets("Test");
      for (int row = 0; row <= source.Rows.Count - 1; row++)
      {
         for (int col = 0; col <= source.Columns.Count - 1; col++)
         {
             dest[row, col].Value = source[row, col].Value;
         }
      }
      
    4. Save and open the new book:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      xb.Save("C:\test2.xls")
      System.Diagnostics.Process.Start("C:\test2.xls")
      

      To write code in C#

      C#
      Copy Code
      xb.Save(@"c:\test2.xls");
      System.Diagnostics.Process.Start(@"C:\test2.xls");