冻结 OpenXml SDK 2.0 for Excel 文档中的窗格
我正在使用 OpenXml 生成 Excel 工作簿,并遵循 http:// /msdn.microsoft.com/en-us/library/cc850837.aspx
如果我可以冻结顶部窗格,那将非常有用,但我找不到方法来做到这一点。我意识到如果我使用 http://linedxml.codeplex.com/ 就可以做到这一点,但现在我想要坚持使用 OpenXml SDK
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我试图解决同样的问题,最终打开 Open XML SDK 2.0 Productivity Tool 并使用“比较文件...”功能来比较两个电子表格,一个带有冻结窗格,另一个没有。
当我这样做时,我得到的代码基本上看起来像这样:
我将其添加到我的程序中,它似乎起到了作用。
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:
I added this to my program and it seemed to do the trick.
您也可以添加选择:
You can add the Selection as well:
当我使用其他答案中提供的代码时,我不断收到 SheetViews 的空错误。我使用 SDK 快捷会议工具查看带有冻结窗格的 Excel 文档的代码,这帮助我创建了以下代码。我必须创建 SheetViews 和 SheetView 类的新实例并附加它们,而不是使用 GetFirstChild 方法。
这是代码。
需要额外注意的是,在创建 SheetView 时,必须包含 TabSelected 和 WorkbookViewId 值,否则在打开文件时会收到一条错误消息,内容为“我们发现某些内容存在问题......”
此外,对于任何想要的人要冻结第一列而不是第一行,下面是一个示例。
When I used the code provided in the other answers, I kept receiving a null error for the SheetViews. 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. Instead of using the GetFirstChild method, I had to create new instances of the SheetViews and SheetView classes and append them.
Here is the code.
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...."
Also, for anyone who wants to freeze the first column, instead of the first row, here is an example.
来自 03/02/2021 的反馈:
您只需在生成类中添加 Excel 文件的内容,如下:
此行确实允许冻结上面一行的所有百叶窗...
没有固定百叶窗的电子表格的代码:
具有固定百叶窗的电子表格代码:
我分享此反馈是因为在找到如何操作之前我也搜索了一段时间。
Feedback from 03/02/2021 :
You just have to add in your generation class the content of the Excel file, this :
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.
创建窗格时出现空指针的解决方案是在添加窗格之前创建工作表
Solution to null pointer when creating pane is create the worksheet before adding the pane
大数据 OpenDocument Writer 示例
经过多次尝试和错误,并使用 这个示例 我发现 SheetViews 位于顶部的工作表内。在我的例子中,为了写入大量数据,我使用流式传输和写入器方法。我把它放在这里是因为这是谷歌为其他人找到我的地方。
冻结第一列示例
冻结第一行示例
使用 (var workbook = SpreadsheetDocument.Create(uniqueFilePath, SpreadsheetDocumentType.Workbook))
{
列表属性列表;
OpenXmlWriter 编写器;
工作簿.AddWorkbookPart();
var workSheetPart = workbook.WorkbookPart.AddNewPart();
writer = OpenXmlWriter.Create(workSheetPart);
/* */
writer.WriteStartElement(new Worksheet());
Large Data OpenDocument Writer Example
After Much Trial and Error, and using this Example I found the SheetViews Goes inside the Worksheet at the top. In My case to write large amounts of data I use a streaming and writer approach. I place this here because it's where Google landed me for others.
Freeze First Column Example
Freeze First Row Example
using (var workbook = SpreadsheetDocument.Create(uniqueFilePath, SpreadsheetDocumentType.Workbook))
{
List attributeList;
OpenXmlWriter writer;
workbook.AddWorkbookPart();
var workSheetPart = workbook.WorkbookPart.AddNewPart();
writer = OpenXmlWriter.Create(workSheetPart);
/* */
writer.WriteStartElement(new Worksheet());
我的情况:我需要更多的冻结行。下面我来解释两件事。
A8
是我冻结面板的位置,因此VerticalSplit = 7
是显示的“上方”行数。如果我放置VerticalSplit = 4
,则显示第 1-4 行,并隐藏第 5-7 行。如果我放置VerticalSplit > 7
然后抛出错误。ActiveCell = "A8"
是用户首次打开此生成的文件时将选择的单元格。关于PaneValues我没有太关注。
My case: I needed more frozen rows. Let me explain below two things.
A8
is where I froze panel, soVerticalSplit = 7
is the amount of lines "above" that are shown. If I placeVerticalSplit = 4
then lines 1-4 are shown and lines 5-7 are hidden. If I placeVerticalSplit > 7
then throws error.ActiveCell = "A8"
is what cell will be selected when user first open this generated file.Regarding
PaneValues
I didn't pay too much attention to it.