How to: Save a Spreadsheet Document
- 2 minutes to read
To save a spreadsheet document (workbook) loaded into SpreadsheetControl, use the control’s SpreadsheetControl.SaveDocument method, or the IWorkbook object’s ISpreadsheetComponent.SaveDocument method. 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 IWorkbook object’s ISpreadsheetComponent.SaveDocument method with the file path passed as a parameter to save a workbook to the file. Use the DocumentFormat enumerator to specify the file format as the method’s second parameter.
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 Stream
Create a FileStream object with the specified file path to save a workbook, and call the IWorkbook object’s ISpreadsheetComponent.SaveDocument method with this stream passed as a parameter. Use the DocumentFormat enumerator to specify the file format as the method’s second parameter.
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);
}