DataTableExporterExtensions.Export(DataTableExporter) Method
Performs export of the cell range to the DataTable.
You require a license to the DevExpress Office File API or DevExpress Universal Subscription to use this method in production code.
Namespace: DevExpress.Spreadsheet.Export
Assembly: DevExpress.Docs.v18.2.dll
Declaration
Parameters
Name | Type | Description |
---|---|---|
exporter | DataTableExporter | A DataTableExporter object created to export data. |
Example
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-export-cell-range-to-a-datatable-e4997
using DevExpress.Spreadsheet;
using DevExpress.Spreadsheet.Export;
Worksheet worksheet = spreadsheetControl1.Document.Worksheets.ActiveWorksheet;
Range range = worksheet.Selection;
bool rangeHasHeaders = this.barCheckItemHasHeaders1.Checked;
// Create a data table with column names obtained from the first row in a range if it has headers.
// 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, rangeHasHeaders);
//Validate cell value types. If cell value types in a column are different, the column values are exported as text.
for (int col = 0; col < range.ColumnCount; col++)
{
CellValueType cellType = range[0, col].Value.Type;
for (int r = 1; r < range.RowCount; r++)
{
if (cellType != range[r, col].Value.Type)
{
dataTable.Columns[col].DataType = typeof(string);
break;
}
}
}
// Create the exporter that obtains data from the specified range,
// skips the header row (if required) and populates the previously created data table.
DataTableExporter exporter = worksheet.CreateDataTableExporter(range, dataTable, rangeHasHeaders);
// Handle value conversion errors.
exporter.CellValueConversionError += exporter_CellValueConversionError;
// 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;
}
See Also