我正在使用OpenXml生成Excel工作簿,并且一直在关注http://msdn.microsoft.com/en-us/library/cc850837.aspx
上的示例如果我可以冻结顶部窗格,那将非常有用,但我找不到办法做到这一点。我意识到如果我使用http://closedxml.codeplex.com/,我可以这样做但是现在我想坚持使用OpenXml SDK
有什么想法吗?
答案 0 :(得分:9)
我试图解决同样的问题,最后打开Open XML SDK 2.0 Productivity Tool并使用Compare Files...
功能比较两个电子表格,一个是冻结的窗格,一个是没有。
当我这样做时,我被引导到基本上看起来像这样的代码:
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;
我将此添加到我的程序中,它似乎可以解决问题。
答案 1 :(得分:7)
您也可以添加选择:
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);
答案 2 :(得分:1)
当我使用其他答案中提供的代码时,我一直收到SheetViews的空错误。我使用SDK Productivity Tools查看带有冻结窗格的excel文档的代码,这有助于我创建下面的代码。我不得不使用GetFirstChild方法,而是创建SheetViews和SheetView类的新实例并附加它们。
这是代码。
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
SheetViews sheetViews = new SheetViews();
SheetView sheetView = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };
Pane pane = new Pane() { ActivePane = PaneValues.BottomLeft, State = PaneStateValues.Frozen, TopLeftCell = "A2", VerticalSplit = 1D };
Selection selection = new Selection() { Pane = PaneValues.BottomLeft };
sheetView.Append(pane);
sheetView.Append(selection);
sheetViews.Append(sheetView);
worksheetPart.Worksheet.Append(sheetViews);
另外需要注意的是,在创建SheetView时,必须包含TabSelected和WorkbookViewId值,否则在打开“我们发现某些内容存在问题......”文件时会收到错误。
此外,对于想要冻结第一列而不是第一行的人来说,这是一个例子。
var sheetViews = new SheetViews();
var sheetView = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };
var pane = new Pane() { ActivePane = PaneValues.TopRight, HorizontalSplit = 1D, State = PaneStateValues.Frozen, TopLeftCell = "B1" };
var selection = new Selection() { Pane = PaneValues.TopRight };
sheetView.Append(pane);
sheetView.Append(selection);
sheetViews.Append(sheetView);
答案 3 :(得分:0)