求帮:NPOI设置Excel单元格背景及合并问题?

wh32967515 2014-03-03 08:58:55
有关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");

...全文
289 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wh32967515 2014-03-04
  • 打赏
  • 举报
回复
今天早上查资料,发现另一个有完整的demo,学习了。已搞定。 http://www.cnblogs.com/amour/archive/2013/01/11/2856166.html 分还是给到大家,谢谢楼上两位。
水目 2014-03-04
  • 打赏
  • 举报
回复
第一个不清楚, 第二个看下边

 CellStyle.FillPattern = FillPatternType.SQUARES;
                    CellStyle.FillForegroundColor = CellStyle.FillBackgroundColor = HSSFColor.YELLOW.index;
本拉灯 2014-03-04
  • 打赏
  • 举报
回复
是这个方法 _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;
wh32967515 2014-03-04
  • 打赏
  • 举报
回复
没有回啊,

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧