---
title: "poi开发"
author: "dovecat"
date: "2007-03-28T21:27:43Z"
tags: []
slug: "poi"
draft: false
lastmod: "2007-03-28T21:27:43Z"
id: 66180
url: "https://dovecat.iteye.com/blog/66180"
---

1、设置分页符的bug。  
　　  
　　poi里的HSSFSheet类提供了setRowBreak方法可以设置Sheet的分页符。  
　　  
　　Bug：如果你要设置分页符的Sheet是本来就有的，并且你没有在里面插入过分页符，那么调用setRowBreak时POI会抛出空指针的异常。  
　　  
　　解决方法：在Excel里给这个sheet插入一个分页符，用POI打开后再把它删掉，然后你就可以随意插入分页符了。  
　　  
　　如果sheet是由poi生成的则没有这个问题。我跟踪了setRowBreak的源代码，发现是Sheet.java下的PageBreakRecord rowBreaks这个变量在搞鬼，如果Sheet里原来没有分页符，开发这个模块的那位兄台忘了为这个对象new实例，所以只能我们先手工给Excel插入一个分页符来触发poi为rowBreaks创建实例。  
　　  
　　2、如何拷贝行。  
　　  
　　我在gmane.org的poi用户论坛翻遍了每个相关的帖子，找遍了api，也没看到一个拷贝行的方法，没办法，只能自己写：  
　　  
　　//注：this.fWorkbook是一个HSSHWorkbook，请自行在外部new  
　　public void copyRows(String pSourceSheetName, String pTargetSheetName, int pStartRow, int pEndRow, int pPosition)  
　　{  
　　HSSFRow sourceRow = null;  
　　HSSFRow targetRow = null;  
　　HSSFCell sourceCell = null;  
　　HSSFCell targetCell = null;  
　　HSSFSheet sourceSheet = null;  
　　HSSFSheet targetSheet = null;  
　　Region region = null;  
　　int cType;  
　　int i;  
　　short j;  
　　int targetRowFrom;  
　　int targetRowTo;  
　　  
　　if ((pStartRow == -1) || (pEndRow == -1))  
　　{  
　　return;  
　　}  
　　sourceSheet = this.fWorkbook.getSheet(pSourceSheetName);  
　　targetSheet = this.fWorkbook.getSheet(pTargetSheetName);  
　　//拷贝合并的单元格  
　　for (i = 0; i &lt; sourceSheet.getNumMergedRegions(); i++)  
　　{  
　　region = sourceSheet.getMergedRegionAt(i);  
　　if ((region.getRowFrom() &gt;= pStartRow) && (region.getRowTo() &lt;= pEndRow))  
　　{  
　　targetRowFrom = region.getRowFrom() - pStartRow + pPosition;  
　　targetRowTo = region.getRowTo() - pStartRow + pPosition;  
　　region.setRowFrom(targetRowFrom);  
　　region.setRowTo(targetRowTo);  
　　targetSheet.addMergedRegion(region);  
　　}  
　　}  
　　//设置列宽  
　　for (i = pStartRow; i &lt;= pEndRow; i++)  
　　{  
　　sourceRow = sourceSheet.getRow(i);  
　　if (sourceRow != null)  
　　{  
　　for (j = sourceRow.getFirstCellNum(); j &lt; sourceRow.getLastCellNum(); j++)  
　　{  
　　targetSheet.setColumnWidth(j, sourceSheet.getColumnWidth(j));  
　　}  
　　break;  
　　}  
　　}  
　　//拷贝行并填充数据  
　　for (;i &lt;= pEndRow; i++)  
　　{  
　　sourceRow = sourceSheet.getRow(i);  
　　if (sourceRow == null)  
　　{  
　　continue;  
　　}  
　　targetRow = targetSheet.createRow(i - pStartRow + pPosition);  
　　targetRow.setHeight(sourceRow.getHeight());  
　　for (j = sourceRow.getFirstCellNum(); j &lt; sourceRow.getLastCellNum(); j++)  
　　{  
　　sourceCell = sourceRow.getCell(j);  
　　if (sourceCell == null)  
　　{  
　　continue;  
　　}  
　　targetCell = targetRow.createCell(j);  
　　targetCell.setEncoding(sourceCell.getEncoding());  
　　targetCell.setCellStyle(sourceCell.getCellStyle());  
　　cType = sourceCell.getCellType();  
　　targetCell.setCellType(cType);  
　　switch (cType)  
　　{  
　　case HSSFCell.CELL\_TYPE\_BOOLEAN:  
　　targetCell.setCellValue(sourceCell.getBooleanCellValue());  
　　break;  
　　case HSSFCell.CELL\_TYPE\_ERROR:  
　　targetCell.setCellErrorValue(sourceCell.getErrorCellValue());  
　　break;  
　　case HSSFCell.CELL\_TYPE\_FORMULA:  
　　//parseFormula这个函数的用途在后面说明  
　　targetCell.setCellFormula(parseFormula(sourceCell.getCellFormula()));  
　　break;  
　　case HSSFCell.CELL\_TYPE\_NUMERIC:  
　　targetCell.setCellValue(sourceCell.getNumericCellValue());  
　　break;  
　　case HSSFCell.CELL\_TYPE\_STRING:  
　　targetCell.setCellValue(sourceCell.getStringCellValue());  
　　break;  
　　}  
　　}  
　　}  
　　}  
　　  
　　这个函数有两个问题暂时无法解决：  
　　  
　　a、只能在同一个Workbook里面使用，跨Workbook总是拷不过去，不知道为什么？  
　　  
　　b、由于在拷贝行时也把行高也拷过去了，如果往这些单元格里写入的数据长度超过单元格长度，那么他们不会自动调整行高！  
　　  
　　有哪位大侠知道上面两个问题任意一个的解决方法，请第一时间通知我！！！  
　　  
　　3、公式的问题。  
　　  
　　POI对Excel公式的支持是相当好的，但是我发现一个问题，如果公式里面的函数不带参数，比如now()或today()，那么你通过getCellFormula()取出来的值就是now(ATTR(semiVolatile))和today(ATTR(semiVolatile))，这样的值写入Excel是会出错的，这也是我上面copyRow的函数在写入公式前要调用parseFormula的原因，parseFormula这个函数的功能很简单，就是把ATTR(semiVolatile)删掉，我把它的代码贴出来：  
　　private String parseFormula(String pPOIFormula)  
　　{  
　　final String cstReplaceString = "ATTR(semiVolatile)"; //$NON-NLS-1$  
　　StringBuffer result = null;  
　　int index;  
　　  
　　result = new StringBuffer();  
　　index = pPOIFormula.indexOf(cstReplaceString);  
　　if (index &gt;= 0)  
　　{  
　　result.append(pPOIFormula.substring(0, index));  
　　result.append(pPOIFormula.substring(index + cstReplaceString.length()));  
　　}  
　　else  
　　{  
　　result.append(pPOIFormula);  
　　}  
　　  
　　return result.toString();  
　　}  
　　至于为什么会出现ATTR(semiVolatile)，希望哪位大侠现身跟我解释一下。  
　　  
　　4、向Excel写入图片的问题。  
　　  
　　我上poi论坛查相关帖子，得到两种结论：1、不支持写入图片；2、支持写入图片，通过EscherGraphics2d这个Class实现。于是我就去查EscherGraphics2d这个Class，发现这个Class提供了N个drawImage方法，喜出望外的我开始写代码，结果调了一天，一直看不到效果，黔驴技穷的我在万般无奈下只好跟踪进drawImage这个函数内部，经过N个函数调用后在最底层函数发现了最终答案（偶当场暴走！！！）：  
　　  
　　public boolean drawImage(Image image, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,  
　　int sx2, int sy2, Color bgColor, ImageObserver imageobserver)  
　　{  
　　if (logger.check( POILogger.WARN ))  
　　logger.log(POILogger.WARN,"drawImage() not supported");  
　　return true;  
　　}