Microsoft Excel ファイルのテーブルを作成して管理します。

テーブルの作成

スプレッドシートの利点の 1 つは、電話番号リスト、タスク リスト、取引リスト、資産または負債のリストなど、さまざまな種類のリストを作成できることです。複数のユーザーが協力して、さまざまなリストを使用、作成、および維持できます。

Aspose.Cells は、リストの作成と管理をサポートしています。

リスト オブジェクトの利点

データのリストを実際のリスト オブジェクトに変換すると、多くの利点があります。

  • 新しい行と列が自動的に含まれます。
  • リストの下部に合計行を簡単に追加して、SUM、AVERAGE、COUNT などを表示できます。
  • 右側に追加された列は、List オブジェクトに自動的に組み込まれます。
  • 行と列に基づくグラフは自動的に展開されます。
  • 行と列に割り当てられた名前付き範囲は、自動的に展開されます。
  • リストは、偶発的な行と列の削除から保護されています。

Microsoft Excel を使用してリスト オブジェクトを作成する

  • List オブジェクトを作成するためのデータ範囲の選択
  • [リストの作成] ダイアログが表示されます。
  • データの List オブジェクトを実装し、合計行を指定します (選択データ、 それからリスト、 に続く合計行).

Aspose.Cells API を使用

Aspose.Cells はクラスを提供し、ワークブック、Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。

ワークシートは、ワークシートクラス。のワークシートクラスには、ワークシートを管理するためのさまざまなプロパティとメソッドが用意されています。を作成するにはListObjectワークシートでは、リストオブジェクトのコレクション プロパティワークシートクラス。各ListObject実際には、ListObjectCollectionクラスは、さらに追加List オブジェクトを追加し、リストのセル範囲を指定するメソッド。

指定されたセル範囲に従って、List オブジェクトは Aspose.Cells によって作成されます。属性を使用します (たとえば、ShowTotals, ListColumnsなど)のListObjectリストを制御するクラス。

以下の例では、同じものを作成しましたListObject上記のセクションで Microsoft Excel を使用して作成したように、Aspose.Cells API を使用します。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create a Workbook object.
// Open a template excel file.
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the List objects collection in the first worksheet.
Aspose.Cells.Tables.ListObjectCollection listObjects = workbook.Worksheets[0].ListObjects;
// Add a List based on the data source range with headers on.
listObjects.Add(1, 1, 7, 5, true);
// Show the total row for the List.
listObjects[0].ShowTotals = true;
// Calculate the total of the last (5th ) list column.
listObjects[0].ListColumns[4].TotalsCalculation = Aspose.Cells.Tables.TotalsCalculation.Sum;
// Save the excel file.
workbook.Save(dataDir + "output.xls");

テーブルをフォーマットする

関連するデータのグループを管理および分析するために、セルの範囲をリスト オブジェクト (Excel テーブルとも呼ばれます) に変換することができます。テーブルは、他の行や列のデータとは独立して管理される関連データを含む一連の行と列です。既定では、テーブルのすべての列のヘッダー行でフィルター処理が有効になっているため、リスト オブジェクト データをすばやくフィルター処理または並べ替えることができます。各合計行セルの集計関数のドロップダウン リストを提供するリスト オブジェクトに、合計行 (数値データの操作に役立つ集計関数の選択を提供するリスト内の特別な行) を追加できます。 Aspose.Cells は、リスト (またはテーブル) を作成および管理するためのオプションを提供します。

リスト オブジェクトの書式設定

Aspose.Cells はクラスを提供し、ワークブック、Microsoft Excel ファイルを表します。のワークブッククラスにはワークシートExcel ファイル内の各ワークシートにアクセスできるコレクション。

ワークシートは、ワークシートクラス。のワークシートクラスには、ワークシートを管理するためのさまざまなプロパティとメソッドが用意されています。を作成するにはListObjectワークシートで、使用リストオブジェクトのコレクション プロパティワークシートクラス。各ListObject実際には、ListObjectCollectionクラスは、さらに追加メソッドを使用して List オブジェクトを追加し、それが包含するセルの範囲を指定します。指定されたセル範囲に従って、ListObjectは Aspose.Cells によってワークシートに作成されます。属性を使用します (たとえば、表スタイルの種類 ) のListObjectクラスを使用して、要件に合わせてテーブルをフォーマットします。

以下の例では、サンプル データをワークシートに追加し、ListObjectそれにデフォルトのスタイルを適用します。ListObjectスタイルは、Microsoft Excel 2007/2010 でサポートされています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a workbook.
Workbook workbook = new Workbook();
// Obtaining the reference of the default(first) worksheet
Worksheet sheet = workbook.Worksheets[0];
// Obtaining Worksheet's cells collection
Cells cells = sheet.Cells;
// Setting the value to the cells
Aspose.Cells.Cell cell = cells["A1"];
cell.PutValue("Employee");
cell = cells["B1"];
cell.PutValue("Quarter");
cell = cells["C1"];
cell.PutValue("Product");
cell = cells["D1"];
cell.PutValue("Continent");
cell = cells["E1"];
cell.PutValue("Country");
cell = cells["F1"];
cell.PutValue("Sale");
cell = cells["A2"];
cell.PutValue("David");
cell = cells["A3"];
cell.PutValue("David");
cell = cells["A4"];
cell.PutValue("David");
cell = cells["A5"];
cell.PutValue("David");
cell = cells["A6"];
cell.PutValue("James");
cell = cells["A7"];
cell.PutValue("James");
cell = cells["A8"];
cell.PutValue("James");
cell = cells["A9"];
cell.PutValue("James");
cell = cells["A10"];
cell.PutValue("James");
cell = cells["A11"];
cell.PutValue("Miya");
cell = cells["A12"];
cell.PutValue("Miya");
cell = cells["A13"];
cell.PutValue("Miya");
cell = cells["A14"];
cell.PutValue("Miya");
cell = cells["A15"];
cell.PutValue("Miya");
cell = cells["B2"];
cell.PutValue(1);
cell = cells["B3"];
cell.PutValue(2);
cell = cells["B4"];
cell.PutValue(3);
cell = cells["B5"];
cell.PutValue(4);
cell = cells["B6"];
cell.PutValue(1);
cell = cells["B7"];
cell.PutValue(2);
cell = cells["B8"];
cell.PutValue(3);
cell = cells["B9"];
cell.PutValue(4);
cell = cells["B10"];
cell.PutValue(4);
cell = cells["B11"];
cell.PutValue(1);
cell = cells["B12"];
cell.PutValue(1);
cell = cells["B13"];
cell.PutValue(2);
cell = cells["B14"];
cell.PutValue(2);
cell = cells["B15"];
cell.PutValue(2);
cell = cells["C2"];
cell.PutValue("Maxilaku");
cell = cells["C3"];
cell.PutValue("Maxilaku");
cell = cells["C4"];
cell.PutValue("Chai");
cell = cells["C5"];
cell.PutValue("Maxilaku");
cell = cells["C6"];
cell.PutValue("Chang");
cell = cells["C7"];
cell.PutValue("Chang");
cell = cells["C8"];
cell.PutValue("Chang");
cell = cells["C9"];
cell.PutValue("Chang");
cell = cells["C10"];
cell.PutValue("Chang");
cell = cells["C11"];
cell.PutValue("Geitost");
cell = cells["C12"];
cell.PutValue("Chai");
cell = cells["C13"];
cell.PutValue("Geitost");
cell = cells["C14"];
cell.PutValue("Geitost");
cell = cells["C15"];
cell.PutValue("Geitost");
cell = cells["D2"];
cell.PutValue("Asia");
cell = cells["D3"];
cell.PutValue("Asia");
cell = cells["D4"];
cell.PutValue("Asia");
cell = cells["D5"];
cell.PutValue("Asia");
cell = cells["D6"];
cell.PutValue("Europe");
cell = cells["D7"];
cell.PutValue("Europe");
cell = cells["D8"];
cell.PutValue("Europe");
cell = cells["D9"];
cell.PutValue("Europe");
cell = cells["D10"];
cell.PutValue("Europe");
cell = cells["D11"];
cell.PutValue("America");
cell = cells["D12"];
cell.PutValue("America");
cell = cells["D13"];
cell.PutValue("America");
cell = cells["D14"];
cell.PutValue("America");
cell = cells["D15"];
cell.PutValue("America");
cell = cells["E2"];
cell.PutValue("China");
cell = cells["E3"];
cell.PutValue("India");
cell = cells["E4"];
cell.PutValue("Korea");
cell = cells["E5"];
cell.PutValue("India");
cell = cells["E6"];
cell.PutValue("France");
cell = cells["E7"];
cell.PutValue("France");
cell = cells["E8"];
cell.PutValue("Germany");
cell = cells["E9"];
cell.PutValue("Italy");
cell = cells["E10"];
cell.PutValue("France");
cell = cells["E11"];
cell.PutValue("U.S.");
cell = cells["E12"];
cell.PutValue("U.S.");
cell = cells["E13"];
cell.PutValue("Brazil");
cell = cells["E14"];
cell.PutValue("U.S.");
cell = cells["E15"];
cell.PutValue("U.S.");
cell = cells["F2"];
cell.PutValue(2000);
cell = cells["F3"];
cell.PutValue(500);
cell = cells["F4"];
cell.PutValue(1200);
cell = cells["F5"];
cell.PutValue(1500);
cell = cells["F6"];
cell.PutValue(500);
cell = cells["F7"];
cell.PutValue(1500);
cell = cells["F8"];
cell.PutValue(800);
cell = cells["F9"];
cell.PutValue(900);
cell = cells["F10"];
cell.PutValue(500);
cell = cells["F11"];
cell.PutValue(1600);
cell = cells["F12"];
cell.PutValue(600);
cell = cells["F13"];
cell.PutValue(2000);
cell = cells["F14"];
cell.PutValue(500);
cell = cells["F15"];
cell.PutValue(900);
// Adding a new List Object to the worksheet
Aspose.Cells.Tables.ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add("A1", "F15", true)];
// Adding Default Style to the table
listObject.TableStyleType = Aspose.Cells.Tables.TableStyleType.TableStyleMedium10;
// Show Total
listObject.ShowTotals = true;
// Set the Quarter field's calculation type
listObject.ListColumns[1].TotalsCalculation = Aspose.Cells.Tables.TotalsCalculation.Count;
// Saving the Excel file
workbook.Save(dataDir + "output.xlsx");

先行トピック