小計の適用と詳細の下のアウトライン集計行の方向の変更
Contents
[
Hide
]
この記事では、データに小計を適用する方法と、詳細の下にあるアウトライン集計行の方向を変更する方法について説明します。
を使用して小計をデータに適用できますWorksheet.Cells.Subtotal()方法。次のパラメータを取ります。
- セルエリア 小計を適用する範囲
- グループ化 ゼロベースの整数オフセットとして、グループ化するフィールド
- 関数 小計機能。
- トータルリスト 小計が追加されるフィールドを示すゼロから始まるフィールド オフセットの配列。
- 交換 現在の小計を置き換えるかどうかを示します
- 改ページ - グループ間に改ページを追加するかどうかを示します
- 概要以下のデータ データの下に要約を追加するかどうかを示します。
また、アウトラインの方向を制御できます詳細の下の要約行次のスクリーンショットに示すように、Worksheet.Outline.SummaryRowBelow プロパティを使用しています。この設定は、Microsoft Excel で開くことができます。データ > アウトライン > 設定
ソース ファイルと出力ファイルのイメージ
次のスクリーンショットは、以下のサンプル コードで使用されるソース Excel ファイルを示しており、列 A と B にいくつかのデータが含まれています。
次のスクリーンショットは、サンプル コードによって生成された出力 Excel ファイルを示しています。ご覧のとおり、小計は範囲 A2:B11 に適用され、アウトラインの方向は詳細の下の集計行です。
C# 小計を適用し、アウトライン集計行の方向を変更するコード
上記の出力を実現するためのサンプル コードを次に示します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 workbook from source Excel file | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Get the Cells collection in the first worksheet | |
Cells cells = worksheet.Cells; | |
// Create a cellarea i.e.., A2:B11 | |
CellArea ca = CellArea.CreateCellArea("A2", "B11"); | |
// Apply subtotal, the consolidation function is Sum and it will applied to Second column (B) in the list | |
cells.Subtotal(ca, 0, ConsolidationFunction.Sum, new int[] { 1 }, true, false, true); | |
// Set the direction of outline summary | |
worksheet.Outline.SummaryRowBelow = true; | |
// Save the excel file | |
workbook.Save(dataDir + "output_out.xlsx"); |