To create multi-row column headers in ReoGrid, you can utilize the ExtensionColumnHeader
property of the worksheet. This allows you to extend the column header area vertically to accommodate multiple rows of headers, up to a maximum of 10 rows. The cells within these header rows can be merged horizontally or vertically, and you have the freedom to customize the header text and style as needed.
Creating Multi-Row Headers
Access the Extension Column Header: First, get a reference to the extension column header from your worksheet.
var extensionHeader = worksheet.ExtensionColumnHeader;
Set the Number of Rows: Define the number of rows for the column headers. You can do this using the RowCount
property or the SetRowCount
method.
extensionHeader.SetRowCount(3); // Sets the header to have 3 rows
The results are as shown in the following figure.
Access Cells
Cells in multi-row headers, like cells in a worksheet, have their origin coordinates (0,0) at the top left corner.
Merging Cells
To merge cells in multi-row headers, use the MergeCells
method. You can set the rows and columns you want to combine as parameters.
extensionHeader.MergeCells(int startRow, int startColumn, int rowSpan, int columnSpan)
Create the code below.
// Merge first cell (0, 0)
extensionHeader.MergeCells(0, 0, 3, 1);
// Merge from cell in row 0, 2nd column (0, 1)
extensionHeader.MergeCells(0, 1, 1, 4);
Set Cell Text
extensionHeader[0, 0].Text = "No.";
extensionHeader[0, 1].Text = "大分類";
Repeat the MergeCells
and Text settings to create a multi-line header.
extensionHeader.MergeCells(0, 0, 3, 1);
extensionHeader.MergeCells(0, 1, 1, 4);
extensionHeader.MergeCells(1, 1, 1, 2);
extensionHeader.MergeCells(1, 3, 1, 2);
extensionHeader.MergeCells(0, 5, 3, 1);
extensionHeader.MergeCells(0, 6, 1, 4);
extensionHeader.MergeCells(1, 6, 2, 1);
extensionHeader.MergeCells(1, 8, 2, 2);
extensionHeader[0, 0].Text = "No.";
extensionHeader[0, 1].Text = "大分類";
extensionHeader[1, 1].Text = "中分類";
extensionHeader[1, 3].Text = "中分類";
extensionHeader[0, 6].Text = "大分類";
extensionHeader[2, 1].Text = "小分類";
extensionHeader[2, 2].Text = "小分類";
extensionHeader[2, 3].Text = "小分類";
extensionHeader[2, 4].Text = "小分類";
extensionHeader[0, 5].Text = "対象";
The results are as follows.
Unmerging Cells
You can unmerge cells using the UnmergeCells
method.
extensionHeader.UnmergeCells(0, 7);
About Column and Cell Selection
Multi-row column headers can always select individual columns, regardless of whether cells are merged horizontally. Selected columns are displayed in a highlighted color.
If some columns of merged cells are selected, the cells will be displayed in highlight color.
About displaying/hiding columns
If you set a column to be hidden, a thick line will appear in place of the hidden column.
Use with worksheet freeze
Multi-row column headers can be used with worksheet freeze just like regular column headers. If the worksheet is scrolled, some columns will be hidden as shown below.
Use with outlines
Multi-row column headers can be used with outlines just like regular column headers, if the columns are collapsed, the header cells will be hidden.