Excel for WPF | ComponentOne
C1Excel Task-Based Help / Copying Rows from One Book to Another (WPF)
In This Topic
    Copying Rows from One Book to Another (WPF)
    In This Topic

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

    1. Add a reference to C1.WPF.Excel.dll.
    2. Load an existing book.
      C#
      Copy Code
      C1XLBook wb = new C1XLBook();
      wb.Load(@"C:\test.xlsx");
      
    3. Create a new workbook with an XLSheet named Test.
      C#
      Copy Code
      C1XLBook xb = new C1XLBook();
      xb.Sheets.Add("Test");
      
    4. Copy each row from the sheet of the existing book to the new workbook Test sheet.
      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;
                      }
                  }
      
    5. Save and open the new workbook.
      C#
      Copy Code
      // Save and open the file
      xb.Save(@"C:\test2.xlsx");
      System.Diagnostics.Process.Start(@"C:\test2.xlsx");
      
    6. Open the new book. The rows from the first book have been added to the new book.