集計の作成

Microsoft Excel の使用

Microsoft Excelでサブトータルを作成するには:

  1. ブック1.xlsとして保存、ブックの最初のワークシートに簡単なデータリストを作成します(以下の図を参照)。

  2. リスト内の任意のセルを選択します。

  3. データ メニューから 集計 を選択します。 サブトータルダイアログが表示されます。使用する機能やサブトータルを配置する場所を定義します。

    サブトータルを追加するデータ範囲を選択

todo:image_alt_text

サブトータルダイアログ

todo:image_alt_text

Aspose.Cells APIを使用する

Aspose.Cellsには、Microsoft Excelファイルを表すクラス Workbook が提供されています。 Workbook クラスには、Excelファイル内の各ワークシートにアクセスするのを可能にする WorksheetCollection が含まれています。

ワークシートは Worksheet クラスで表されます。このクラスはワークシートや他のオブジェクトを管理するための多くのプロパティやメソッドを提供します。各ワークシートは Cells コレクションで構成されます。ワークシートでサブトータルを作成するには、Cells クラスのsubtotal メソッドを使用します。メソッドのパラメーターに適切な値を指定して、望む結果を得るために使用します。

以下の例は、Aspose.Cells APIを使用してテンプレートファイル(Book1.xls)の最初のワークシートにサブトータルを作成する方法を示しています。

コードを実行すると、サブトータルが追加されたワークシートが作成されます。

サブトータルを適用

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(CreatingSubtotals.class) + "data/";
// Instantiate a new workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Get the Cells collection in the first worksheet
Cells cells = workbook.getWorksheets().get(0).getCells();
// Create a cellarea i.e.., B3:C19
CellArea ca = new CellArea();
ca.StartRow = 2;
ca.StartColumn = 1;
ca.EndRow = 18;
ca.EndColumn = 2;
// Apply subtotal, the consolidation function is Sum and it will applied
// to
// Second column (C) in the list
cells.subtotal(ca, 0, ConsolidationFunction.SUM, new int[] { 1 });
// Save the excel file
workbook.save(dataDir + "CreatingSubtotals_out.xls");
// Print message
System.out.println("Process completed successfully");