GridWeb から DataTable をエクスポートする

ワークシート データのエクスポート

特定の DataTable へ

ワークシート データを特定の DataTable オブジェクトにエクスポートするには:

  1. Aspose.Cells.GridWeb コントロールを Web フォームに追加します。
  2. 特定の DataTable オブジェクトを作成します。
  3. 選択したセルのデータを指定した 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 オブジェクトにエクスポートする必要がある場合があります。

次の例では、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();