我将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模板的SheetData。
data—要插入的数据。
有什么建议吗?
最佳答案
只有当存在某些自定义列行为(如大小、隐藏或分组)时,才会将列子体添加到工作表部件中。可以显式添加列定义,如下所示:
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);
对要隐藏的每个列执行此操作