2016-12-14 33 views
1

Ii已應用NPOI生成excel。到目前爲止,除了我想要大膽行之外,這是非常好的。C#NPOI rowStyle

我曾嘗試:

tmpRow.RowStyle = workbook.CreateCellStyle(); 
tmpRow.RowStyle.SetFont(boldFont); 

然而,一切都沒有改變。

雖然我可以通過設置一個一個做到這一點:

ICellStyle boldFontCellStyle = workbook.CreateCellStyle(); 
IFont boldFont = workbook.CreateFont(); 
boldFont.IsBold = true; 
boldFontCellStyle.SetFont(boldFont); 
for (int p= 0; p <= 12; p++) 
{ 
    tmpRow.GetCell(p).CellStyle = boldFontCellStyle; 
} 
//tmpRow.RowStyle = workbook.CreateCellStyle(); 
//tmpRow.RowStyle.SetFont(boldFont); 

我想知道是否有任何的方式來設置整個行風格NPOI

謝謝。

+0

如圖所示(這裏)應該可以使用您使用的屬性, (https://github.com/tonyqus/npoi/blob/02f080d3ee37e4f04a999be32604b1cb6bf3e649/examples/xssf/SetRowStyle/Program.cs)。 我在更新現有工作簿時遇到同樣的問題,該工作簿通過調用sheet.CreateRow(n)而不是GetRow(n)來修復,該工具似乎具有覆蓋行樣式的單個單元格樣式。 – MetalMichael

回答

0

生成樣式,設置它,然後將其應用於行。事情是這樣的:

var rowStyle = workbook.CreateCellStyle(); 
rowStyle.SetFont(boldFont); 

tmpRow.RowStyle = rowstyle; 
2

嘗試此行的造型

XSSFFont defaultFont = (XSSFFont)workbook.CreateFont(); 
defaultFont.FontHeightInPoints = (short)10; 
defaultFont.FontName = "Arial"; 
defaultFont.Color = IndexedColors.Black.Index; 
defaultFont.IsBold = true; 

XSSFCellStyle yourCellStyle = (XSSFCellStyle)workbook.CreateCellStyle(); 
yourCellStyle.SetFont(defaultFont); 

var row = sheet.CreateRow(0); 
row.RowStyle = yourCellStyle; 

下面一個是爲單元格樣式

XSSFFont yourFont = (XSSFFont)workbook.CreateFont(); 
    yourFont.FontHeightInPoints = (short)12; 
    yourFont.FontName = "Arial";    
    yourFont.IsBold = true; 
    yourFont.IsItalic = false; 
    yourFont.Boldweight = 700; 
    XSSFCellStyle yourStyle = (XSSFCellStyle)workbook.CreateCellStyle(); 
    yourStyle.SetFont(yourFont); 
    for (int p= 0; p <= 12; p++) 
     { 
     row.Cells[p].CellStyle = yourStyle; 
     } 
+0

你應對我的代碼有什麼意義:S – SKLTFZ

+0

爲了方便起見,我只是故意複製了「for」循環部分。在上面我已經提供了我的可行代碼。如果你喜歡,你可以試試。 –

+0

重點是,如果我不介意逐個添加cellstyle,我的代碼也可以工作,我正在尋找類似於行樣式的東西,而不是cellStyle :) – SKLTFZ