从网格导出数据
Contents
[
Hide
]
在我们之前的主题中,我们谈到了将 DataTable 的内容导入到 Aspose.Cells.GridDesktop 控件,但我们故意没有提到 Aspose.Cells.GridDesktop 也支持相反的过程。因此,在本主题中,我们将讨论如何将 Aspose.Cells.GridDesktop 控件中的数据导出到 DataTable。
导出网格内容
导出到特定数据表
要将 Grid 内容导出到特定的 DataTable 对象,请按照以下步骤操作:将 Aspose.Cells.GridDesktop 控件添加到您的形式.
- 根据您的需要创建特定的 DataTable 对象。
- 导出选定的数据工作表到您指定的 DataTable 对象。
在下面给出的示例中,我们创建了一个内部有四列的特定 DataTable 对象。最后,我们将工作表数据(从具有 69 行和 4 列的第一个单元格开始)导出到我们已经创建的 DataTable 对象。
例子:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 对象。对于开发人员来说,仅导出工作表数据将是更快捷的方式。
在下面给出的示例中,我们尝试了不同的方式来解释 ExportDataTable 方法的用法。我们引用了当前活动的工作表,然后将该活动工作表的完整数据导出到新的 DataTable 对象。现在,这个 DataTable 对象可以以开发人员想要的任何方式使用。举个例子,开发人员可以将此 DataTable 对象绑定到 DataGrid 以查看数据。
例子:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
在上述情况下,我们将使用 ExportDataTable 方法的重载版本,该方法将简单地返回一个包含从工作表导出的数据的新 DataTable 对象。