Created
August 7, 2018 09:09
-
-
Save donma/8935833cde9d33416ca57223490129f2 to your computer and use it in GitHub Desktop.
NPOI Font Color Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var workbook = new HSSFWorkbook(); | |
var sheetReportResult = workbook.CreateSheet("報表結果"); | |
//綠色的Style | |
NPOI.SS.UserModel.IFont fontG = workbook.CreateFont(); | |
fontG.Color = NPOI.SS.UserModel.IndexedColors.Green.Index; | |
var styleG = workbook.CreateCellStyle(); | |
styleG.SetFont(fontG); | |
//紅色的Style | |
NPOI.SS.UserModel.IFont fontR = workbook.CreateFont(); | |
fontR.Color = NPOI.SS.UserModel.IndexedColors.Red.Index; | |
var styleR = workbook.CreateCellStyle(); | |
styleR.SetFont(fontR); | |
//產生第一個要用CreateRow | |
sheetReportResult.CreateRow(0).CreateCell(0).SetCellValue("姓名"); | |
//之後的用GetRow 取得在CreateCell | |
sheetReportResult.GetRow(0).CreateCell(1).SetCellValue("電話"); | |
sheetReportResult.GetRow(0).CreateCell(2).SetCellValue("備註"); | |
//第一Row 已經被Title用掉了 所以從1開始 | |
for (var i = 1; i <= 20; i++) | |
{ | |
sheetReportResult.CreateRow(i).CreateCell(0).SetCellValue("用戶" + i); | |
if (i== 5) | |
{ | |
var cell = sheetReportResult.GetRow(i).CreateCell(1); | |
cell.CellStyle = styleR; | |
cell.SetCellValue("091234567" + i); | |
} | |
else if (i == 10) | |
{ | |
var cell = sheetReportResult.GetRow(i).CreateCell(1); | |
cell.CellStyle = styleG; | |
cell.SetCellValue("091234567" + i); | |
} | |
else { | |
var cell = sheetReportResult.GetRow(i).CreateCell(1); | |
cell.SetCellValue("091234567" + i); | |
} | |
sheetReportResult.GetRow(i).CreateCell(2).SetCellValue("我是備註" + i); | |
} | |
sheetReportResult.SetColumnWidth(0, 20 * 256); | |
sheetReportResult.SetColumnWidth(1, 40 * 256); | |
sheetReportResult.SetColumnWidth(2, 40 * 256); | |
var file = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "sample.xls", FileMode.Create); | |
workbook.Write(file); | |
file.Close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment