officefileapi-114072-excel-export-library-workbooks-how-to-create-a-new-document.md
To create a new document using the Excel Export API, do the following.
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
}
}
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(XlDocumentFormat.Xlsx)
' Create the FileStream object with the specified file path.
Using stream As New FileStream("Document.xlsx", FileMode.Create, FileAccess.ReadWrite)
' Create a new document and write it to the specified stream.
Using document As IXlDocument = exporter.CreateDocument(stream)
' your code here
End Using
End Using
After a workbook is generated, you can create other spreadsheet elements, such as worksheets, rows and columns, and cells.
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;
}
}
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(XlDocumentFormat.Xlsx)
' Create the FileStream object with the specified file path.
Using stream As New FileStream("Document.xlsx", FileMode.Create, FileAccess.ReadWrite)
' Create a new document and write it to the specified stream.
Using document As IXlDocument = exporter.CreateDocument(stream)
' Specify the document culture.
document.Options.Culture = CultureInfo.CurrentCulture
End Using
End Using
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;
}
}
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Specify CSV export options.
Dim csvOptions As CsvDataAwareExporterOptions = TryCast(document.Options, CsvDataAwareExporterOptions)
If csvOptions IsNot Nothing Then
' 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
End If
End Using
See Also