.Net 匯出到 Excel 利用 NPOI 變更字體尺寸及樣式 .Net 匯出到 Excel 利用 NPOI 變更字體尺寸及樣式
  .NET       ez      2012-05-10

利用 NPOI 變更字體尺寸及樣式:

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("工作表名稱");
HSSFRow Row = (HSSFRow)sheet.CreateRow(0);
Row.CreateCell(0).SetCellValue("測試文字");
HSSFCellStyle cs = (HSSFCellStyle)workbook.CreateCellStyle();
//啟動多行文字
cs.WrapText = true;
//文字置中
cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.CENTER;
cs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;
//框線樣式及顏色
cs.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOUBLE;
cs.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
cs.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.RightBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
cs.TopBorderColor = NPOI.HSSF.Util.HSSFColor.GREY_50_PERCENT.index;
//背景顏色
cs.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
cs.FillPattern = NPOI.SS.UserModel.FillPatternType.SOLID_FOREGROUND;

HSSFFont font1 = (HSSFFont)workbook.CreateFont();
//字體顏色
font1.Color = NPOI.HSSF.Util.HSSFColor.DARK_BLUE.index;
//字體粗體
font1.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
//字體尺寸
font1.FontHeightInPoints = 25;

cs.SetFont(font1);
Row.GetCell(0).CellStyle = cs;

//欄位寬度
sheet.SetColumnWidth(column.Ordinal, 5000);

標籤:   .NET

我要留言