首页 新闻 会员 周边

NPOI创建单元格并应用简单样式问题

0
悬赏园豆:50 [已关闭问题] 关闭于 2015-12-08 15:13
                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创建单元格还有限制吗?还是我代码写的有问题?

Sandglass的主页 Sandglass | 初学一级 | 园豆:52
提问于:2015-12-08 11:27
< >
分享
所有回答(2)
0

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);

你试试,可以不,我的是可以的

搁忆 | 园豆:612 (小虾三级) | 2015-12-08 12:10

谢啦,现在的问题是为什么创建到AQ列之后样式什么的就应用不上了,之前的列是正常的,我比较郁闷。。。因为我的列比较多。

支持(0) 反对(0) Sandglass | 园豆:52 (初学一级) | 2015-12-08 12:13

@Sandglass: 什么样式,背景么? 看有没有绑对应咯

支持(0) 反对(0) 搁忆 | 园豆:612 (小虾三级) | 2015-12-08 12:16
0

因为格式的原因,这个导出的兼容类型的,在一些版本中打开会出现问题,后来改用2007格式创建就好啦。XSSFWorkbook使用这个对象

Sandglass | 园豆:52 (初学一级) | 2015-12-08 15:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册