FAQ是学习的好素材,把POI的FAQ做些摘要.
*获取核心类位置,判断当前加载jar的版本,用于解决如"MethodNotFoundException"之类异常
ClassLoader classloader = org.apache.poi.poifs.filesystem.POIFSFileSystem.class.getClassLoader(); URL res = classloader.getResource( "org/apache/poi/poifs/filesystem/POIFSFileSystem.class"); String path = res.getPath(); System.out.println("Core POI came from " + path);
*HSSF的"eventmodel"
---无需读入整个Excel,减少内存消耗
---基于AWT的事件模型和SAX结合的基础上
---适合于只读情况
*POI支持BIFF8,故可能不能读Star Office5.1
*Excel在单元格中存储date的格式是数字
---HSSFDateUtil处理
case HSSFCell.CELL_TYPE_NUMERIC: double d = cell.getNumericCellValue(); // test if a date! if (HSSFDateUtil.isCellDateFormatted(cell)) { // format in form of M/D/YY cal.setTime(HSSFDateUtil.getJavaDate(d)); cellText = (String.valueOf(cal.get(Calendar.YEAR))).substring(2); cellText = cal.get(Calendar.MONTH)+1 + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cellText; }
*从servlet获取一个xls,其他2进制类同
---IE在显示通过servlet传输来的2进制文件时有问题,此问题没研究过,不细谈,请见faq给出的解决方案.
*对excel单元格设置格式
---workbook获取dataformat
DataFormat format = wb.createDataFormat();
---创建cellstyle,并设置dataformat
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
---cell设置style
cell.setCellStyle(style);
*在循环外创建cellstyle,在循环内创建cell,避免"Too Many Styles".
大多与Excel有关,是Excel应用多,还是Excel文档复杂呢?