Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeze Panes in OpenXml SDK 2.0 for Excel document

Tags:

c#

excel

openxml

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?

like image 250
Adam Avatar asked Jun 21 '11 16:06

Adam


People also ask

What is the Open XML SDK for office?

This content set provides documentation and guidance for the strongly-typed classes in the Open XML SDK 2.5 for Office. The SDK is built on the System.IO.Packaging API and provides strongly-typed classes to manipulate documents that adhere to the Office Open XML File Formats specification.

What is Office Open XML file format?

The Office Open XML File Formats specification is an open, international, ECMA-376, Second Edition and ISO/IEC 29500 standard. The Open XML file formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML.

How do I view the chartsheet in the Open XML SDK code?

When the Open XML SDK code is run, the following XML is written to the SpreadsheetML document referenced in the code. To view this XML, open the "sheet.xml" file in the "worksheets" folder of the .zip file. The following information from the ISO/IEC 29500 specification introduces the chartsheet (< chartsheet >) element.

What is open XML and why is it useful?

The Open XML file formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML. The Open XML SDK 2.5 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package.


2 Answers

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.

like image 100
Steve Avatar answered Sep 29 '22 18:09

Steve


You can add the Selection as well:

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

SheetViews sheetViews = wsp.Worksheet.GetFirstChild<SheetViews>();
SheetView sheetView = sheetViews.GetFirstChild<SheetView>();

Selection selection1 = new Selection() { Pane = PaneValues.BottomLeft };

Pane pane1 = new Pane() { VerticalSplit = 1D, TopLeftCell = "A2", ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen };

sheetView.Append(pane1);
sheetView.Append(selection1);
like image 31
Emily Avatar answered Sep 29 '22 17:09

Emily