-
Notifications
You must be signed in to change notification settings - Fork 297
ToDataTable
Mats Alm edited this page Nov 3, 2020
·
24 revisions
This method lets you export data from a range or a table into a System.Data.DataTable.
You can call this method without arguments, in this case the method will execute with its default behaviour. var dataTable = worksheet.Cells["A1:F2"].ToDataTable(); In this case it is assumed that:
- The first row of the range contains the column names, the remaining rows contains data.
- Errors in the cells will be handled as blank values.
- Blank values are not allowed for value types (InvalidOperationException will be called).
- Rows where every cell is empty will be ignored.
- The data type of each DataColumn in the DataTable will be determined by the first non blank cell in the corresponding column in the range.
You can control the behaviour of the ToDataTable method by setting values on a ToDataTableOptions instance. This can be done either by using the method signature with an Action<ToDataTableOptions> argument:
// set the DataTable's Name property to "dt2"
var dt2 = sheet.Cells["A1:F11"].ToDataTable(o => o.DataTableName = "dt2");...or by supplying an instance of the ToDataTableOptions class to the ToDataTable method:
var options = ToDataTableOptions.Create();
options.DataTableName = "dt2";
var dt2 = sheet.Cells["A1:F1"].ToDataTable(options);Here are the properties you can set on the ToDataTableOptions class:
| Name | Data type | Default value | Description |
|---|---|---|---|
| FirstRowIsColumnNames | bool | true | Indicates whether the first row of the range are the column names. |
| ColumnNameParsingStrategy | NameParsingStrategy | Preserve | If the FirstRowIsColumnNames is true, this property controls how the column names are parsed. Preserve - Preserve the input string as it is, SpaceToUnderscore - Replaces any spaces with underscores, RemoveSpace - Removes all spaces. |
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles