Freeze Panes in OpenXml SDK 2.0 for Excel document

calendar_today Asked Jun 21, 2011
thumb_up 8 upvotes
history Updated April 16, 2026

Question posted 2011 · +5 upvotes

I’m generating an Excel workbook using OpenXml and have been following the examples at http://msdn.microsoft.com/en-us/library/cc850837.aspx

It would be really useful if I could freeze the top panes, but I can’t find a way to do this. I realise that I can do this if I use http://closedxml.codeplex.com/ but for now I’d like to stick to the OpenXml SDK

Any ideas?

Accepted answer +8 upvotes

I was trying to solve the same problem and ended up opening the Open XML SDK 2.0 Productivity Tool and using the Compare Files... feature to compare two spreadsheets, one with frozen panes and one without.

When I did that, I was led to code that looked basically like this:

WorkbookPart wbp = doc.WorkbookPart;
WorksheetPart wsp = wbp.WorksheetParts.First();

SheetViews sheetviews = wsp.Worksheet.GetFirstChild<SheetViews>();
SheetView sv = sheetviews.GetFirstChild<SheetView>();
Selection selection = sv.GetFirstChild<Selection>();
Pane pane = new Pane(){ VerticalSplit = 1D, TopLeftCell = "A2", ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen };
sv.InsertBefore(pane,selection);
selection.Pane = PaneValues.BottomLeft;

I added this to my program and it seemed to do the trick.

Excel VBA objects referenced (3)

  • Selection — Working with the Selection Object
  • Worksheet — Refer to All the Cells on the Worksheet
  • Worksheet — List of worksheet functions available to Visual Basic

Top excel Q&A (6)

+8 upvotes ranks this answer #106 out of 167 excel solutions on this site .