隐藏 .xlsx 文件中具有不同大小的列

Hide a columns on a .xlsx file that have different sizes

我正在尝试使用 open XML 隐藏 .asp 页面中已存在的 .xlsx 文件中的列,我发现此代码可用于隐藏列

    public void HideColumn(string FileName, UInt32Value columnNumber)
    {
      using (SpreadsheetDocument document = SpreadsheetDocument.Open(FileName, true))
      {
        IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == "Hoja1");
        if (sheets.Count() == 0)
        {
          // The specified worksheet does not exist.
          return;
        }

        WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
        Worksheet worksheet = worksheetPart.Worksheet;
        Columns columns1 = GenerateColumns(columnNumber);
        // The column element is behind the SheetFormatProperties element.
        worksheet.InsertAfter(columns1, worksheet.SheetFormatProperties);
        worksheet.Save();
      }
    }

    // Creates an Columns instance and adds its children.
    public Columns GenerateColumns(UInt32Value ColumnIndex)
    {
      Columns columns1 = new Columns();
      Column column1 = new Column() { Min = ColumnIndex, Max = ColumnIndex, Width = 0D, Hidden = true, CustomWidth = true };
      columns1.Append(column1);
      return columns1;
    }

我只是调用 HideColumn(sFile, 1) 发送文件名和列 ID,但这只适用于列具有原始大小的文件,如果由于某种原因我更改了列大小,这不再起作用.

如何在列大小不同的 .xlsx 文件中隐藏列?

没有大小的列的 .xlsx 文件看起来像这样

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet
    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac"
    xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
    <dimension ref="A1:E1"/>
    <sheetViews>
        <sheetView tabSelected="1" workbookViewId="0">
            <selection activeCell="A2" sqref="A2"/>
        </sheetView>
    </sheetViews>
    <sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0.25"/>
    <sheetData>
        <row r="1" spans="1:5" x14ac:dyDescent="0.25">
            <c r="A1">
                <v>1</v>
            </c>
            <c r="B1">
                <v>2</v>
            </c>
            <c r="C1">
                <v>3</v>
            </c>
            <c r="D1">
                <v>4</v>
            </c>
            <c r="E1">
                <v>5</v>
            </c>
        </row>
    </sheetData>
    <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>

包含不同大小列的 .xlsx 文件如下所示

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet
    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac"
    xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
    <dimension ref="A1:E1"/>
    <sheetViews>
        <sheetView tabSelected="1" workbookViewId="0">
            <selection activeCell="E4" sqref="E4"/>
        </sheetView>
    </sheetViews>
    <sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0.25"/>
    <cols>
        <col min="1" max="1" width="17.5703125" customWidth="1"/>
        <col min="2" max="2" width="20" customWidth="1"/>
        <col min="3" max="3" width="20.42578125" customWidth="1"/>
        <col min="4" max="4" width="19" customWidth="1"/>
        <col min="5" max="5" width="18" customWidth="1"/>
    </cols>
    <sheetData>
        <row r="1" spans="1:5" x14ac:dyDescent="0.25">
            <c r="A1">
                <v>1</v>
            </c>
            <c r="B1">
                <v>2</v>
            </c>
            <c r="C1">
                <v>3</v>
            </c>
            <c r="D1">
                <v>4</v>
            </c>
            <c r="E1">
                <v>5</v>
            </c>
        </row>
    </sheetData>
    <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>

我认为问题在于第二种类型的文件上有此部分

<cols>
    <col min="1" max="1" width="17.5703125" customWidth="1"/>
    <col min="2" max="2" width="20" customWidth="1"/>
    <col min="3" max="3" width="20.42578125" customWidth="1"/>
    <col min="4" max="4" width="19" customWidth="1"/>
    <col min="5" max="5" width="18" customWidth="1"/>
</cols>

这部分是导致问题的部分

如果列已经存在,循环遍历它们到 set/create hidden 属性:

foreach (Column column in worksheet.Descendants<Column>())
{
    // TODO: filter so that you don't hide every column.
    column.SetAttribute(new OpenXmlAttribute("hidden", "1"));
}