JXL copySheet和importSheet 遇到java.lang.NullPointerException
前些天寫一個功能,我在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的大牛應該不會忽略這些問題,我在想是不是我的思路哪裡出了問題,如果有人發現請指點一二)