Back to Devexpress

Import and Export in Spreadsheet for WinForms

windowsforms-405529-controls-and-libraries-spreadsheet-import-and-export.md

latest14.8 KB
Original Source

Import and Export in Spreadsheet for WinForms

  • Jul 09, 2025
  • 5 minutes to read

This help topic describes how to load and save documents from UI and code, and includes links to code samples.

Built-in UI

Users can click the Open button on the File ribbon tab (or press Ctrl+O) to invoke the Open dialog. Then, they can select the file and click Open.

On the File ribbon tab, users can click the Save button (or press Ctrl+S) to save the changes made to the current document. They can click Save As (or press F12) to invoke the Save As dialog used to save a document copy as a new file, and specify the document’s location, name, and format.

Refer to the following topic for more information on how to add a ribbon UI to the Spreadsheet Control: Get Started with the WinForms Spreadsheet Control.

Tip

You can prevent users from using the Create, Load or Save commands. To disable or hide these commands from the Ribbon UI and pop-up menu, set the SpreadsheetBehaviorOptions.CreateNew, SpreadsheetBehaviorOptions.Open and SpreadsheetBehaviorOptions.Save/SpreadsheetBehaviorOptions.SaveAs properties to DocumentCapability.Disabled.

Load and Save Operations in Code

The SpreadsheetControl includes the following methods to load or save the document and specify its options.

MemberDescription
SpreadsheetControl.LoadDocumentLoads a document from a file or stream. Optionally, you can specify the document format with the DocumentFormat enum.
ISpreadsheetControl.LoadDocumentLoads a document from a file or stream. Optionally, you can specify the document format with the DocumentFormat enum.
SpreadsheetControl.CreateNewDocumentCreates and loads a new empty workbook.
ISpreadsheetComponent.CreateNewDocumentCreates and loads a new empty workbook.
SpreadsheetControl.SaveDocumentSaves the control’s document to a file or stream and specifies the document’s format and encryption settings.
ISpreadsheetComponent.SaveDocumentSaves the control’s document to a file or stream and specifies the document’s format and encryption settings.
WorkbookSaveOptionsProvides access to options which define the file name and file format that are used when saving and loading the workbook.

The following code snippet uses the WorkbookSaveOptions.CurrentFileName property to specify the saved document file name (including the path and extension):

csharp
public partial class Form1 : Form {
    // ...

    public Form1() {
        InitializeComponent();
        spreadsheetControl1.DocumentLoaded += spreadsheetControl1_DocumentLoaded;
        spreadsheetControl1.EmptyDocumentCreated += spreadsheetControl1_EmptyDocumentCreated;
        //...
    }
    private void spreadsheetControl1_DocumentLoaded(object sender, EventArgs e) {
        IWorkbook workbook = (sender as SpreadsheetControl).Document;
        workbook.Options.Save.CurrentFileName = 
            Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "result.xlsx");
    }
    private void spreadsheetControl1_EmptyDocumentCreated(object sender, EventArgs e) {
        IWorkbook workbook = (sender as SpreadsheetControl).Document;
        workbook.Options.Save.CurrentFileName = "newSpreadsheet.xlsx";
    }
}
vb
Public Partial Class Form1
    Inherits Form
    Private workbook As IWorkbook
    Public Sub New()
        InitializeComponent()
        AddHandler spreadsheetControl1.DocumentLoaded, AddressOf spreadsheetControl1_DocumentLoaded
        AddHandler spreadsheetControl1.EmptyDocumentCreated, AddressOf spreadsheetControl1_EmptyDocumentCreated
        ' ...
    End Sub
    Private Sub spreadsheetControl1_DocumentLoaded(sender As Object, e As EventArgs)
        Dim workbook As IWorkbook = CType(sender, SpreadsheetControl).Document
        workbook.Options.Save.CurrentFileName = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "result.xlsx")
    End Sub
    Private Sub spreadsheetControl1_EmptyDocumentCreated(sender As Object, e As EventArgs)
        Dim workbook As IWorkbook = CType(sender, SpreadsheetControl).Document
        workbook.Options.Save.CurrentFileName = "newSpreadsheet.xlsx"
    End Sub
End Class

How to: Load and Save a Document

The following code snippet loads the document from a file stream when the form is opened and saves the result to the file when the form is closed:

csharp
private void Form1_Load(object sender, EventArgs e) {
using (Stream stream = new FileStream("FirstLook.xlsx", FileMode.Open)) {
    spreadsheetControl1.LoadDocument(stream);
  }
}
//...
private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
   spreadsheetControl1.SaveDocument("Result.xlsx", DocumentFormat.Xlsx);
}
vb
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Using stream As Stream = New FileStream("FirstLook.xlsx", FileMode.Open)
            spreadsheetControl1.LoadDocument(stream)
        End Using
    End Sub
'...
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
    spreadsheetControl1.SaveDocument("Result.xlsx", DocumentFormat.Xlsx)
End Sub

Note

We do not recommend that you use the DocumentFormat.Undefined field as the SaveDocument method parameter. Otherwise, the document is saved with an invalid format.

Refer to the following examples to get code samples for file operations:

Basic Format-Specific API

The table below lists document formats the SpreadsheetControl supports, and the API used to set format-specific import and export options. You can specify these options in the SpreadsheetControl.BeforeImport or SpreadsheetControl.BeforeExport event handlers.

FormatAccessed ByImport OptionsExport Options
Plain TextDocumentFormat.TextTxtDocumentImporterOptionsTxtDocumentExporterOptions
CSVDocumentFormat.CsvCsvDocumentImporterOptionsCsvDocumentExporterOptions
OpenXMLDocumentFormat.OpenXmlOpenXmlDocumentImporterOptionsOpenXmlDocumentExporterOptions
XlsDocumentFormat.XlsXlsDocumentImporterOptionsXlsDocumentExporterOptions
XlsbDocumentFormat.XlsbXlsbDocumentImporterOptionsXlsbDocumentExporterOptions
XlsmDocumentFormat.XlsmXlsmDocumentImporterOptionsXlsmDocumentExporterOptions
XltDocumentFormat.XltXltDocumentImporterOptionsXltDocumentExporterOptions
XltxDocumentFormat.XltxXltxDocumentImporterOptionsXltxDocumentExporterOptions
XmlSpreadsheet2003DocumentFormat.XmlSpreadsheet2003XmlSpreadsheet2003DocumentImporterOptionsXmlSpreadsheet2003DocumentExporterOptions

Perform Actions Before Import

The following code snippet handles the SpreadsheetControl.BeforeImport event for different document formats:

csharp
private void SpreadsheetControl_BeforeImport(object sender, BeforeImportEventArgs e) {
    if (e.DocumentFormat == DocumentFormat.Text) {
        //Detects plain text encoding automatically
        ((TxtDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
    }

    if (e.DocumentFormat == DocumentFormat.Csv) {
        //Disables removing all leading and trailing whitespace characters
        ((CsvDocumentImporterOptions)e.Options).TrimBlanks = false;
    }

    if (e.DocumentFormat == DocumentFormat.OpenXml) {
        //Sets manual formula calculation mode
        ((OpenXmlDocumentImporterOptions)e.Options).OverrideCalculationMode = DevExpress.XtraSpreadsheet.Import.CalculationModeOverride.Manual;
    }
}
vb
Private Sub SpreadsheetControl_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
    If e.DocumentFormat = DocumentFormat.PlainText Then
        'Detects plain text encoding automatically
        (CType(e.Options, TxtDocumentImporterOptions)).AutoDetectEncoding = True
    End If 

    If e.DocumentFormat = DocumentFormat.Doc Then
        'Disables removing all leading and trailing whitespace characters
        (CType(e.Options, CsvDocumentImporterOptions)).TrimBlanks = False
    End If

    If e.DocumentFormat = DocumentFormat.Html Then
        'Sets manual formula calculation mode
        (CType(e.Options, OpenXmlDocumentImporterOptions)).OverrideCalculationMode = DevExpress.XtraSpreadsheet.Import.CalculationModeOverride.Manual
    End If
End Sub

Perform Actions Before Export

The following code snippet specifies export options for different formats in the SpreadsheetControl.BeforeExport event handler:

csharp
private void SpreadsheetControl_BeforeExport(object sender, BeforeExportEventArgs e) {
    if (e.DocumentFormat == DocumentFormat.Text) {
        //Specifies the formula export mode
        TxtDocumentExporterOptions plainTextOptions = e.Options as TxtDocumentExporterOptions;
        plainTextOptions.FormulaExportMode = DevExpress.XtraSpreadsheet.Export.FormulaExportMode.CalculatedValue;
    }

    if (e.DocumentFormat == DocumentFormat.Csv) {
        //Specifies CSV document hidden column behavior and the worksheet name 
        CsvDocumentExporterOptions csvOptions = e.Options as CsvDocumentExporterOptions;
        csvOptions.SkipHiddenColumns = True;
        csvOptions.WorksheetName = "Results";
    }
}
vb
Private Sub SpreadsheetControl_BeforeExport(ByVal sender As Object, ByVal e As BeforeExportEventArgs)
    If e.DocumentFormat = DocumentFormat.Text Then
    'Specifies the formula export mode
    Dim plainTextOptions As TxtDocumentExporterOptions = TryCast(e.Options, TxtDocumentExporterOptions)
    plainTextOptions.FormulaExportMode = DevExpress.XtraSpreadsheet.Export.FormulaExportMode.CalculatedValue
    End If

    If e.DocumentFormat = DocumentFormat.Csv Then
    'Specifies CSV document hidden column behavior and the worksheet name
    Dim csvOptions As CsvDocumentExporterOptions = TryCast(e.Options, CsvDocumentExporterOptions)
    csvOptions.SkipHiddenColumns = True 
    csvOptions.WorksheetName = "Results"
    End If
End Sub