'Freeze panes in Excel using C# and EPPlus
I want to freeze first 5 columns and three rows in excel. I have written the following code for that
Worksheets.View.FreezePanes(5, 5);
but it freezes columns in first 4 rows also. I want to freeze first 4 columns in excel except in first 4 rows. Is it possible to do?
Solution 1:[1]
The first value is for how many rows you want frozen, and the second is for how many columns you want frozen. Therefore, to freeze the first 3 rows and 5 columns you would need to call it as the following:
Worksheets.View.FreezePanes(3, 5);
You can also take a look at this SO question for more information on FreezePanes
.
Solution 2:[2]
For me to freeze the first row following code worked. I am not sure what is logic there.
worksheet.View.FreezePanes(2,1);
Solution 3:[3]
From the ExcelWorksheet object, access the View property.
On the returned ExcelWorksheetView object, call the FreezePanes(row, column) method, passing the row and column of the first cell which is NOT frozen.
For example, to freeze the first complete two panes of Excel Worksheet, you would need to pass in the column (3,1) to the row parameter:
worksheetObject.View.FreezePanes(3, 1);
So to Freeze only first row completely you can now call worksheetObject.View.FreezePanes(2,1);
only!
This is also mentioned in official Example of EPPlus.
Therefore to answer original question raised by @user2148124 the answer should be
worksheetObject.View.FreezePanes(3, 5);
Solution 4:[4]
I know it's a long time since last post in the topic, but I was recently dealing with this problem and I found that way to get what I wanted (EPPlus v4.5.3):
public static void FreezeHeader(ExcelWorksheet sheet)
{
var xdoc = sheet.WorksheetXml;
var sheetViews = xdoc.GetElementsByTagName("sheetViews");
var sheetViewNode = sheetViews[0].ChildNodes[0];
var paneNode = xdoc.CreateNode(System.Xml.XmlNodeType.Element, "pane", xdoc.DocumentElement.NamespaceURI);
var ySplit = xdoc.CreateAttribute("ySplit");
ySplit.Value = "1";
var topLeftCell = xdoc.CreateAttribute("topLeftCell");
topLeftCell.Value = "A2";
var activePane = xdoc.CreateAttribute("activePane");
activePane.Value = "bottomLeft";
var state = xdoc.CreateAttribute("state");
state.Value = "frozen";
paneNode.Attributes.Append(ySplit);
paneNode.Attributes.Append(topLeftCell);
paneNode.Attributes.Append(activePane);
paneNode.Attributes.Append(state);
sheetViewNode.AppendChild(paneNode);
}
I achieved this by comparing the xml of two Excel files (one with freezed header and another witouht).
Typically when creating a simple excel file, you get the following :
<sheetViews>
<sheetView workbookViewId="0">
</sheetView>
</sheetViews>
Now if you freeze the first row and examine the xml, you will see that :
<sheetViews>
<sheetView tabSelected="1" topLeftCell="Z1" zoomScale="85" zoomScaleNormal="85" workbookViewId="0">
<pane ySplit="1" topLeftCell="A213" activePane="bottomLeft" state="frozen"/>
<selection activeCell="O1" sqref="O1"/><selection pane="bottomLeft" activeCell="AD229" sqref="AD229"/>
</sheetView>
From that I deduced I had to add the "pane" node to the xml structue:
<pane ySplit="1" topLeftCell="A213" activePane="bottomLeft" state="frozen"/>
That's what the code I provide is doing :-)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | CPW |
Solution 3 | |
Solution 4 | Lopopix |