Epplus可以用範圍宣告,改變範圍內所有儲存格的色彩外觀。
using (ExcelRange rng = ws.Cells["A1:E1"])
{
rng.Style.Font.Bold = true;
}
當然,大多數時候我們是從資料庫將資料以迴圈帶出,事先不知道筆數,因此擺在for 迴圈內逐筆指定:
sheet1.Cells[startRow, 5].Style.Font.Bold = true;
sheet1.Cells[startRow, 5].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
sheet1.Cells[startRow, 5].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Aqua);
C#寫久的開發者,常常會習慣.Style.Fill...嘗試方法一路找下去,找到SetColor,但光是這樣無法執行。還要加上Style.Fill.PatternType。否則在編譯時會報錯。
此外網路上的範例會寫Style.Fill.PatternType = ExcelFillStyle.Solid,但這樣IDE會顯示找不到,還是要補齊 OfficeOpenXml.Style.ExcelFillStyle.Solid 才能運作。
儲存格內文字變色的範例:(這是vb.net 的,語法類似)
If Dt1.Rows(rIndex)(colIndex).GetType.ToString() = "System.Int32" Then
If Dt1.Rows(rIndex)(colIndex) > 0 Then
sheet1.Cells(startRow, colIndex + 1).Style.Font.Color.SetColor(System.Drawing.Color.Blue)
End If
End If
沒有留言:
張貼留言