officefileapi-devexpress-dot-spreadsheet-dot-export.md
Performs export of the worksheet range to the data table.
Namespace : DevExpress.Spreadsheet.Export
Assembly : DevExpress.Spreadsheet.v25.2.Core.dll
NuGet Package : DevExpress.Spreadsheet.Core
public class DataTableExporter
Public Class DataTableExporter
The following members return DataTableExporter objects:
To export a worksheet range to a DataTable, perform the following steps.
Create a new DataTable instance. You can also use the WorksheetExtensions.CreateDataTable method to create a DataTable based on the first row of the range containing data for export.
Instantiate a DataTableExporter object.
Specify general export options using the DataTableExporter.Options property.
Change default converter settings (if required) using the DataTableExportOptions.DefaultCellValueToColumnTypeConverter and the DataTableExportOptions.DefaultCellValueToStringConverter properties.
Add custom converters, if required, using the DataTableExportOptions.CustomConverters property.
Call the DataTableExporterExtensions.Export method.
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Export;
Worksheet worksheet = spreadsheetControl1.Document.Worksheets[0];
CellRange range = worksheet.Tables[0].Range;
// Create a data table with column names obtained from the first row in a range.
// Column data types are obtained from cell value types of cells in the first data row of the worksheet range.
DataTable dataTable = worksheet.CreateDataTable(range, true);
// Create the exporter that obtains data from the specified range which has a header row and populates the previously created data table.
DataTableExporter exporter = worksheet.CreateDataTableExporter(range, dataTable, true);
// Handle value conversion errors.
exporter.CellValueConversionError += exporter_CellValueConversionError;
// Specify exporter options.
exporter.Options.ConvertEmptyCells = true;
exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0;
exporter.Options.DefaultCellValueToColumnTypeConverter.SkipErrorValues = barCheckItemSkipErrors.Checked;
// Perform the export.
exporter.Export();
void exporter_CellValueConversionError(object sender, CellValueConversionErrorEventArgs e)
{
MessageBox.Show("Error in cell " + e.Cell.GetReferenceA1());
e.DataTableValue = null;
e.Action = DataTableExporterAction.Continue;
}
Imports DevExpress.Spreadsheet
Imports DevExpress.Spreadsheet.Export
Dim worksheet As Worksheet = spreadsheetControl1.Document.Worksheets(0)
Dim range As CellRange = worksheet.Tables(0).Range
' Create a data table with column names obtained from the first row in a range.
' Column data types are obtained from cell value types of cells in the first data row of the worksheet range.
Dim dataTable As DataTable = worksheet.CreateDataTable(range, True)
' Create the exporter that obtains data from the specified range which has a header row and populates the previously created data table.
Dim exporter As DataTableExporter = worksheet.CreateDataTableExporter(range, dataTable, True)
' Handle value conversion errors.
AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
' Specify exporter options.
exporter.Options.ConvertEmptyCells = True
exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0
exporter.Options.DefaultCellValueToColumnTypeConverter.SkipErrorValues = barCheckItemSkipErrors.Checked
' Perform the export.
exporter.Export()
Private Sub exporter_CellValueConversionError(ByVal sender As Object, ByVal e As CellValueConversionErrorEventArgs)
MessageBox.Show("Error in cell " & e.Cell.GetReferenceA1())
e.DataTableValue = Nothing
e.Action = DataTableExporterAction.Continue
End Sub
Object DataTableExporter
See Also
How to: Export a Worksheet Range to a DataTable