The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Save a Document to a File

  • 3 min 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 require 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 save a spreadsheet document loaded to the Workbook object, use the Workbook.SaveDocument method.

Save to File

Call the Workbook.SaveDocument method with the specified file path to save a workbook to the 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();
// ...

// Save the modified document to the file.
workbook.SaveDocument("Documents\\SavedDocument.xlsx", DocumentFormat.Xlsx);

Save to Stream

Create the FileStream object with the specified file path to save a workbook, and call the Workbook.SaveDocument method with this stream 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();
// ...

// Save the modified document to the stream.
using (FileStream stream = new FileStream("Documents\\SavedDocument.xlsx", 
    FileMode.Create, FileAccess.ReadWrite)) {
    workbook.SaveDocument(stream, DocumentFormat.Xlsx);
}

Asynchronous Load

Use the Workbook.SaveDocumentAsync method to asynchronously save a workbook to the file or stream.

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