MDA/MDD/TDD/DDD/DDDDDDD
posts - 536, comments - 111, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

poi笔记

Posted on 2008-03-14 22:26 leekiang 阅读(4136) 评论(0)  编辑  收藏 所属分类: 文件处理
注意用poi解析器从word中抽取文本后,输入流会关闭,原因如下:
InputStreams passed to POIFSFileSystem are now automatically closed. A warning is generated for people who might've relied on them not being closed before, and a wrapper to restore the old behaviour is supplied(POI-DEVELOPERS)
据说在POI3.0.3会修正该问题。

1,poi格子里的文本如何换行
  加"\n",同时设置style.setWrapText(true);
 
2,style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
 设置前景色,有半透明的效果
 
3,sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));// 起始cell行、起始cell列、结束cell行、结束cell列。
   被合并了的单元格不需要生成。
   单元格合并後设置边框只在原第一个上有效,如果想应用的合并後的整体,则需要一个个的Create出单元格并应用样式,这个明显是一个不太方便的操作,期待POI下一版的改进了
   注意行是y轴,列是x轴。
   poi-contrib下的HSSFRegionUtil可以设置合并区域的边框的颜色和样式
例如:HSSFRegionUtil.setLeftBorderColor(HSSFColor.BLACK.index,row,sheet,wb);

4,sheet.createFreezePane(2, 1);
  冻结窗口,左上角为行参和列参的起始处,从0开始
 
5,列宽sheet.setColumnWidth((short) 0, (short) (35.7 * 110));//第一个参数为列的下标,110为想设置的宽度像素。
  http://www.cnblogs.com/interboy/archive/2007/08/27/872028.html

6,行高row.setHeight((short) (15.625 * 100));//100为要设置的行高的像素
   row.setHeightInPoints((short)100);//应该可以达到同样的效果

7, POI將註解的部分,視做一個文字方塊,我們可以設定它的大小及內容,然後將之指定給某特定的Cell。
节录部分Code如下:
HSSFPatriarch patr = sheet.createDrawingPatriarch();
HSSFComment comment = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 8, rowCount));
HSSFRichTextString str = new HSSFRichTextString("TEST");
comment.setString(str);
comment.setAuthor("TEST");
cell.setCellComment(comment);

8,sheet的中文名
workbook.setSheetName(0,"统计",(short)1);//第三个参数为编码方式,HSSFWorkbook.ENCODING_UTF_16=1
sheet的名称不允许出现重复。

9, 先定义一个基本样式,再复制样式的属性。不知何故不能clonebean
  HSSFCellStyle headerStyle = workbook.createCellStyle();
  PropertyUtils.copyProperties(headerStyle, baseStyle);//

10, 输出,中文名
        response.reset();
        response.setContentType("applicationnd.ms-excel");
        response.addHeader("Content-disposition",
                "attachment;filename="+new String("统计".getBytes("GBK"), "ISO-8859-1")+".xls");
        ServletOutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();

11,cell.setCellValue(new HSSFRichTextString("测试"));
在poi2.5.1里不管用,只能用cell.setCellValue("测试");
忘了二者有何区别

12,直接用workbook.getBytes()得到的文档内容的结构是有问题的,会导致打开xls时报错。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workBook.write(bos);
//ByteArrayInputStream is = new ByteArrayInputStream(workBook.getBytes());//错误
ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());//正确
详见 http://numenzq.javaeye.com/blog/218816

13,POI的老版本的下载地址:http://archive.apache.org/dist/jakarta/poi/release/bin

14,每个sheet有65536的最大行数限制?
   数据量非常大时,拆分为多个sheet(每个1万行)可显著降低内存占用。
   http://www.javaeye.com/topic/74835?page=3

15,可用PropertyUtils.copyProperties(newStyle, baseStyle)把baseStyle的属性拷贝到newStyle中,但Font等拷贝不了。(浅拷贝,深拷贝?)

16,POI如何复制行,设置分页符bug? 使用POI操作Excel的几点注意事项
和http://www.blogjava.net/liaojiyong/archive/2007/11/14/160588.html

17,复制行,复制sheet
http://zhidao.baidu.com/question/66868683.html
http://www.aub.org.cn/J2SE/Article137612.html
http://zhidao.baidu.com/question/42447398.html

18,用HSSFCell的getCellType()只能判断三种类型,返回值为int   0,1,2  
  numeric(0),   formula(2)   or   string(1)  
  但是仅仅依靠这个有时候不能完全解决问题,  
  有时候需要用HSSFDataFormat来共同判断:  
    cell.getCellStyle().getDataFormat()
见http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
  https://svn.apache.org/repos/asf/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java


19,poi3.2把以下属性或方法删掉了:
HSSFWorkbook.ENCODING_UTF_16
cell.setEncoding
所以还是继续用poi3.0.2吧
        
http://blog.csdn.net/impeller/archive/2007/10/29/1855368.aspx
解析Excel注释,填充并生成报表的一个例子
http://hi.baidu.com/bluewhale84/blog/item/b9ebd5c295911130e4dd3bd0.html
http://kevintuntun.bokee.com/2692649.html
http://www.ccw.com.cn/htm/center/prog/02_10_22_2.asp
http://llying.javaeye.com/blog/171455
http://hi.baidu.com/fish1996/blog/item/71412181e9825bdabd3e1eed.html(toutf8string)
poi的一个让人失望的bug

openxml4j支持excel2007



只有注册用户登录后才能发表评论。


网站导航: