本文介绍了如何隐藏使用C#中的OpenXML电子表格列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用DocumentFormat.OpenXML用C#。如何隐藏特定的列结果
中的部分代码:
使用(SpreadsheetDocument sDocument = SpreadsheetDocument.Open(resultFileName,真))
{
WorkbookPart workbookPart = sDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.AddNewPart< WorksheetPart>();
VAR RELID = workbookPart.GetIdOfPart(worksheetPart);
GenerateWorksheetPartContent(worksheetPart,数据,templateSheet);
无功柱= worksheetPart.Worksheet.Descendants<列>(); < - 空
INT [] = colomnsToHide新INT [] {3,8,16,17,18};
的foreach(INT我colomnsToHide)
{
柱[I] .Hidden = TRUE;
}
}
templateSheet - SheetData从XLSX模板结果
数据 - 数据插入
任何建议
解决方案
? 列后裔仅添加到worksheetpart时,有一些自定义的列的行为,如大小,隐藏,或分组。你可以明确地添加这个列定义:
列列=新列();
//最小和放大器;马克斯参考1索引的列序号
柱栏3 =新列(){最小= 3,最大值= 3,宽度= 0,CustomWidth = TRUE,隐藏=真};
柱column8 =新列(){最小= 8,最大= 8,宽度= 0,CustomWidth = TRUE,隐藏=真};
// ...重复每个列
columns.Append(栏3);
columns.Append(column8);
worksheetPart.Append(列);
为你做这个要隐藏
每列
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?
解决方案
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
这篇关于如何隐藏使用C#中的OpenXML电子表格列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!