Back to Devexpress

DataTableExporter Class

officefileapi-devexpress-dot-spreadsheet-dot-export.md

latest6.5 KB
Original Source

DataTableExporter Class

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

Declaration

csharp
public class DataTableExporter
vb
Public Class DataTableExporter

The following members return DataTableExporter objects:

Remarks

To export a worksheet range to a DataTable, perform the following steps.

  1. 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.

  2. Instantiate a DataTableExporter object.

  3. Specify general export options using the DataTableExporter.Options property.

  4. Change default converter settings (if required) using the DataTableExportOptions.DefaultCellValueToColumnTypeConverter and the DataTableExportOptions.DefaultCellValueToStringConverter properties.

  5. Add custom converters, if required, using the DataTableExportOptions.CustomConverters property.

  6. Call the DataTableExporterExtensions.Export method.

Example

View Example

csharp
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;
        }
vb
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

Inheritance

Object DataTableExporter

Extension Methods

Export()

See Also

DataTableExporter Members

How to: Export a Worksheet Range to a DataTable

How to: Use a Custom Converter for Export to a DataTable

DevExpress.Spreadsheet.Export Namespace