POI操作EXCEL的问题,添加批注,设置背景色不成功,请指教。

Think Nothing 2011-01-11 03:14:03
读取一个现有的excel文件,对每行数据验证,在出错的地方加上批注,设置背景色,然后保存成另外一个文件,代码如下:
String fileName = "resource.xls";

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
"D:\\upload\\"
+ fileName));
HSSFWorkbook resourceFile = new HSSFWorkbook(fs);
// TODO 验证处理

// 单位工作表
HSSFSheet shOrg = resourceFile.getSheet("Sheet1");
if (shOrg == null) {
return;
}
int lastRowNum = shOrg.getLastRowNum();// 取得最后一行
HSSFRow curRow = null;
HSSFCell curCell = null;
HSSFComment comment = null;
HSSFCellStyle cellStyle = null;
// 创建HSSFPatriarch对象,HSSFPatriarch是所有注释的容器.
HSSFPatriarch patr = shOrg.createDrawingPatriarch();
for (int rowNum = 1; rowNum <= lastRowNum; rowNum++) {
curRow = shOrg.getRow(rowNum);
// 单位名称
curCell = curRow.getCell(0);
// 添加单元格注释
// 定义注释的大小和位置,详见文档
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
(short) 3, 3, (short) 5, 6);

comment = patr.createCellComment(anchor);
// 设置注释内容
comment.setString(new HSSFRichTextString(" 单位名称不正确! "));
comment.setAuthor("test");
curCell.setCellComment(comment);

// 设置单元格背景
cellStyle = resourceFile.createCellStyle();
cellStyle.setFillBackgroundColor(HSSFColor.DARK_RED.index);
curCell.setCellStyle(cellStyle);

}

// 保存文件
String outputFileName = "error_resource.xls";
FileOutputStream stream = new FileOutputStream(new File(
"D:\\upload\\"
+ outputFileName));
resourceFile.write(stream);
stream.close();
...全文
6548 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
是一格啊 2013-04-11
  • 打赏
  • 举报
回复
我也遇到了这个问题,少了句cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);谢谢楼主和帮楼主解决问题的人
钱成军 2013-01-14
  • 打赏
  • 举报
回复
说的好好好啊
xli_feel 2011-12-16
  • 打赏
  • 举报
回复
haixiang710厉害..解决了我的问题
ID967511934 2011-09-13
  • 打赏
  • 举报
回复
cellStyle.setFillForegroundColor((short) 13);// 设置背景色

cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
ID967511934 2011-09-13
  • 打赏
  • 举报
回复
cellStyle.setFillForegroundColor((short) 13);// 设置背景色

cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
Think Nothing 2011-01-11
  • 打赏
  • 举报
回复
非常感谢二位,问题已经解决。
magong 2011-01-11
  • 打赏
  • 举报
回复
OK,参照4楼的内容,LZ的程序需要修改的地方是:

// 设置单元格背景
cellStyle = resourceFile.createCellStyle();
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.DARK_RED.index);
curCell.setCellStyle(cellStyle);

这Excel前景色和后景色还真是有点别扭。

还有一点需要注意的是程序自己读出来的行数有可能超,这样可能会在某些行上报NPE错。
haixiang710 2011-01-11
  • 打赏
  • 举报
回复
至于颜色
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置前景填充样式
cellStyle.setFillForegroundColor(HSSFColor.DARK_RED.index);//前景填充色

关于颜色的部分说明:

HSSFWorkbook wb = new HSSFWorkbook();
...
HSSFCellStyle style = wb.createCellStyle();
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.WHITE.index);
cell.setCellStyle(style); //cell 是 HSSFCell 对象
setFillPattern是设置单元格填充样式,SOLID_FOREGROUND纯色使用前景颜色填充,接着设置前景颜色(setFillForegroundColor)就可以给单元格着色了。setFillForegroundColor()方法的参数是一个short类型,POI使用索引来代表颜色,默认已经有一些颜色了,如:
8: BLACK
60: BROWN
59: OLIVE_GREEN
58: DARK_GREEN
...
颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字。

二、接下来,使用自定义颜色
如果不使用POI提供的默认颜色,就需要自定颜色索引:
HSSFPalette palette = wb.getCustomPalette(); //wb HSSFWorkbook对象
palette.setColorAtIndex((short) 9, (byte) (color.getRed()), (byte) (color.getGreen()), (byte) (color.getBlue()));

/*设置颜色的索引只能是 8 ~ 64,在此之外的索引无效,也不会报错。以下三种方式都可以设置成功。
palette.setColorAtIndex((short)9, (byte) (0xff & 251), (byte) (0xff & 161), (byte) (0xff & 161));
palette.setColorAtIndex((short)10, (byte) (0x66), (byte) (0xcd), (byte) (0xaa));
palette.setColorAtIndex((short)11, (byte) (255), (byte) (165), (byte) (0));
*/
然后使用颜色,如上例,可以用新的颜色索引,替换原有的颜色:
style.setFillForegroundColor((short) 9);

三、setFillPattern(),设置单元格填充的样式,比如:
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
style.setFillForegroundColor(HSSFColor.RED.index);
style.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
这样当前单元格就被红蓝交替的格子填充


上面3行代码,去掉setFillPattern设置填充样式的一行,同时设置前景色和背景色,生成的文件没有填充颜色,此时既不会用前景色填充,也不会用背景色填充。这种情况与 setFillPattern(HSSFCellStyle.NO_FILL); 时一样。
api上setFillBackgroundColor方法说明有如下示例:


public void setFillBackgroundColor(short bg)set the background fill color.
For example:

cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
//上面代码经测试,是黑色点状的背景(无前景),设置红色背景色无效optionally a Foreground and background fill can be applied: Note: Ensure Foreground color is set prior to background
cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());or, for the special case of SOLID_FILL:
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
cs.setFillForegroundColor(new HSSFColor.RED().getIndex());
haixiang710 2011-01-11
  • 打赏
  • 举报
回复
为什么我用你的代码测试没问题呢..
可以生成批注...
不过大概由于版本问题,我的POI包没有你下面的方法:
comment = patr.createCellComment(anchor);
我的是comment=patr.createComment(anchor);
Think Nothing 2011-01-11
  • 打赏
  • 举报
回复
谢谢,测试了一下这段代码,可以生成,但是我的程序是从一个现有的文件读出来的
haixiang710 2011-01-11
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
//创建工作簿对象
HSSFWorkbook wb=new HSSFWorkbook();
//创建工作表对象
HSSFSheet sheet=wb.createSheet("我的工作表");
//创建绘图对象
HSSFPatriarch p=sheet.createDrawingPatriarch();
//创建单元格对象,批注插入到4行,1列,B5单元格
HSSFCell cell=sheet.createRow(4).createCell(1);
//插入单元格内容
cell.setCellValue(new HSSFRichTextString("批注"));
//获取批注对象
//(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2)
//前四个参数是坐标点,后四个参数是编辑和显示批注时的大小.
HSSFComment comment=p.createComment(new HSSFClientAnchor(0,0,0,0,(short)3,3,(short)5,6));
//输入批注信息
comment.setString(new HSSFRichTextString("插件批注成功!插件批注成功!"));
//添加作者,选中B5单元格,看状态栏
comment.setAuthor("toad");
//将批注添加到单元格对象中
cell.setCellComment(comment);
//创建输出流
FileOutputStream out=new FileOutputStream("writerPostil.xls");

wb.write(out);
//关闭流对象
out.close();
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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