前些天写一个功能,我在excel中定义了一个模板,用户选择数据后根据模板生产excel;但是遇到要生产多页的情况;简单的說就是sheet1的模板要复制到sheet2,于是我使用了copySheet和importSheet两个jxl的方法,其中遇到了java.lang.NullPointerException的问题;我又想直接复制cell得了,可是没法实现复制合并单元格的情况!
来我还是觉定使用copysheet和importsheet两个方法;myeclipse根据报错的提示,我从网上下载了jxl最新的源码,根据提示在src/jxl/write/biff/WritableSheetCopier.java发现了这段代码
try
{
if (c != null)
{
toSheet.addCell(c);
// Cell.setCellFeatures short circuits when the cell is copied,
// so make sure the copy logic handles the validated cells
if (c.getCellFeatures() != null &
c.getCellFeatures().hasDataValidation())
{
validatedCells.add(c);
}
}
}
很明显&没有短路机制,切c.getCellFeatures() != null单独这一个判断明显还不够严谨!于是我改成如下代码
try
{
if (c != null)
{
toSheet.addCell(c);
// Cell.setCellFeatures short circuits when the cell is copied,
// so make sure the copy logic handles the validated cells
if (c.getCellFeatures() != null &&!c.getCellFeatures().toString().toLowerCase().equals("null")&&
c.getCellFeatures().hasDataValidation())
/* if (c.getCellFeatures() != null &
c.getCellFeatures().hasDataValidation())*/
{
validatedCells.add(c);
}
}
}
修改完成后,我用importSheet成功的复制了sheet1的模板到sheet2!(copySheet会把我的一些边框漏了)同时我还发现这个问题不止在这一个方法中有,这个文件的其他方法中也存在,建议大家全部改正,打一个自己公司用的jxl.jar包!(写这些api的大牛应该不会忽略这些问题,我在想是不是我的思路哪里出了问题,如果有人发现请指点一二)