【问题标题】:Remove the x namespace using openxml使用 openxml 删除 x 命名空间
【发布时间】:2013-10-03 13:26:28
【问题描述】:

我发现了这个问题,我也遇到了同样的问题。

XLSX- how to get rid of the default namespace prefix x:?

生成 OpenXML Excel 工作表时遇到问题。特别是样式表部分。

当样式表以某种方式获得时间线样式(仅限 Excel 2013)时,它能够生成 excel 文件。然后样式表包含正确的命名空间 (xmlns:x="...")。这使得它可以在 Excel 2010 中打开。但是,当使用 Excel 2010 保存生成的 Excel 文件时,x-namespace 将成为默认命名空间,并且会从所有元素中删除,时间轴扩展元素除外。在excel中重新打开文件时,这将导致错误。在 OpenXml 生产力工具中打开同一个文件时,会出现 x 是未知命名空间的错误。

所以这里有一些 xml-result 代码。

生成excel文件(stylesheet.xml)后首先:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<x:styleSheet xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" mc:Ignorable="x14ac">
  <x:fonts count="3" x14ac:knownFonts="1">
    <x:font>
      <x:sz val="11"/>
      <x:color theme="1"/>
      <x:name val="Calibri"/>
      <x:family val="2"/>
      <x:scheme val="minor"/>
    </x:font>
    <!-- more fonts -->
  </x:fonts>
  <x:fills count="7">
    <x:fill>
      <x:patternFill patternType="none"/>
    </x:fill>
    <!-- more fills -->
  </x:fills>
  <x:borders count="1">
    <x:border>
      <x:left/>
      <x:right/>
      <x:top/>
      <x:bottom/>
      <x:diagonal/>
    </x:border>
  </x:borders>
  <x:cellStyleXfs count="1">
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
  </x:cellStyleXfs>
  <x:cellXfs count="18">
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
    <x:xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" pivotButton="1"/>
    <x:xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyAlignment="1">
      <x:alignment horizontal="left"/>
    </x:xf>
    <!-- more cells -->
  </x:cellXfs>
  <x:cellStyles count="1">
    <x:cellStyle name="Standaard" xfId="0" builtinId="0"/>
  </x:cellStyles>
  <x:dxfs count="1">
    <x:dxf>
      <x:numFmt numFmtId="19" formatCode="m/d/yyyy"/>
    </x:dxf>
  </x:dxfs>
  <x:tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/>
  <x:extLst>
    <x:ext xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}">
      <x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>
    </x:ext>
    <x:ext xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" uri="{9260A510-F301-46a8-8635-F512D64BE5F5}">
      <x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>
    </x:ext>
  </x:extLst>
</x:styleSheet>

在 Excel 2010 中保存后

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" mc:Ignorable="x14ac">
  <fonts count="3" x14ac:knownFonts="1">
    <font>
      <sz val="11"/>
      <color theme="1"/>
      <name val="Calibri"/>
      <family val="2"/>
      <scheme val="minor"/>
    </font>
    <!-- more fonts -->
  <fills count="7">
    <fill>
      <patternFill patternType="none"/>
    </fill>
    <!-- more fills -->
  </fills>
  <borders count="1">
    <border>
      <left/>
      <right/>
      <top/>
      <bottom/>
      <diagonal/>
    </border>
  </borders>
  <cellStyleXfs count="1">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0"/>
  </cellStyleXfs>
  <cellXfs count="18">
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>
    <xf numFmtId="14" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" pivotButton="1"/>
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyAlignment="1">
      <alignment horizontal="left"/>
    </xf>
    <!-- more cells -->
  </cellXfs>
  <cellStyles count="1">
    <cellStyle name="Standaard" xfId="0" builtinId="0"/>
  </cellStyles>
  <dxfs count="1">
    <dxf>
      <numFmt numFmtId="164" formatCode="m/d/yyyy"/>
    </dxf>
  </dxfs>
  <tableStyles count="0" defaultTableStyle="TableStyleMedium2" defaultPivotStyle="PivotStyleLight16"/>
  <extLst>
    <ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">
      <x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/>
    </ext>
    <x:ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main">
      <x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/>
    </x:ext>
  </extLst>
</styleSheet>

如您所见。 &lt;x:ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" 部分仍然包含从其他元素中删除的 x 命名空间。

有人知道如何解决这个问题吗?我的第一个想法是让 x 命名空间成为默认命名空间,这样它就不再需要样式表中的 x: 了。但我无法找到一种方法来告诉 openXML SDK 这样做。

【问题讨论】:

    标签: c# excel xml openxml-sdk


    【解决方案1】:

    我知道这是一个老问题,但以防万一其他人有同样的问题,我所做的就是删除冲突的部分。虽然不是最佳解决方案,但它允许用户在 Excel 2010 中打开文件,用另一个名称重新保存,然后重新打开刚刚创建的文件,没有问题。

    要删除我使用此代码的部分:

            if (Document.WorkbookPart.WorkbookStylesPart.Stylesheet.Descendants<StylesheetExtensionList>().Any())
            {
                Document.WorkbookPart.WorkbookStylesPart.Stylesheet.RemoveAllChildren<StylesheetExtensionList>();
            }
    

    我不使用Document.WorkbookPart.WorkbookStylesPart.Stylesheet.Save(),因为我将更改保存在其他地方,但您可能需要保存它,这取决于您的需要。

    我选择删除所有孩子,但也许您只能删除导致问题的孩子。

    希望对您有所帮助;)

    【讨论】:

    • 非常感谢您抽出宝贵时间提供答案。我被困在这个确切的问题上。删除有问题的部分是我计划尝试的可能修复方法之一。我很好奇:您是否尝试将 SpreadsheetML 命名空间从“x”前缀移动为默认命名空间?它会更加劳动密集(将所有元素的前缀更改为 nil),但我想知道它是否也可以工作。
    • @ZachBlocker 我没有尝试移动前缀,因为我相信和你一样,它似乎更耗时,所以我选择了我发布的更“可读”的代码,但它应该也可以工作。
    猜你喜欢
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2019-03-22
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多