Skip to main content

How to: Load a Document into SpreadsheetControl

  • 2 minutes to read

To load an existing spreadsheet document (workbook) into SpreadsheetControl, use the SpreadsheetControl.LoadDocument method. Method overrides enable you to load a document from a file, a stream or a byte array.

Events related to document loading are listed in the table below.

Event Description
SpreadsheetControl.BeforeImport Occurs before a document is loaded.
SpreadsheetControl.DocumentLoaded Occurs when the document is loaded.
SpreadsheetControl.InvalidFormatException Occurs if the data you are trying to load has an incorrect format. You can subscribe to this event and perform actions required to resolve this situation (for example, inform an end-user about the incorrect file). This event fires if the WorkbookImportOptions.ThrowExceptionOnInvalidDocument property is true (use SpreadsheetControl.Options.Import.ThrowExceptionOnInvalidDocument notation).

Load from File

Call the SpreadsheetControl.LoadDocument method with the 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.

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

Load from Stream

Create a FileStream object with the specified file path to open the existing file, and call the SpreadsheetControl.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.

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

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