后台代码:
- Java code
public ActionForward doExpJdstudent(Action action, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("success to upload action===================== "); ActionForward forward = null; try { String path = "I:/workspace/diwebdemo/web/report/student.xls"; //InputStream is = new BufferedInputStream(new FileInputStream(path)); InputStream is = new FileInputStream(new File("I:/workspace/diwebdemo/web/report/student.xls")); List<Jdstudent> jsList = new ArrayList<Jdstudent>(); JdstudentService jdstudentService = new JdstudentService(); jsList = jdstudentService.getAllJdstudentList(); Map beans = new HashMap(); beans.put("students", jsList); XLSTransformer transformer = new XLSTransformer(); Workbook wb = transformer.transformXLS( is, beans); //response.setCharacterEncoding("UTF-8"); response.setHeader("Content-disposition", "inline; filename=" + "学员.xls"); response.setContentType("application/vnd.ms-excel"); OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); is.close(); forward = action.findForward(Viewconst.CONST_SUCCESS); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); log.error(e.getCause()); forward = action.findForward(Viewconst.CONST_FAILURE); } return forward; }
运行action报错:
- Java code
java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFSheet.removeRow(Lorg/apache/poi/hssf/usermodel/HSSFRow;)V at net.sf.jxls.util.TagBodyHelper.removeBorders(TagBodyHelper.java:139) at net.sf.jxls.controller.SheetTransformationControllerImpl.removeBorders(SheetTransformationControllerImpl.java:85) at net.sf.jxls.tag.ForEachTag.process(ForEachTag.java:157) at net.sf.jxls.transformer.TagRowTransformer.transform(TagRowTransformer.java:36) at net.sf.jxls.transformer.SheetTransformer.transformSheet(SheetTransformer.java:90) at net.sf.jxls.transformer.XLSTransformer.transformWorkbook(XLSTransformer.java:243) at net.sf.jxls.transformer.XLSTransformer.transformXLS(XLSTransformer.java:222) at view.xlsexport.JdStudentXls.doExpJdstudent(JdStudentXls.java:93) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585)
excel模版:
学员信息
姓名 身份证号码
<jx:forEach items="${students}" var="item">
${item.name} ${item.idnumber}
</jx:forEach>
------解决方案--------------------------------------------------------
jar包有点乱啊。。用 jxls,但是为什么错误信息里还有POIjar包得信息??
你把运行期的jar包,整理下把。用Tomcat的话就是WEB-INF/lib下的jar包。
------解决方案--------------------------------------------------------
lz不妨把这段代码运行一遍,或许会有意外的收获呢!