注意用poi解析器從word中抽取文本后,輸入流會(huì)關(guān)閉,原因如下:
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)
據(jù)說在POI3.0.3會(huì)修正該問題。
1,poi格子里的文本如何換行
? 加"\n",同時(shí)設(shè)置style.setWrapText(true);
?
2,style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
?style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
?設(shè)置前景色,有半透明的效果
?
3,sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));// 起始cell行、起始cell列、結(jié)束cell行、結(jié)束cell列。
?? 被合并了的單元格不需要生成。
?? 單元格合并後設(shè)置邊框只在原第一個(gè)上有效,如果想應(yīng)用的合并後的整體,則需要一個(gè)個(gè)的Create出單元格并應(yīng)用樣式,這個(gè)明顯是一個(gè)不太方便的操作,期待POI下一版的改進(jìn)了
?? 注意行是y軸,列是x軸。
?? poi-contrib下的HSSFRegionUtil可以設(shè)置合并區(qū)域的邊框的顏色和樣式
例如:HSSFRegionUtil.setLeftBorderColor(HSSFColor.BLACK.index,row,sheet,wb);
4,sheet.createFreezePane(2, 1);
? 凍結(jié)窗口,左上角為行參和列參的起始處,從0開始
?
5,列寬sheet.setColumnWidth((short) 0, (short) (35.7 * 110));//第一個(gè)參數(shù)為列的下標(biāo),110為想設(shè)置的寬度像素。
? http://www.cnblogs.com/interboy/archive/2007/08/27/872028.html
6,行高row.setHeight((short) (15.625 * 100));//100為要設(shè)置的行高的像素
?? row.setHeightInPoints((short)100);//應(yīng)該可以達(dá)到同樣的效果
7, POI將註解的部分,視做一個(gè)文字方塊,我們可以設(shè)定它的大小及內(nèi)容,然後將之指定給某特定的Cell。
節(jié)錄部分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,"統(tǒng)計(jì)",(short)1);//第三個(gè)參數(shù)為編碼方式,HSSFWorkbook.ENCODING_UTF_16=1
sheet的名稱不允許出現(xiàn)重復(fù)。
9, 先定義一個(gè)基本樣式,再?gòu)?fù)制樣式的屬性。不知何故不能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("統(tǒng)計(jì)".getBytes("GBK"), "ISO-8859-1")+".xls");
?? ??? ?ServletOutputStream out = response.getOutputStream();
?? ??? ?workbook.write(out);
?? ??? ?out.flush();
?? ??? ?out.close();
11,cell.setCellValue(new HSSFRichTextString("測(cè)試"));
在poi2.5.1里不管用,只能用cell.setCellValue("測(cè)試");
忘了二者有何區(qū)別
12,直接用workbook.getBytes()得到的文檔內(nèi)容的結(jié)構(gòu)是有問題的,會(huì)導(dǎo)致打開xls時(shí)報(bào)錯(cuò)。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workBook.write(bos);
//ByteArrayInputStream is = new ByteArrayInputStream(workBook.getBytes());//錯(cuò)誤
ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());//正確
詳見 http://numenzq.javaeye.com/blog/218816
13,POI的老版本的下載地址:http://archive.apache.org/dist/jakarta/poi/release/bin
14,每個(gè)sheet有65536的最大行數(shù)限制?
?? 數(shù)據(jù)量非常大時(shí),拆分為多個(gè)sheet(每個(gè)1萬行)可顯著降低內(nèi)存占用。
?? http://www.javaeye.com/topic/74835?page=3
15,可用PropertyUtils.copyProperties(newStyle, baseStyle)把baseStyle的屬性拷貝到newStyle中,但Font等拷貝不了。(淺拷貝,深拷貝?)
16,POI如何復(fù)制行,設(shè)置分頁符bug? 見使用POI操作Excel的幾點(diǎn)注意事項(xiàng)
和http://www.aygfsteel.com/liaojiyong/archive/2007/11/14/160588.html
17,復(fù)制行,復(fù)制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) ?
? 但是僅僅依靠這個(gè)有時(shí)候不能完全解決問題, ?
? 有時(shí)候需要用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
所以還是繼續(xù)用poi3.0.2吧
?? ??? ?
http://blog.csdn.net/impeller/archive/2007/10/29/1855368.aspx
解析Excel注釋,填充并生成報(bào)表的一個(gè)例子
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的一個(gè)讓人失望的bug
openxml4j支持excel2007
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)
據(jù)說在POI3.0.3會(huì)修正該問題。
1,poi格子里的文本如何換行
? 加"\n",同時(shí)設(shè)置style.setWrapText(true);
?
2,style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
?style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
?設(shè)置前景色,有半透明的效果
?
3,sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 1));// 起始cell行、起始cell列、結(jié)束cell行、結(jié)束cell列。
?? 被合并了的單元格不需要生成。
?? 單元格合并後設(shè)置邊框只在原第一個(gè)上有效,如果想應(yīng)用的合并後的整體,則需要一個(gè)個(gè)的Create出單元格并應(yīng)用樣式,這個(gè)明顯是一個(gè)不太方便的操作,期待POI下一版的改進(jìn)了
?? 注意行是y軸,列是x軸。
?? poi-contrib下的HSSFRegionUtil可以設(shè)置合并區(qū)域的邊框的顏色和樣式
例如:HSSFRegionUtil.setLeftBorderColor(HSSFColor.BLACK.index,row,sheet,wb);
4,sheet.createFreezePane(2, 1);
? 凍結(jié)窗口,左上角為行參和列參的起始處,從0開始
?
5,列寬sheet.setColumnWidth((short) 0, (short) (35.7 * 110));//第一個(gè)參數(shù)為列的下標(biāo),110為想設(shè)置的寬度像素。
? http://www.cnblogs.com/interboy/archive/2007/08/27/872028.html
6,行高row.setHeight((short) (15.625 * 100));//100為要設(shè)置的行高的像素
?? row.setHeightInPoints((short)100);//應(yīng)該可以達(dá)到同樣的效果
7, POI將註解的部分,視做一個(gè)文字方塊,我們可以設(shè)定它的大小及內(nèi)容,然後將之指定給某特定的Cell。
節(jié)錄部分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,"統(tǒng)計(jì)",(short)1);//第三個(gè)參數(shù)為編碼方式,HSSFWorkbook.ENCODING_UTF_16=1
sheet的名稱不允許出現(xiàn)重復(fù)。
9, 先定義一個(gè)基本樣式,再?gòu)?fù)制樣式的屬性。不知何故不能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("統(tǒng)計(jì)".getBytes("GBK"), "ISO-8859-1")+".xls");
?? ??? ?ServletOutputStream out = response.getOutputStream();
?? ??? ?workbook.write(out);
?? ??? ?out.flush();
?? ??? ?out.close();
11,cell.setCellValue(new HSSFRichTextString("測(cè)試"));
在poi2.5.1里不管用,只能用cell.setCellValue("測(cè)試");
忘了二者有何區(qū)別
12,直接用workbook.getBytes()得到的文檔內(nèi)容的結(jié)構(gòu)是有問題的,會(huì)導(dǎo)致打開xls時(shí)報(bào)錯(cuò)。
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workBook.write(bos);
//ByteArrayInputStream is = new ByteArrayInputStream(workBook.getBytes());//錯(cuò)誤
ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());//正確
詳見 http://numenzq.javaeye.com/blog/218816
13,POI的老版本的下載地址:http://archive.apache.org/dist/jakarta/poi/release/bin
14,每個(gè)sheet有65536的最大行數(shù)限制?
?? 數(shù)據(jù)量非常大時(shí),拆分為多個(gè)sheet(每個(gè)1萬行)可顯著降低內(nèi)存占用。
?? http://www.javaeye.com/topic/74835?page=3
15,可用PropertyUtils.copyProperties(newStyle, baseStyle)把baseStyle的屬性拷貝到newStyle中,但Font等拷貝不了。(淺拷貝,深拷貝?)
16,POI如何復(fù)制行,設(shè)置分頁符bug? 見使用POI操作Excel的幾點(diǎn)注意事項(xiàng)
和http://www.aygfsteel.com/liaojiyong/archive/2007/11/14/160588.html
17,復(fù)制行,復(fù)制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) ?
? 但是僅僅依靠這個(gè)有時(shí)候不能完全解決問題, ?
? 有時(shí)候需要用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
所以還是繼續(xù)用poi3.0.2吧
?? ??? ?
http://blog.csdn.net/impeller/archive/2007/10/29/1855368.aspx
解析Excel注釋,填充并生成報(bào)表的一個(gè)例子
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的一個(gè)讓人失望的bug
openxml4j支持excel2007