如何使用 C# 隐藏 OpenXML 电子表格中的列?
我将 DocumentFormat.OpenXML 与 C# 一起使用。如何隐藏特定列?
部分代码:
using (SpreadsheetDocument sDocument = SpreadsheetDocument.Open(resultFileName, true))
{
WorkbookPart workbookPart = sDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
var relId = workbookPart.GetIdOfPart(worksheetPart);
GenerateWorksheetPartContent(worksheetPart, data, templateSheet);
var columns = worksheetPart.Worksheet.Descendants<Column>(); <-- Empty
int[] colomnsToHide = new int[] { 3, 8, 16, 17, 18 };
foreach (int i in colomnsToHide)
{
columns[i].Hidden = true;
}
}
templateSheet - 来自 xlsx-template 的 SheetData。
data - 要插入的数据。
有什么建议吗?
I'm using DocumentFormat.OpenXML with C#. How do I hide specific columns?
Some part of the code:
using (SpreadsheetDocument sDocument = SpreadsheetDocument.Open(resultFileName, true))
{
WorkbookPart workbookPart = sDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
var relId = workbookPart.GetIdOfPart(worksheetPart);
GenerateWorksheetPartContent(worksheetPart, data, templateSheet);
var columns = worksheetPart.Worksheet.Descendants<Column>(); <-- Empty
int[] colomnsToHide = new int[] { 3, 8, 16, 17, 18 };
foreach (int i in colomnsToHide)
{
columns[i].Hidden = true;
}
}
templateSheet - SheetData from xlsx-template.
data - data to insert.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当存在某些自定义列行为(例如大小、隐藏或分组)时,列后代才会添加到工作表部分。您可以像这样显式添加列定义:
对要隐藏的每个列执行此操作
The Column descendant is only added to the worksheetpart when there is some custom column behavior, such as sizes, hiding, or grouping. You can explicitly add column definitions like this:
Do this for each column you wish to hide