Dwz作前台页面,Jfinal后台使用前台下载excel【两种解决方案】

废话不说了,开始了!html

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

  先说说个人逻辑:web

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

前台jsp部分:app

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

Controller部分:webapp

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页面作成的下载【给人以假象的感受】ui

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

前台jsp部分:3d

 <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表达式遍历的后台传出来的集合。

以上代码属本人概括总结,若有问题,还请多多指教。

相关文章
相关标签/搜索