I'm using DocumentFormat.OpenXML with C#. How do I hide specific columns?
Some part of the code:
我用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 - SheetData from xlsx-template.
data - data to insert.
模板-来自xlsx模板的SheetData。数据——要插入的数据。
Any suggestions?
有什么建议吗?
1 个解决方案
#1
4
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:
只有在有一些自定义列行为(如大小、隐藏或分组)时,才将列后代添加到工作表部分。可以显式地添加列定义如下:
Columns columns = new Columns();
// Min & Max refer to the 1-indexed column ordinal
Column column3 = new Column(){ Min = 3, Max = 3, Width = 0, CustomWidth = true, Hidden = true };
Column column8 = new Column(){ Min = 8, Max = 8, Width = 0, CustomWidth = true, Hidden = true };
// ... repeat for each column
columns.Append(column3);
columns.Append(column8);
worksheetPart.Append(columns);
Do this for each column you wish to hide
对你想要隐藏的每一栏都这样做
#1
4
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:
只有在有一些自定义列行为(如大小、隐藏或分组)时,才将列后代添加到工作表部分。可以显式地添加列定义如下:
Columns columns = new Columns();
// Min & Max refer to the 1-indexed column ordinal
Column column3 = new Column(){ Min = 3, Max = 3, Width = 0, CustomWidth = true, Hidden = true };
Column column8 = new Column(){ Min = 8, Max = 8, Width = 0, CustomWidth = true, Hidden = true };
// ... repeat for each column
columns.Append(column3);
columns.Append(column8);
worksheetPart.Append(columns);
Do this for each column you wish to hide
对你想要隐藏的每一栏都这样做