IRow row = sheet.CreateRow(0); row.HeightInPoints = 20; var cellIndex = 0; foreach (var item in excelColumns) { sheet.SetColumnWidth(cellIndex, 15 * 256); ICell cell = row.CreateCell(cellIndex); cellIndex++; cell.SetCellValue(item.DisplayName); #region 设置样式 ICellStyle cellStyle = workbook.CreateCellStyle(); if (!item.AllowNull) { cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Red.Index; cellStyle.FillPattern = FillPattern.SolidForeground; } cellStyle.Alignment = HorizontalAlignment.Center; cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.WrapText = true; cellStyle.IsLocked = true; cell.CellStyle = cellStyle; #endregion row.Cells.Add(cell); }
变量:excelColumns为87个列,每个单元格应用水平和垂直居中的样式,个别列背景为红色,但输出的Excel文件到AQ列之后就不应用样式了,想问下NPOI创建单元格还有限制吗?还是我代码写的有问题?
IFont fontAll2 = workbook.CreateFont();
fontAll2.FontName = "Arial Black";
fontAll2.FontHeight = (short)(13.5 * 13.5);
ICellStyle ItemCellStyle1 = workbook.CreateCellStyle();
ItemCellStyle1.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
ItemCellStyle1.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
ItemCellStyle1.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
ItemCellStyle1.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
ItemCellStyle1.BottomBorderColor = HSSFColor.BLACK.index;
ItemCellStyle1.LeftBorderColor = HSSFColor.BLACK.index;
ItemCellStyle1.RightBorderColor = HSSFColor.BLACK.index;
ItemCellStyle1.TopBorderColor = HSSFColor.BLACK.index;
ItemCellStyle1.Alignment = HorizontalAlignment.CENTER;
ItemCellStyle1.VerticalAlignment = VerticalAlignment.CENTER;
ItemCellStyle1.WrapText = true;
ItemCellStyle1.SetFont(fontAll2);
你试试,可以不,我的是可以的
谢啦,现在的问题是为什么创建到AQ列之后样式什么的就应用不上了,之前的列是正常的,我比较郁闷。。。因为我的列比较多。
@Sandglass: 什么样式,背景么? 看有没有绑对应咯
因为格式的原因,这个导出的兼容类型的,在一些版本中打开会出现问题,后来改用2007格式创建就好啦。XSSFWorkbook使用这个对象