2014-04-14 35 views
1

正如您已閱讀標題,我一直在尋找一種方法來格式化電子表格格式的電子表格。使用下面的代碼我確實得到了我的貨幣格式,但貨幣符號顯示在數字後面。Office Open XML SDK電子表格如何在數字前面顯示貨幣符號?

 // currency format 
     stylesheet.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.CellFormats>().InsertAt<DocumentFormat.OpenXml.Spreadsheet.CellFormat>(
      new DocumentFormat.OpenXml.Spreadsheet.CellFormat() 
      { 
       NumberFormatId = 164, 
       ApplyNumberFormat = true 
      }, 
      8); 

有沒有人知道在前面顯示貨幣符號的貨幣格式NumberFormatId?

回答

2

事實證明,我需要爲我的電子表格添加基本樣式。下面的代碼添加了貨幣的基本樣式。

// Currency 
stylesheet.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.NumberingFormats>().InsertAt<DocumentFormat.OpenXml.Spreadsheet.NumberingFormat>(
    new DocumentFormat.OpenXml.Spreadsheet.NumberingFormat() 
    { 
    NumberFormatId = 164, 
    FormatCode = "\"" + System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencySymbol + "\"\\ " + "#,##0.00" 
    },0); 

感謝@Vadim的回覆。

-1

如何改變單元格的文字方向?爲我工作。

+0

是你的答案與問題相關的$ 1.23 ? –

0

我試圖改進上述建議,因爲我嘗試了同樣的做法,並沒有得到負值的$符號。我應用了以下更改並修正了帶有負值的$符號問題。

stylesheet.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.NumberingFormats>().InsertAt<DocumentFormat.OpenXml.Spreadsheet.NumberingFormat>(
    new DocumentFormat.OpenXml.Spreadsheet.NumberingFormat() 
    { 
    NumberFormatId = 164, 
    FormatCode = "\"" + System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencySymbol + "\"\\ " + "#,##0.00;" + "\"(" + System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrencySymbol + "\"\\ " + "#,##.00)[Red];0.00" 

    }, 0); 

輸出:爲正值負值($ 1.23){字體顏色讀)