Exporting Data from Grid
Exporting Grid Contents
Exporting To a Specific DataTable
To export the Grid contents to a specific DataTable object, please follow the steps below:Add Aspose.Cells.GridDesktop control to your Form.
- Create a specific DataTable object according to your needs.
- Export the data of a selected Worksheet to your specified DataTable object.
In the example given below, we have created a specific DataTable object having four columns inside. Finally, we exported worksheet data (starting from first cell with 69 rows and 4 columns) to a DataTable object already created by us.
Example:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Creating a new DataTable object | |
DataTable dataTable = new DataTable(); | |
// Adding specific columns to the DataTable object | |
dataTable.Columns.Add("ProductName", System.Type.GetType("System.String")); | |
dataTable.Columns.Add("CategoryName", System.Type.GetType("System.String")); | |
dataTable.Columns.Add("QuantityPerUnit", System.Type.GetType("System.String")); | |
dataTable.Columns.Add("UnitsInStock", System.Type.GetType("System.Int32")); | |
// Exporting the data of the first worksheet of the Grid to the specific DataTable object | |
dataTable = gridDesktop1.Worksheets[0].ExportDataTable(dataTable, 0, 0, 69, 4, true); |
Exporting To a New DataTable
Sometimes, developers may not be interested in creating their own DataTable object and might have a simple need to just export the worksheet data to a new DataTable object. It would be more quickest way for the developers to just export the worksheet data.
In the example given below, we have tried a different way to explain the usage of ExportDataTable method. We have taken the reference of the worksheet that is currently active and then we exported the complete data of that active worksheet to a new DataTable object. Now, this DataTable object can be used in any way a developer wants. Just for an instance, a developer may bind this DataTable object to a DataGrid to view the data.
Example:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing the reference of the worksheet that is currently active | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
//Getting the total number of rows and columns inside the worksheet | |
int totalRows = sheet.RowsCount; | |
int totalCols = sheet.ColumnsCount; | |
// Exporting the data of the active worksheet to a new DataTable object | |
DataTable table = sheet.ExportDataTable(0, 0, totalRows, totalCols, false, true); |