2013-10-03 84 views
4

我發現這個問題,我有點相同的問題。使用openxml刪除x命名空間

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

我產生EEN OpenXML的Excel工作表時,有一個問題。特別是樣式表部分。 當樣式表以某種方式獲得時間軸的樣式(僅適用於Excel 2013)時,它能夠生成一個Excel文件。樣式表然後包含正確的命名空間(xmlns:x =「...」)。這可以在Excel 2010中打開它。但是,當使用Excel 2010保存生成的Excel文件時,x命名空間將成爲默認命名空間,除了時間軸擴展元素外,它將從所有的ellement中刪除。當在Excel中重新打開文件時,這將導致錯誤。在OpenXml Productivity Tool中打開相同的文件時,會出現x是未知名稱空間的錯誤。

所以這裏有一些xml結果代碼。生成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> 

保存,你可以看到後。 <x:ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}"部分仍然包含將其從其他ellement中刪除的x命名空間。

任何人都知道如何解決這個問題?我的第一個想法是使x命名空間成爲默認命名空間,因此它不再需要樣式表中的x:。但是我無法找到一種方法來告訴openXML SDK這樣做。

回答

3

我知道這是一個老問題,但爲防萬一別人有同樣的問題,我所做的就是刪除衝突的部分。雖然不是最佳解決方案,但它允許用戶在Excel 2010中打開該文件,使用其他名稱保存它,然後重新打開剛剛創建的文件而不會出現問題。

要刪除我用這個代碼段:

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

我din't使用Document.WorkbookPart.WorkbookStylesPart.Stylesheet.Save()因爲我保存在另一個地方的變化,但你可能需要保存它,這取決於你的需求。

我選擇刪除所有的孩子,但也許你只能刪除導致問題的那個孩子。

希望對你有所幫助;)

+0

非常感謝您花時間提供答案。我被困在這個確切的問題上。刪除違規部分是我計劃嘗試的可能修復之一。我很好奇:您是否嘗試將SpreadsheetML名稱空間從「x」前綴移動爲默認名稱空間?這將會更加勞動密集(將所有元素的前綴更改爲零),但我想知道它是否也能起作用。 –

+0

@ZachBlocker我沒有嘗試移動前綴,因爲我相信和你一樣,它似乎更費時間,所以我選擇了我發佈的更「可讀」的代碼,但它也應該工作。 – emmanuel