2014-05-06 83 views
2

我想從頭開始使用OpenXML創建一個Excel電子表格,我已經得到了一切正常工作(將實際值轉儲到實際單元格中),但現在我試圖將數字格式應用於列我遇到了一個問題。我有styles.xml,看起來像這樣:在OpenXML中應用數字格式

<?xml version="1.0" encoding="utf-8"?> 
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> 
<x:numFmts count="12"> 
    <x:numFmt numFmtId="216" formatCode="#,###" /> 
    <x:numFmt numFmtId="217" formatCode="$#,###" /> 
    <x:numFmt numFmtId="218" formatCode="#0.00" /> 
    <x:numFmt numFmtId="219" formatCode="#,###" /> 
    <x:numFmt numFmtId="220" formatCode="#0.0%" /> 
    <x:numFmt numFmtId="221" formatCode="#,###" /> 
    <x:numFmt numFmtId="222" formatCode="#0.0%" /> 
    <x:numFmt numFmtId="223" formatCode="#0.0%" /> 
    <x:numFmt numFmtId="224" formatCode="#0.0%" /> 
    <x:numFmt numFmtId="225" formatCode="#,###" /> 
    <x:numFmt numFmtId="226" formatCode="#,###" /> 
    <x:numFmt numFmtId="227" formatCode="#0.0%" /> 
</x:numFmts> 
<x:cellXfs count="12"> 
    <x:xf numFmtId="216" applyNumberFormat="1" /> 
    <x:xf numFmtId="217" applyNumberFormat="1" /> 
    <x:xf numFmtId="218" applyNumberFormat="1" /> 
    <x:xf numFmtId="219" applyNumberFormat="1" /> 
    <x:xf numFmtId="220" applyNumberFormat="1" /> 
    <x:xf numFmtId="221" applyNumberFormat="1" /> 
    <x:xf numFmtId="222" applyNumberFormat="1" /> 
    <x:xf numFmtId="223" applyNumberFormat="1" /> 
    <x:xf numFmtId="224" applyNumberFormat="1" /> 
    <x:xf numFmtId="225" applyNumberFormat="1" /> 
    <x:xf numFmtId="226" applyNumberFormat="1" /> 
    <x:xf numFmtId="227" applyNumberFormat="1" /> 
</x:cellXfs> 
</x:styleSheet> 

但Excel中似乎並不喜歡它,並經過「修理」的文件中刪除。我在這裏錯過了什麼?這個文檔有點點正好需要什麼來保持Excel的快樂。

我手動分配numFmtId開始我認爲可能是一個適當的高數。這是做到這一點的正確方法嗎?

此外,我知道formatCode是重複的,但我認爲Excel不會被絆倒,如果有必要我可以合併它們。

我列的定義是這樣的(在sheet.xml):

<x:cols> 
    <x:col min="1" max="1" width="7" /> 
    <x:col min="2" max="2" width="58" /> 
    <x:col min="3" max="3" width="16" style="0" /> 
    <x:col min="4" max="4" width="6" style="1" /> 
    <x:col min="5" max="5" width="17" style="2" /> 
    <x:col min="6" max="6" width="16" style="3" /> 
    <x:col min="7" max="7" width="18" style="4" /> 
    <x:col min="8" max="8" width="17" style="5" /> 
    <x:col min="9" max="9" width="20" style="6" /> 
    <x:col min="10" max="10" width="21" style="7" /> 
    <x:col min="11" max="11" width="21" style="8" /> 
    <x:col min="12" max="12" width="16" style="9" /> 
    <x:col min="13" max="13" width="16" style="10" /> 
    <x:col min="14" max="14" width="19" style="11" /> 
</x:cols> 

爲了比較 - 在​​這裏是一個片段從用Excel本身創造了一個工作styles.xml文件:

<numFmts count="1"> 
    <numFmt numFmtId="164" formatCode="#,##0\p"/> // where does 164 come from? 
</numFmts> 

<cellXfs count="3"> 
    <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/> // do you always need a 0 xf entry? It isn't referenced in the sheet.xml file 
    <xf numFmtId="3" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>  // assuming numFmtId = 3 is a built in format?? 
    <xf numFmtId="164" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/> 
</cellXfs> 

回答

3

回答我的問題,但我碰到這個帖子:

http://polymathprogrammer.com/2009/11/09/how-to-create-stylesheet-in-excel-open-xml/

這表明最小樣式表需要的不僅僅是numFmtscellXfs。所以我適應他們的代碼產生最小的樣式表爲我準備好要插入我的手機的格式和數字格式(我是在一個循環做):

private Stylesheet CreateStylesheet() 
    { 
     Stylesheet ss = new Stylesheet(); 

     Fonts fts = new Fonts(); 
     DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font() 
     { 
      FontName = new FontName() 
      { 
       Val = "Calibri" 
      }, 
      FontSize = new FontSize() 
      { 
       Val = 11 
      } 
     }; 
     fts.Append(ft); 
     fts.Count = (uint)fts.ChildElements.Count; 

     Fills fills = new Fills(); 
     fills.Append(new Fill() 
     { 
      PatternFill = new PatternFill() 
      { 
       PatternType = PatternValues.None 
      } 
     }); 
     fills.Append(new Fill() 
     { 
      PatternFill = new PatternFill() 
      { 
       PatternType = PatternValues.Gray125 
      } 
     }); 
     fills.Count = (uint)fills.ChildElements.Count; 

     Borders borders = new Borders(); 
     Border border = new Border() 
     { 
      LeftBorder = new LeftBorder(), 
      RightBorder = new RightBorder(), 
      TopBorder = new TopBorder(), 
      BottomBorder = new BottomBorder(), 
      DiagonalBorder = new DiagonalBorder() 
     }; 
     borders.Append(border); 
     borders.Count = (uint)borders.ChildElements.Count; 

     CellStyleFormats csfs = new CellStyleFormats(); 
     CellFormat cf = new CellFormat() { 
      NumberFormatId = 0, 
      FontId = 0, 
      FillId = 0, 
      BorderId = 0 
     }; 

     csfs.Append(cf); 
     csfs.Count = (uint)csfs.ChildElements.Count; 

     NumberingFormats nfs = new NumberingFormats(); 
     CellFormats cfs = new CellFormats(); 
     cf = new CellFormat() 
     { 
      NumberFormatId = 0, 
      FontId = 0, 
      FillId = 0, 
      BorderId = 0, 
      FormatId = 0 
     }; 
     cfs.Append(cf); 

     ss.Append(nfs); 
     ss.Append(fts); 
     ss.Append(fills); 
     ss.Append(borders); 
     ss.Append(csfs); 
     ss.Append(cfs); 

     CellStyles css = new CellStyles(); 
     CellStyle cs = new CellStyle() 
     { 
      Name = "Normal", 
      FormatId = 0, 
      BuiltinId = 0 
     }; 
     css.Append(cs); 
     css.Count = (uint)css.ChildElements.Count; 
     ss.Append(css); 

     DifferentialFormats dfs = new DifferentialFormats(); 
     dfs.Count = 0; 
     ss.Append(dfs); 

     TableStyles tss = new TableStyles() 
     { 
      Count = 0, 
      DefaultTableStyle = "TableStyleMedium9", 
      DefaultPivotStyle = "PivotStyleLight16" 
     }; 
     ss.Append(tss); 

     return ss; 
    } 

不積極,有沒有東西有可能是下降了,但我沒有耐心通過反覆試驗來看它是否可以變得更苗條。

我想一個單元格的格式必須FontIdFillIdBorderIdFormatId除了NumberFormatId並以具有這些ID,您需要創建爲他們每個人至少一個條目。儘管XML Schema將它們標記爲「可選」。

相關問題