Excel for .NET
グループと小計

Grouping enables you to categorize data based on their common attributes. In Excel, you can build an outline tree using the grouping feature where each group task is a summary task and all its children may have the same property values. Additionally, the grouped data can be collapsed and expanded to facilitate easier analysis of the sheet data.

You can even create sub-totals in Excel by defining the outline levels of rows and columns by using the OutlineLevel property.

To create groups and subtotals in Excel, use the following code. In this example, we create groups by defining outline levels of rows and columns and show subtotals by applying a formula on cell values. For more information on cell formulas, see Apply Formula.

C#
コードのコピー
c1XLBook1.Sheets.Add("Group and SubTotal");
XLSheet sheet = c1XLBook1.Sheets[1];
XLStyle totalStyle = new XLStyle(c1XLBook1);
totalStyle.Font = new Font(c1XLBook1.DefaultFont, FontStyle.Bold);
sheet[2, 1].Value = "Number";
sheet[2, 2].Value = "ID";
sheet[3, 1].Value = 12;
sheet[3, 2].Value = 17;
sheet.Rows[3].OutlineLevel = 2;
sheet.Rows[3].Visible = false;
sheet[4, 1].Value = 12;
sheet[4, 2].Value = 14;
sheet.Rows[4].OutlineLevel = 2;
sheet.Rows[4].Visible = false;
sheet[5, 1].Value = "12 Total";
sheet[5, 1].Style = totalStyle;
sheet[5, 2].Value = 31;
sheet[5, 2].Formula = "SUBTOTAL(9,C4:C5)";
sheet.Rows[5].OutlineLevel = 1;
sheet[6, 1].Value = 34;
sheet[6, 2].Value = 109;
sheet.Rows[6].OutlineLevel = 2;
sheet[7, 1].Value = "34 Total";
sheet[7, 1].Style = totalStyle;
sheet[7, 2].Value = 109;
sheet[7, 2].Formula = "SUBTOTAL(9,C7:C7)";
sheet.Rows[7].OutlineLevel = 1;
sheet[8, 1].Value = "Grand Total";
sheet[8, 1].Style = totalStyle;
sheet[8, 2].Value = 140;
sheet[8, 2].Formula = "SUBTOTAL(9,C4:C7)";
sheet.Rows[8].OutlineLevel = 0;