Back to Devexpress

How to: Load a Document into SpreadsheetControl

windowsforms-15350-controls-and-libraries-spreadsheet-examples-files-how-to-load-a-document-into-spreadsheet-control.md

latest3.6 KB
Original Source

How to: Load a Document into SpreadsheetControl

  • Apr 22, 2022
  • 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.

EventDescription
SpreadsheetControl.BeforeImportOccurs before a document is loaded.
SpreadsheetControl.DocumentLoadedOccurs when the document is loaded.
SpreadsheetControl.InvalidFormatExceptionOccurs 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.

csharp
// Load a workbook from a file.
workbook.LoadDocument("Documents\\Document.xlsx", DocumentFormat.Xlsx);
vb
' 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.

csharp
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);
}
vb
Imports System.IO
Imports DevExpress.Spreadsheet
' ...

' Load a workbook from a stream.
Using stream As New FileStream("Documents\Document.xlsx", FileMode.Open)
    spreadsheetControl1.LoadDocument(stream, DocumentFormat.Xlsx)
End Using

See Also

Supported Formats

How to: Save a Document to a File

How to: Create a New Workbook