Skip to main content
All docs
V26.1
  • Import and Export Spreadsheet Documents

    • 8 minutes to read

    Run Demo: Convert Spreadsheet to HTML

    This document describes how to load and save spreadsheet documents in different formats.

    Supported Formats

    The Spreadsheet Document API supports the following file formats for importing and exporting data:

    XLSX
    Microsoft Office Open XML format - the default file format starting with Microsoft Excel 2007.
    XLSM
    Microsoft Office Open XML macro-enabled format (limited support - macros cannot be executed or modified).
    XLSB
    Microsoft Excel 2007-2010 binary file format (BIFF12).
    XLS
    • Microsoft Excel 97-2003 binary file format (import/export).
    • Microsoft Excel 5.0/95 binary file format (import only).
    • Microsoft Excel 2 Worksheets / BIFF2 binary file format (import only).
    XLTX
    Microsoft Office Open XML template file.
    XLTM
    Microsoft Office Open XML macro-enabled template file (limited support - macros cannot be executed or modified).
    XLT
    Microsoft Excel 97-2003 template file.
    XML
    XML Spreadsheet 2003 file format (limited support - pivot tables cannot be loaded or saved).
    CSV
    Comma Separated Values - the plain text format that uses commas as separators between cell values.
    TXT
    Tab Delimited Text - the plain text format that uses tab characters as separators between cell values.
    PDF (export only)
    Portable Document Format.
    HTML (export only)
    Web Page.

    Initiate Load and Save Operations

    Use the following Workbook members to load and save documents and specify load and save options:

    Member Description
    Workbook.LoadDocument(stream, format) Loads a document from a file or stream. A DocumentFormat value specifies the document format.
    Workbook.LoadDocumentAsync(stream, format) Asynchronously loads a document from a file or stream. A DocumentFormat value specifies the document format.
    Workbook.CreateNewDocument Creates a new empty workbook.
    Workbook.SaveDocument(stream, format) Saves the workbook to a file or stream. Overloads allow you to specify the document format and encryption settings.
    Workbook.SaveDocumentAsync(stream, format) Asynchronously saves the workbook to a file or stream. Overloads allow you to specify the document format and encryption settings.
    DocumentOptions.Import Provides access to options used to import workbooks in different formats.
    DocumentOptions.Save Provides access to the workbook save options.

    Load and Save a Document

    The following code snippet loads a workbook from a stream and saves the workbook to a file:

    using System.IO;
    using DevExpress.Spreadsheet;
    
    using var workbook = new Workbook();
    
    // Load a workbook from a stream.
    using var stream = File.OpenRead("Document.xlsx");
    workbook.LoadDocument(stream, DocumentFormat.Xlsx);
    
    // Save the workbook to a file.
    Directory.CreateDirectory("output");
    workbook.SaveDocument(Path.Combine("output", "SavedDocument.xlsx"), DocumentFormat.Xlsx);
    

    Refer to the following help topics for additional information:

    Note

    Do not pass the DocumentFormat.Undefined value to the SaveDocument method. Otherwise, the SaveDocument method saves the document in an invalid format.

    Import Data from CSV and TXT Files

    To import data from a CSV or TXT file, pass the DocumentFormat.Csv or DocumentFormat.Text value to the Workbook.LoadDocument(fileName, format) method. The workbook places imported data on the first worksheet.

    Before you load the file, use the WorkbookImportOptions.Csv or WorkbookImportOptions.Txt property (Workbook.Options.Import.Csv or Workbook.Options.Import.Txt) to access import options. Specify the following options to control how the workbook parses text:

    Property Description
    CsvDocumentImporterOptions.Delimiter Gets or sets the character that separates values in the text.
    CsvDocumentImporterOptions.AutoDetectDelimiter Gets or sets a value indicating whether to automatically detect a character separating values in the CSV file.
    CsvDocumentImporterOptions.Encoding Gets or sets the encoding used to interpret characters in the loaded CSV document.
    CsvDocumentImporterOptions.Culture Gets or sets culture information used to parse imported data.
    CsvDocumentImporterOptions.ColumnTypes Provides access to the object which enables you to specify the data types for columns in an imported document.
    CsvDocumentImporterOptions.ValueTypeDetectMode Gets or sets the level of complexity to which the type of imported value is recognized.
    CsvDocumentImporterOptions.TrimBlanks Gets or sets whether to remove all leading and trailing white-space characters from each imported string value.

    The TxtDocumentImporterOptions class contains the same options for TXT files.

    If you set the Delimiter property, the AutoDetectDelimiter property gets false.

    The following code snippet loads a semicolon-separated CSV file with German number and date formats, imports first column values as text, and saves the data to an XLSX file:

    using System.Globalization;
    using System.IO;
    using System.Text;
    using DevExpress.Spreadsheet;
    using DevExpress.XtraSpreadsheet.Import;
    
    using var workbook = new Workbook();
    
    // Specify CSV import options.
    var csvOptions = workbook.Options.Import.Csv;
    csvOptions.Delimiter = ';';
    csvOptions.Encoding = Encoding.UTF8;
    csvOptions.Culture = new CultureInfo("de-DE");
    // Import values in the first column as text to maintain leading zeros.
    csvOptions.ColumnTypes[0] = ColumnDataType.Text;
    
    // Load data from a CSV file.
    workbook.LoadDocument("Document.csv", DocumentFormat.Csv);
    
    Directory.CreateDirectory("output");
    workbook.SaveDocument(Path.Combine("output", "Document.xlsx"), DocumentFormat.Xlsx);
    

    The following code snippet loads a tab-delimited TXT file and saves the data to an XLSX file:

    using System.IO;
    using DevExpress.Spreadsheet;
    
    using var workbook = new Workbook();
    
    // Load data from a TXT file.
    workbook.LoadDocument("Document.txt", DocumentFormat.Text);
    
    Directory.CreateDirectory("output");
    workbook.SaveDocument(Path.Combine("output", "Document.xlsx"), DocumentFormat.Xlsx);
    

    To import data from an array, a list, or a data table into an existing worksheet, call the Import extension method for the worksheet.

    Refer to the following help topic for additional information: How to: Import Data to a Worksheet in Spreadsheet Document API.

    Basic Format-Specific API

    Handle the Workbook.BeforeImport or Workbook.BeforeExport event to specify format-specific import and export options. Cast the e.Options value to the options class that matches the document format:

    Format DocumentFormat Field Import Options Export Options
    CSV DocumentFormat.Csv CsvDocumentImporterOptions CsvDocumentExporterOptions
    Text DocumentFormat.Text TxtDocumentImporterOptions TxtDocumentExporterOptions
    XLSX DocumentFormat.Xlsx OpenXmlDocumentImporterOptions OpenXmlDocumentExporterOptions
    XLS DocumentFormat.Xls XlsDocumentImporterOptions XlsDocumentExporterOptions
    XLSB DocumentFormat.Xlsb XlsbDocumentImporterOptions XlsbDocumentExporterOptions
    XLSM DocumentFormat.Xlsm XlsmDocumentImporterOptions XlsmDocumentExporterOptions
    XLT DocumentFormat.Xlt XltDocumentImporterOptions XltDocumentExporterOptions
    XLTM DocumentFormat.Xltm XltmDocumentImporterOptions XltmDocumentExporterOptions
    XLTX DocumentFormat.Xltx XltxDocumentImporterOptions XltxDocumentExporterOptions
    XmlSpreadsheet2003 DocumentFormat.XmlSpreadsheet2003 XmlSpreadsheet2003DocumentImporterOptions XmlSpreadsheet2003DocumentExporterOptions
    HTML ❌ ❌ HtmlDocumentExporterOptions
    PDF ❌ ❌ PdfDocumentExporterOptions
    RTF ❌ ❌ RtfCellContentExportOptions

    Perform Actions Before Import

    The following code snippet handles the Workbook.BeforeImport event to specify import options for different document formats and loads an XLSX document:

    using DevExpress.Spreadsheet;
    using DevExpress.XtraSpreadsheet.Import;
    
    using var workbook = new Workbook();
    workbook.BeforeImport += Workbook_BeforeImport;
    
    workbook.LoadDocument("Document.xlsx", DocumentFormat.Xlsx);
    
    void Workbook_BeforeImport(object sender, SpreadsheetBeforeImportEventArgs e)
    {
        if (e.DocumentFormat == DocumentFormat.Text)
        {
            // Detect plain text encoding automatically.
            ((TxtDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
        }
    
        if (e.DocumentFormat == DocumentFormat.Csv)
        {
            // Maintain leading and trailing white-space characters.
            ((CsvDocumentImporterOptions)e.Options).TrimBlanks = false;
        }
    
        if (e.DocumentFormat == DocumentFormat.Xlsx)
        {
            // Load the document in manual calculation mode.
            ((OpenXmlDocumentImporterOptions)e.Options).OverrideCalculationMode = CalculationModeOverride.Manual;
        }
    }
    

    Perform Actions Before Export

    The following code snippet handles the Workbook.BeforeExport event to specify export options for different formats and saves the Results worksheet to CSV and TXT files. The BeforeExport event reports the DocumentFormat.Csv value for both CSV and TXT export, so the handler checks the e.Options type to identify the format:

    using System.IO;
    using DevExpress.Spreadsheet;
    using DevExpress.XtraSpreadsheet.Export;
    
    using var workbook = new Workbook();
    
    // Prepare the "Results" worksheet.
    var worksheet = workbook.Worksheets[0];
    worksheet.Name = "Results";
    worksheet["A1"].Value = "Product";
    worksheet["B1"].Value = "Quantity";
    worksheet["C1"].Value = "Price";
    worksheet["D1"].Value = "Amount";
    worksheet["A2"].Value = "Chai";
    worksheet["B2"].Value = 40;
    worksheet["C2"].Value = 18;
    worksheet["D2"].Formula = "=B2*C2";
    worksheet.Columns["C"].Visible = false;
    
    // Calculate formulas before export.
    workbook.Calculate();
    
    workbook.BeforeExport += Workbook_BeforeExport;
    
    Directory.CreateDirectory("output");
    workbook.SaveDocument(Path.Combine("output", "Results.csv"), DocumentFormat.Csv);
    workbook.SaveDocument(Path.Combine("output", "Results.txt"), DocumentFormat.Text);
    
    void Workbook_BeforeExport(object sender, SpreadsheetBeforeExportEventArgs e)
    {
        if (e.Options is TxtDocumentExporterOptions textOptions)
        {
            // Export formula results instead of formulas.
            textOptions.FormulaExportMode = FormulaExportMode.CalculatedValue;
        }
    
        if (e.Options is CsvDocumentExporterOptions csvOptions)
        {
            // Skip hidden columns and specify the worksheet to export.
            csvOptions.SkipHiddenColumns = true;
            csvOptions.Worksheet = "Results";
        }
    }
    

    Note

    The CsvDocumentExporterOptions.Worksheet property must specify the name of an existing worksheet. Otherwise, the SaveDocument method throws an ArgumentException.