GridWebからDataTableをエクスポート
ワークシートデータのエクスポート
特定のDataTableに
ワークシートデータを特定のDataTableオブジェクトにエクスポートするには:
- Aspose.Cells.GridWebコントロールをWebフォームに追加します。
- 特定のDataTableオブジェクトを作成します。
- 選択したセルのデータを指定されたDataTableオブジェクトにエクスポートします。
以下の例では、4つの列を持つ特定のDataTableオブジェクトが作成されます。ワークシートデータは、ワークシートで可視なすべての行と列から始まって、すでに作成された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("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にバインドしてデータを表示することができます。
// 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(); |