如何使用 C# 隐藏 OpenXML 电子表格中的列?

发布于 2024-12-11 02:32:48 字数 800 浏览 0 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-12-18 02:32:48

仅当存在某些自定义列行为(例如大小、隐藏或分组)时,列后代才会添加到工作表部分。您可以像这样显式添加列定义:

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);

对要隐藏的每个列执行此操作

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文