Introduction
In our previous topic, we have talked about importing the contents of a DataTable to Aspose.Grid.Desktop control but we purposely didn't mention that Aspose.Grid.Desktop supports the reverse process too. So, in this topic, we will discuss about exporting the data inside Aspose.Grid.Desktop control to a DataTable.
Exporting Grid Contents
To a Specific DataTable
To export the Grid contents to a specific DataTable object, please follow the steps below:
- Add Aspose.Grid.Desktop 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:
[C#]
//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);
[VB.NET]
'Creating a new DataTable object
Dim dataTable As 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)
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.
NOTE: In this case, we will use an overloaded version of ExportDataTable method that will simply return a new DataTable object containing data exported from worksheet.
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:
[C#]
//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);
[VB.NET]
'Accessing the reference of the worksheet that is currently active
Dim sheet As Worksheet = gridDesktop1.GetActiveWorksheet()
'Getting the total number of rows and columns inside the worksheet
Dim totalRows As Integer = sheet.RowsCount
Dim totalCols As Integer = sheet.ColumnsCount
'Exporting the data of the active worksheet to a new DataTable object
Dim table As DataTable = sheet.ExportDataTable(0,0,totalRows,totalCols,False,True)