WorksheetExtensions.Import(Worksheet, DataTable, Boolean, Int32, Int32) Method
Imports data from a data table.
You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this method in production code.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Docs.v24.2.dll
NuGet Package: DevExpress.Document.Processor
Declaration
public static void Import(
this Worksheet sheet,
DataTable source,
bool addHeader,
int firstRowIndex,
int firstColumnIndex
)
Parameters
Name | Type | Description |
---|---|---|
sheet | Worksheet | A Worksheet that is the worksheet to which the data is imported. |
source | DataTable | A DataTable object that is the data source for import. |
addHeader | Boolean | true, to insert column names to the row above the cells containing imported data; otherwise, false. |
firstRowIndex | Int32 | An integer that is the row index of the start cell in which the imported data will be inserted. |
firstColumnIndex | Int32 | An integer that is the column index of the start cell in which the imported data will be inserted. |
Remarks
The following code imports data from a DataTable object to a worksheet:
Note that the cell data types are set automatically according to the data types of the source column. Cell formats are set automatically to the default value for the cell data type. Refer to the following topic for information on how to change this behavior: How to: Specify Number or Date Format for Cell Content
byte[] imageBytes1 = File.ReadAllBytes("images//img.png");
byte[] imageBytes2 = File.ReadAllBytes("images//x-docserver.png");
// ...
Workbook workbook = new Workbook();
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Clear(worksheet.GetUsedRange());
ImportDataTable(worksheet);
// ...
}
finally
{
workbook.EndUpdate();
workbook.SaveDocument("result.xlsx");
Process.Start(new ProcessStartInfo("result.xlsx") { UseShellExecute = true });
}
workbook.Dispose();
// ...
void ImportDataTable(Worksheet worksheet)
{
// Create a "Products" DataTable object with four columns.
DataTable sourceTable = new DataTable("Products");
sourceTable.Columns.Add("Product", typeof(string));
sourceTable.Columns.Add("Price", typeof(float));
sourceTable.Columns.Add("Quantity", typeof(Int32));
sourceTable.Columns.Add("Discount", typeof(float));
sourceTable.Columns.Add("Image", typeof(byte[]));
sourceTable.Rows.Add("Chocolade", 5, 15, 0.03, imageBytes1);
sourceTable.Rows.Add("Konbu", 9, 55, 0.1, imageBytes1);
sourceTable.Rows.Add("Geitost", 15, 70, 0.07, imageBytes2);
// Import data from the data table into the worksheet and insert it, starting with cell B2.
worksheet.Import(sourceTable, true, 1, 1);
}
The image below shows the results.