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

时间:2017-05-10 16:38:31

标签: openxml openxml-sdk

我正在尝试使用开放式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,但这仅适用于文件,如果由于某种原因我更改了列大小,这些列的原始大小不再有效。< / p>

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

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

<?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>

此部分是导致问题的部分

1 个答案:

答案 0 :(得分:0)

如果列已存在,请循环遍历它们以设置/创建hidden属性:

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