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

How to: Save a Document to a File

  • 2 minutes to read

To save a spreadsheet document (workbook) loaded into SpreadsheetControl, use the SpreadsheetControl.SaveDocument method of the control, or the ISpreadsheetComponent.SaveDocument method of the IWorkbook object. These methods enable you to save a document to a file or stream.

The SpreadsheetControl.BeforeExport event is raised before the document is saved. The SpreadsheetControl.DocumentSaved event is raised after the document is saved.

  • Save to File

Call the SpreadsheetControl.SaveDocument with the file path as a parameter to save a workbook to the file. Specify the file format as the second parameter of the method using the DocumentFormat enumerator.

Note

Certain methods can throw unhandled exceptions if a problem occurs. Consider the situation when a document is being saved to a locked or read-only file. To prevent application failure, subscribe to the SpreadsheetControl.UnhandledException event and set the SpreadsheetUnhandledExceptionEventArgs.Handled property to true.

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

Save to a Stream

Create a FileStream object with the specified file path to save a workbook, and call the ISpreadsheetComponent.SaveDocument method of the IWorkbook object with this stream passed as a parameter. Specify the file format as the second parameter of the method using the DocumentFormat enumerator.

using System.IO;
using DevExpress.Spreadsheet;
// ...

IWorkbook workbook = spreadsheetControl1.Document;

// ...

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