Skip to main content
A newer version of this page is available. .

How to: Load a Document to a Workbook

  • 3 minutes to read

Important

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

To load an existing spreadsheet document into the Workbook object, use the Workbook.LoadDocument method.

Load from File

Create a new Workbook object and call the Workbook.LoadDocument method with the passed file path to load a workbook from the existing file. Specify the file format as the second parameter of the method using the DocumentFormat enumerator.

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

Workbook workbook = new Workbook();

// Load a workbook from the file.
workbook.LoadDocument("Documents\\Document.xlsx", DocumentFormat.Xlsx);

Load from Stream

Create the FileStream object with the specified file path to open the existing file, and call the Workbook.LoadDocument method with this stream object passed as a parameter. Specify the file format as the second parameter of the method using the DocumentFormat enumerator.

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

Workbook workbook = new Workbook();

// Load a workbook from the stream.
using (FileStream stream = new FileStream("Documents\\Document.xlsx", FileMode.Open))
{
    workbook.LoadDocument(stream, DocumentFormat.Xlsx);
}

Asynchronous Load

Use the Workbook.LoadDocumentAsync method to asynchronously load a workbook from file, stream or byte array.

Important

Take into account the following when you call this method:

  • The events fired by this method’s call may occur in a different thread than the target operation.

  • The operation is not thread safe (documents should not be accessed simultaneously by different threads). Wait until the operation is completed before working with the document, i.e., use the await operator.

The code sample below shows how to merge two asynchronously loaded workbooks and save the result asynchronously.

private async void MergeWorkbooks()
{
  using (Workbook workbook1 = new Workbook())
  using (Workbook workbook2 = new Workbook())
  {
      await Task.WhenAll(new Task[]
      {
          workbook1.LoadDocumentAsync("book1.xlsx"),
          workbook2.LoadDocumentAsync("book2.xlsx")
      });
      workbook1.Append(workbook2);
      await workbook1.SaveDocumentAsync("merged.xlsx");
  }
}
See Also