jfinal+poi导出excel

xiaoxiao2021-02-28  28

要: 近两天一直再整这个文件下载,本来一个都整不出来,得知Jfinal有个renderFile()就解决了,又碰到dwz前台报Http status: 200 OK ajaxOptions: parsererror thrownError: SyntaxError: Unexpected token,然后,改成jsp下载excel后奇迹般的Jfinal的renderFile()也通了,太高兴了,双喜呀!

废话不说了,开始了!

 方案一:JFinal的renderFile("路径")功能

  先说说我的逻辑:

    前台页面点击请求发送到后台Controller,然后Controller层主要根据所需条件进行表格的组装,组装好上传到服务器后,然后跳转到前台直接显示下载框,下载即可。

前台jsp部分:

<li><a class="icon" href="<%=basePath%>feedback/expFeedBack" ><span>导出信息</span></a></li>

Controller部分:

public void expFeedBack(){ String time=""; int count = 0; String mypath=""; try { List<FeedBack> list = FeedBack.dao.find("select f.id,f.content,f.datatime,f.tele from feedback f"); count = list.size(); time = DateUtil.formatDate(); String path = new File("").getAbsolutePath().replaceAll("\\\\", "/"); //获得Tomcat的默认路径 mypath = path.substring(0,path.lastIndexOf("/"))+"/webapps/3d/excel/"+time+"-"+count+"条.xlsx"; //截取字符串 FileOutputStream os = new FileOutputStream(mypath); Workbook workBook = new SXSSFWorkbook(100); // 只在内存中保留100行记录 Sheet sheet = workBook.createSheet(); try { int i=0; Row row1 = sheet.createRow(i++); Cell cell = row1.createCell(0); cell.setCellValue("id"); cell = row1.createCell(1); cell.setCellValue("反馈内容"); cell = row1.createCell(2); cell.setCellValue("反馈时间"); cell = row1.createCell(3); cell.setCellValue("联系方式"); cell = row1.createCell(4); for (int j = 0; j < list.size(); j++) { row1 = sheet.createRow(i++); cell = row1.createCell(0); cell.setCellValue(list.get(j).getInt("id")); cell = row1.createCell(1); cell.setCellValue(list.get(j).getStr("content")); cell = row1.createCell(2); cell.setCellValue(list.get(j).getStr("datatime")); cell = row1.createCell(3); cell.setCellValue(list.get(j).getStr("tele")); cell = row1.createCell(4); } workBook.write(os); }catch(Exception e){ e.printStackTrace(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } //判断路径是否存在 if(new File(mypath).isFile()){ renderFile(new File(mypath)); }else{ renderNull(); } }

然后就愉快的可以下载了

方案二:jsp页面做成的下载【给人以假象的感觉】

jsp导出excel其实就是后台查询完数据放入list集合中,然后跳转到jsp,然后jsp按照规定的格式显示出来,并且前台弹出框,所以就有了jsp下载excel

前台jsp部分:

<li><a class="icon" href="<%=basePath%>feedback/expFeedBack" ><span>导出信息</span></a></li>

然后就是后台部分:

 setAttr("feedBackList", FeedBack.dao.find("select * from feedback")); renderJsp("/admin/download.jsp");

这里的download.jsp是我做的一个下载弹出框要弹出格式的页面,内容如下

<%@ page contentType="application/vnd.ms-excel; charset=gbk" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page language="java" pageEncoding="GBK" import="com.dxcm.common.util.*"%> <% String time = DateUtil.formatDate(); String filename = new String((time).getBytes("GBK"),"ISO-8859-1"); response.setHeader("Content-disposition","attachment; filename="+filename+".xls"); %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body > <table border="1"> <tr> <td>ID</td> <td>反馈内容</td> <td>反馈时间</td> <td>联系方式</td> </tr> <c:forEach items="${feedBackList}" var ="f"> <tr> <td>${f.id}</td> <td>${f.content}</td> <td>${f.datatime}</td> <td>${f.tele}</td> </tr> </c:forEach> </table> </body> </html>

这里我用的是EL表达式遍历的后台传出来的集合。

以上代码属本人归纳总结,如有问题,还请多多指教。

转载请注明原文地址: https://www.6miu.com/read-200017.html

最新回复(0)