Skip to main content

Use the Excel Export API to Create a New Document

  • 4 minutes to read

To create a new document using the Excel Export API, do the following.

  1. Use the XlExport.CreateExporter method to create an object exposing the IXlExporter interface to perform an export to the specified file format.
  2. Call the IXlExporter.CreateDocument method to create a new document and to write it to the specified file stream.

Note

When you finish working with the IXlDocument object, call the Dispose method to release all the resources used by the object. Otherwise, generated content is not written to the output file. You can also modify the IXlDocument object within the using statement (Using block in Visual Basic).

// Create an exporter instance. 
IXlExporter exporter = XlExport.CreateExporter(XlDocumentFormat.Xlsx);
// Create the FileStream object with the specified file path. 
using (FileStream stream = new FileStream("Document.xlsx", FileMode.Create, FileAccess.ReadWrite))
{

    // Create a new document and write it to the specified stream. 
    using (IXlDocument document = exporter.CreateDocument(stream))
    {
        // your code here
    }
}

After a workbook is generated, you can create other spreadsheet elements, such as worksheets, rows and columns, and cells.

Manage Export Options for a Created Document

Use the IXlDocument.Options property to specify export options. The code sample below specifies culture settings for a created document:

// Create an exporter instance. 
IXlExporter exporter = XlExport.CreateExporter(XlDocumentFormat.Xlsx);
// Create the FileStream object with the specified file path. 
using (FileStream stream = new FileStream("Document.xlsx", FileMode.Create, FileAccess.ReadWrite)) {

    // Create a new document and write it to the specified stream. 
    using (IXlDocument document = exporter.CreateDocument(stream)) {

        // Specify the document culture.
        document.Options.Culture = CultureInfo.CurrentCulture;
    }
}

CSV Export Options

Cast the IXlDocument.Options property to the CsvDataAwareExporterOptions class to specify options to export the document to CSV format.

The code sample below shows how to specify CsvDataAwareExporterOptions:

// Create an exporter instance.
IXlExporter exporter = XlExport.CreateExporter(documentFormat);

// Create a new document.
using(IXlDocument document = exporter.CreateDocument(stream)) {
    document.Options.Culture = CultureInfo.CurrentCulture;

    // Specify CSV export options.
    CsvDataAwareExporterOptions csvOptions = document.Options as CsvDataAwareExporterOptions;
    if(csvOptions != null) {

        // Set the encoding of the text-based file to which the workbook is exported.
        csvOptions.Encoding = Encoding.UTF8;

        // Write a preamble that specifies the used encoding.
        csvOptions.WritePreamble = true;

        // Use the current culture's format string to convert a cell value to a string. 
        csvOptions.UseCellNumberFormat = false;

        // Insert the newline character after the last row of the resulting text.
        csvOptions.NewlineAfterLastRow = true;
    }
}
See Also