简体   繁体   English

冻结 OpenXml 中的窗格 SDK 2.0 for Excel 文档

[英]Freeze Panes in OpenXml SDK 2.0 for Excel document

I'm generating an Excel workbook using OpenXml and have been following the examples at http://msdn.microsoft.com/en-us/library/cc850837.aspx我正在使用 OpenXml 生成一个 Excel 工作簿,并一直在关注 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我意识到如果我使用http://closedxml.codeplex.com/我可以做到这一点,但现在我想坚持使用 OpenXml SDK

Any ideas?有任何想法吗?

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.我试图解决同样的问题并最终打开 Open XML SDK 2.0 Productivity Tool 并使用Compare Files...功能来比较两个电子表格,一个有冻结窗格,一个没有。

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.我将它添加到我的程序中,它似乎可以解决问题。

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);

When I used the code provided in the other answers, I kept receiving a null error for the SheetViews.当我使用其他答案中提供的代码时,我一直收到 SheetView 的 null 错误。 I used the SDK Productivity Tools to view the code for an excel document with a frozen pane, which helped me create the below code.我使用 SDK 生产力工具查看带有冻结窗格的 excel 文档的代码,这帮助我创建了以下代码。 Instead of using the GetFirstChild method, I had to create new instances of the SheetViews and SheetView classes and append them.我没有使用 GetFirstChild 方法,而是必须创建 SheetViews 和 SheetView 类的新实例以及 append 它们。

Here is the code.这是代码。

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);

One extra note is that when creating the SheetView, you must include the TabSelected and WorkbookViewId values, otherwise you will receive an error when opening the file about "We found a problem with some content in...."一个额外的注意事项是,在创建 SheetView 时,您必须包括 TabSelected 和 WorkbookViewId 值,否则在打开文件时您将收到有关“我们发现...中的某些内容有问题”的错误。

Also, for anyone who wants to freeze the first column, instead of the first row, here is an example.此外,对于想要冻结第一列而不是第一行的任何人,这里有一个示例。

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);

Feedback from 03/02/2021: 2021 年 3 月 2 日的反馈:

You just have to add in your generation class the content of the Excel file, this:你只需要在你的生成 class 中添加 Excel 文件的内容,这个:

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

This line allows indeed, to freeze all the shutters of the upper line...这条线确实允许冻结上线的所有百叶窗......

Code of a spreadsheet without the fixed shutters:没有固定百叶窗的电子表格代码: 在此处输入图像描述

Code of a spreadsheet with fixed shutters:带有固定百叶窗的电子表格代码: 在此处输入图像描述

I share this feedback because I also searched for a while before finding out how to do it.我分享这个反馈是因为我也搜索了一段时间才知道如何去做。

MemoryStream documentStream = new ();
    SpreadsheetDocument document = SpreadsheetDocument.Create(documentStream, SpreadsheetDocumentType.Workbook);
    WorkbookPart workbookPart = document.AddWorkbookPart();
    workbookPart.Workbook = new Workbook();

    WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
    stylePart.Stylesheet = new ScrapBuyplanStyleSheet().GenerateStyleSheet();
    stylePart.Stylesheet.Save();
    
    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet();
    
    // Freeze Panes
    SheetViews sheetViews = new ();
    SheetView sheetView = new () {TabSelected = true, WorkbookViewId = (UInt32Value) 0U};
    Pane pane = new ()
    {
        ActivePane = PaneValues.TopRight,
        State = PaneStateValues.Frozen, 
        TopLeftCell = "B1", 
        //VerticalSplit = 1D,
        HorizontalSplit = 1D
    };
    Selection selection = new () {Pane = PaneValues.TopRight};
    sheetView.Append(pane);
    sheetView.Append(selection);
    sheetViews.Append(sheetView);
    worksheetPart.Worksheet.Append(sheetViews);

Solution to null pointer when creating pane is create the worksheet before adding the pane创建窗格时null指针的解决方案是在添加窗格之前创建工作表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM