日期:2014-05-16  浏览次数:21128 次

求帮:NPOI设置Excel单元格背景及合并问题?
本帖最后由 wh32967515 于 2014-03-03 08:59:59 编辑
有关NPOI导出Excel的两个小问题,谢谢!
1) 单元格合并的类:CellRangeAddress,在哪个包中,怎么始终找不到?
已导入的包如下:
using NPOI.POIFS.FileSystem;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.Util.IO;
using NPOI.Util.Collections;
using NPOI.DDF;
using NPOI.HPSF;
using NPOI.HPSF.Extractor;
using NPOI.HPSF.Wellknown;
using NPOI.HSSF.Util;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Model;


2) 通过以下方法设置了单元格背景(代码正常执行,未报错),但导出的Excel中却无效果,是何原因?


ICellStyle style = workbook.CreateCellStyle();
//设置单元格的样式:水平对齐居中,有边框
style.Alignment = HorizontalAlignment.CENTER;
style.VerticalAlignment = VerticalAlignment.TOP;
style.BorderBottom = CellBorderType.THIN;
style.BorderLeft = CellBorderType.THIN;
style.BorderRight = CellBorderType.THIN;
style.BorderTop = CellBorderType.THIN;
cell = row.CreateCell(0, CellType.STRING);

style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.RED.index; //指定背景颜色

//将新的样式赋给单元格
cell.CellStyle = style;

//填值
cell.SetCellValue("ABCDE");


------解决方案--------------------
是这个方法  _currentSheet.AddMergedRegion(new CellRangeAddress(BeginX, EndX, BeginY, EndY));


第二个
   _currentSheet.GetRow(row).GetCell(column).CellStyle.FillForegroundColor = colIndex;
            _currentSheet.GetRow(row).GetCell(column).CellStyle.FillBackgroundColor = colIndex;
            _currentSheet.GetRow(row).GetCell(column).CellStyle.FillPattern = FillPatternType.BIG_SPOTS;
------解决方案--------------------
第一个不清楚,
第二个看下边

 CellStyle.FillPattern = FillPatternType.SQUARES;
                    CellStyle.FillForegroundColor = CellStyle.FillBackgroundColor = HSSFColor.YELLOW.index;