//设置单元格
public ICellStyle SetStyle(ref IWorkbook workbook,short background,short color)
{
ICellStyle style = workbook.CreateCellStyle();
{
style.Alignment = HorizontalAlignment.Center;//水平居中
style.VerticalAlignment = VerticalAlignment.Center;//垂直居中
IFont font = workbook.CreateFont();//文字设置
font.FontName = "微软雅黑";
font.Boldweight = short.MaxValue;//加粗
font.FontHeightInPoints = 11;//文字大小
if (color > 0)//文字颜色
font.Color = color;
else
font.Color = NPOI.HSSF.Util.HSSFColor.Black.Index;
style.SetFont(font);
if (background>0)//背景色
style.FillForegroundColor = background;
else
style.FillForegroundColor = short.Parse(GetRandomString());
#region 边框设置
style.FillPattern = FillPattern.SolidForeground;
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.BorderTop = BorderStyle.Thin;
style.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style.RightBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
style.TopBorderColor = NPOI.HSSF.Util.HSSFColor.Black.Index;
#endregion
style.WrapText = true;//文字换行
}
return style;
}
///设置表格宽高、单元格部分代码
{
ICellStyle style = SetStyle(ref workbook, 44, 0);
ISheet sheet = workbook.CreateSheet();
ICellStyle borderStyle = workbook.CreateCellStyle();
sheet.SetColumnWidth(0, 8 * 256);//设置列宽
sheet.SetColumnWidth(1, 30 * 256);
sheet.SetColumnWidth(2, 8 * 256);
workbook.SetSheetName(sheetIndex, name + "日报");
IRow ir1 = sheet.CreateRow(0);
ir1.Height = 400;//设置单元格高度
ICell cel0 = ir1.CreateCell(0);
cel0.SetCellValue("");
cel0.CellStyle = style;//设置单元格
}