从 GridWeb 导出 DataTable
Contents
[
Hide
]
将 DataView 导入到 GridWeb讨论了将 DataView 的内容导入到 Aspose.Cells.GridWeb 控件。本主题讨论了将来自 Aspose.Cells.GridWeb 控件的数据导出到 DataTable。
导出工作表数据
导出到特定的 DataTable
将工作表数据导出到特定的 DataTable 对象:
- 将Aspose.Cells.GridWeb控件添加到您的Web表单中。
- 创建特定的 DataTable 对象。
- 将选定单元格的数据导出到指定的 DataTable 对象。
下面的示例创建一个具有四列的特定 DataTable 对象。工作表数据从第一个单元格开始导出,工作表中所有可见的行和列都导出到已创建的 DataTable 对象。
This file contains 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("Name", System.Type.GetType("System.String")); | |
dataTable.Columns.Add("Gender", System.Type.GetType("System.String")); | |
dataTable.Columns.Add("Age", System.Type.GetType("System.Int32")); | |
dataTable.Columns.Add("Class", System.Type.GetType("System.String")); | |
// Accessing the reference of the worksheet that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Getting the total number of rows and columns inside the worksheet | |
int totalColumns = sheet.Cells.MaxColumn + 1; | |
int totalRows = sheet.Cells.MaxRow + 1; | |
// Exporting the data of the active worksheet to a specific DataTable object | |
dataTable = sheet.Cells.Export(0, 0, totalRows, totalColumns, true, true); | |
// Display exported data table in GridView | |
GridView1.DataSource = dataTable; | |
GridView1.DataBind(); |
导出到新的 DataTable
有时,您不想创建 DataTable 对象,而只需将工作表数据导出到新的 DataTable 对象。
下面的示例尝试以不同方式展示 Export 方法的使用。它获取活动工作表的引用,将该工作表的完整数据导出到新的 DataTable 对象。现在可以以任何方式使用 DataTable 对象。例如,可以将 DataTable 对象绑定到 GridView 以查看数据。
This file contains 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 | |
GridWorksheet sheet1 = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Getting the total number of rows and columns inside the worksheet | |
int totalColumns1 = sheet.Cells.MaxColumn + 1; | |
int totalRows1 = sheet.Cells.MaxRow + 1; | |
// Exporting the data of the active worksheet to a new DataTable object | |
DataTable dt = sheet.Cells.Export(0, 0, totalRows1, totalColumns1, true, true); | |
// Display exported data table in GridView | |
GridView2.DataSource = dataTable; | |
GridView2.DataBind(); |