这也算是我昨天一天时间的学习成果吧,初入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表达式