Gridからデータをエクスポート

グリッドコンテンツのエクスポート

特定のDataTableにエクスポート

グリッドのコンテンツを特定のDataTableオブジェクトにエクスポートするには、以下の手順に従ってください:Aspose.Cells.GridDesktopコントロールをFormに追加します。

  • 必要に応じて特定のDataTableオブジェクトを作成します。
  • 選択したWorksheetのデータを指定したDataTableオブジェクトにエクスポートします。

以下の例では、4つの列を持つ特定のDataTableオブジェクトを作成しました。最後に、作成済みのDataTableオブジェクトにワークシートデータ(最初のセルから始まり、69行と4列)をエクスポートしました。

例:

// 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オブジェクトにエクスポートするだけでよい場合があります。これは開発者にとっては最も迅速な方法です。

以下の例では、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);