Back to Devexpress

How to: Merge Multiple Workbooks Into One Document

officefileapi-120383-spreadsheet-document-api-examples-workbooks-how-to-merge-multiple-workbooks-into-one-document.md

latest3.0 KB
Original Source

How to: Merge Multiple Workbooks Into One Document

  • Sep 19, 2023
  • 2 minutes to read

Important

The Workbook class is defined in the DevExpress.Docs.v25.2.dll assembly. Add this assembly to your project to use the Workbook API. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

Use one of the following methods to combine data from multiple workbooks into a single document.

MethodDescription
WorkbookExtensions.AppendAppends all worksheets from the specified workbooks to the current workbook.
Workbook.MergeCombines specified workbooks into a new document.

Copy Workbooks to Another Document

csharp
// Add a reference to the DevExpress.Docs.dll assembly.
using DevExpress.Spreadsheet;
// ...

// Create the first workbook.
Workbook book1 = new Workbook();
book1.LoadDocument("Document1.xlsx", DocumentFormat.Xlsx);

// Create the second workbook.
Workbook book2 = new Workbook();
book2.LoadDocument("Document2.xlsx", DocumentFormat.Xlsx);

// Copy all worksheets from "Document1" to "Document2".
book2.Append(book1);
vb
' Add a reference to the DevExpress.Docs.dll assembly.
Imports DevExpress.Spreadsheet
' ...

' Create the first workbook.
Dim book1 As New Workbook()
book1.LoadDocument("Document1.xlsx", DocumentFormat.Xlsx)

' Create the second workbook.
Dim book2 As New Workbook()
book2.LoadDocument("Document2.xlsx", DocumentFormat.Xlsx)

' Copy all worksheets from "Document1" to "Document2".
book2.Append(book1)

Merge Workbooks into a New Document

csharp
// Add a reference to the DevExpress.Docs.dll assembly.
using DevExpress.Spreadsheet;
// ...

// Create the first workbook.
Workbook book1 = new Workbook();
book1.LoadDocument("Document1.xlsx", DocumentFormat.Xlsx);

// Create the second workbook.
Workbook book2 = new Workbook();
book2.LoadDocument("Document2.xlsx", DocumentFormat.Xlsx);

// Combine two documents into a new document.
Workbook result = Workbook.Merge(book1, book2);
vb
' Add a reference to the DevExpress.Docs.dll assembly.
Imports DevExpress.Spreadsheet
' ...

' Create the first workbook.
Dim book1 As New Workbook()
book1.LoadDocument("Document1.xlsx", DocumentFormat.Xlsx)

' Create the second workbook.
Dim book2 As New Workbook()
book2.LoadDocument("Document2.xlsx", DocumentFormat.Xlsx)

' Combine two documents into a new document.
Dim result As Workbook = Workbook.Merge(book1, book2)