从Grid中导出数据
导出Grid内容
导出到特定的DataTable
要将Grid内容导出到特定的DataTable对象,请按照以下步骤进行:将Aspose.Cells.GridDesktop控件添加到您的表单中。
- 根据您的需求创建一个特定的DataTable对象。
- 将所选的工作表数据导出到您指定的DataTable对象。
在下面的示例中,我们创建了一个特定的DataTable对象,该对象内部有四列。最后,我们将工作表数据(从第一个单元格开始,共69行4列)导出到由我们创建的DataTable对象中。
示例:
// 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); |
导出到新的DataTable
有时,开发人员可能对创建自己的DataTable对象不感兴趣,而只是需要简单地将工作表数据导出到一个新的DataTable对象。对于开发人员来说,仅仅将工作表数据导出到新的DataTable对象是更快速的方式。
在下面的示例中,我们尝试通过不同的方式来解释ExportDataTable方法的用法。我们使用了当前处于活动状态的工作表的引用,然后将该活动工作表的完整数据导出到一个新的DataTable对象中。现在,这个DataTable对象可以按照开发人员的意愿进行使用。举个例子,开发人员可以将这个DataTable对象绑定到DataGrid上以查看数据。
示例:
// 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); |