0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SpreadsheetGearでExcelのグループ化をする

Posted at
GroupingSample.cs
class GroupingDef {
	public int ColIdx { get; set; }
	public int RowIdx { get; set; }
	public int ColLen { get; set; }
	public int RowLen { get; set; }

	/// <summary>
	/// 行のグループ化であるか
	/// </summary>
	public bool IsRow { get; set; }

	/// <summary>
	/// 集計行(列)を詳細データの下(右)にするか
	/// </summary>
	public bool IsBelowSummaryPosition { get; set; }
}

class GroupingSample {

	public void SetGrouping(SpreadsheetGear.IWorksheet worksheet, GroupingDef[] defList) {
		//アウトラインの集計行列の位置を設定
		if (defList.Count() > 0) {
			if (defList[0].IsBelowSummaryPosition) {
				if (defList[0].IsRow) {
					//詳細データの下
					worksheet.Outline.SummaryRow = SpreadsheetGear.SummaryRow.Below;
				} else {
					//詳細データの右
					worksheet.Outline.SummaryColumn = SpreadsheetGear.SummaryColumn.Right;
				}
			} else {
				if (defList[0].IsRow) {
					//詳細データの上
					worksheet.Outline.SummaryRow = SpreadsheetGear.SummaryRow.Above;
				} else {
					//詳細データの左
					worksheet.Outline.SummaryColumn = SpreadsheetGear.SummaryColumn.Left;
				}
			}
		}

		//グループ化設定
		foreach (var def in defList) {
			SpreadsheetGear.IRange range = worksheet.Cells[def.RowIdx, def.ColIdx, def.RowLen, def.ColLen];
			if (def.IsRow) {
				//行のグループ化
				if (range.EntireRow.OutlineLevel < 8) {		//最大8段階
					range.EntireRow.Group();
				}
			} else {
				//列のグループ化
				if (range.EntireColumn.OutlineLevel < 8) {		//最大8段階
					range.EntireColumn.Group();
				}
			}
		}
	}
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?