1. 程式人生 > >將json串匯入poi xls模板中再將其用io寫到指定位置資料夾

將json串匯入poi xls模板中再將其用io寫到指定位置資料夾

這也算是我昨天一天時間的學習成果吧,初入java,大神見笑。

String templatePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF" + File.separator + "lib" + File.separator + "template" + File.separator + "detailtemp" + ".xlsx";
//建立XLSTransformer物件
XLSTransformer transformer = new XLSTransformer();
BufferedInputStream in = null;
OutputStream out=null;
File file = null;
file=new File(templatePath);
//測試路徑
System.out.println("檔案是否存在:"+file.exists());
//獲取詳情結果
List<Map<String, Object>> list = this.selectResultDetail(vo);

Map<String,Object> beans = new HashMap<String,Object>();
beans.put("detailMap",list);
try {
   //獲得模板的輸入流
   in=new BufferedInputStream(new FileInputStream(file));
   //in = new FileInputStream(templatePath);
   //將beans通過模板輸入流寫到workbook中
   /*Workbook workbook = transformer.transformXLS(in,beans);
   out = new FileOutputStream("D:/home/gofar/CLUE/test.xls");
   workbook.write(out);
   out.flush();*/
   transformer.transformXLS(templatePath,beans,"D:/home/gofar/CLUE/test.xlsx");
} catch (Exception e) {
   e.printStackTrace();
}

註釋是因為我試了很多次,一直報空指標異常,然後我就用各種方法,最後發現,原來是模板裡面el表示式沒寫對,導致找不到對應的欄位存放資料。

這是模板裡面的el表示式