How to: Copy Worksheets between Workbooks
- 2 min to read
You require a license to the DevExpress Office File API or DevExpress Universal Subscription to use these examples in production code. Refer to the DevExpress Subscription page for pricing information.
This example demonstrates how to add an existing worksheet to a new workbook. To do this, access a Worksheet object that specifies a worksheet into which you want to copy another worksheet and call its Worksheet.CopyFrom method with the source worksheet object passed as a parameter.
A complete sample project is available at https://github.com/DevExpress-Examples/spreadsheet-document-server-api-e4339
// Create a source workbook. Workbook sourceWorkbook = new Workbook(); // Add a new worksheet. sourceWorkbook.Worksheets.Add(); // Modify the second worksheet of the source workbook to be copied. sourceWorkbook.Worksheets.Cells["A1"].Value = "A worksheet to be copied"; sourceWorkbook.Worksheets.Cells["A1"].Font.Color = Color.ForestGreen; // Copy the second worksheet of the source workbook into the first worksheet of another workbook. workbook.Worksheets.CopyFrom(sourceWorkbook.Worksheets);