实现批量导出功能

前提回顾:

 

 

演示文件下载:

所有文件下载的请求,只能发同步请求,不能异步

Controller:

@RequestMapping("/workbench/activity/fileDownload.do")
public void fileDownload(HttpServletResponse response) throws Exception{
    //1.设置响应类型
    response.setContentType("application/octet-stream;charset=utf-8");
    //2.获取输出流
    OutputStream out = response.getOutputStream();

    //设置响应头信息,使浏览器接收响应信息之后,直接激活文件下载窗口,即使能打开也不打开
    response.addHeader("Content-Disposition", "attachment;filename=mystudentList.xls");

    //读取excel文件(InputStream),输出到浏览器(OutputStream)
    InputStream is = new FileInputStream("E:\\java\\SSM\\studentList.xls");
    byte[] buff= new byte[256];

    int len=0;
    while ((len=is.read(buff))!=-1){
        out.write(buff,0,len);
    }
    is.close();
    out.flush();//从tomcat里借用
}

filedownloadtest.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
    <base href="<%=basePath%>>">
    <script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
    <title>演示文件下载</title>
    <script type="text/javascript">
        $(function () {
            //给下载按钮添加单击事件
            $("#fileDownloadBtn").click(function () {
                //发送文件下载的请求
                window.location.href="workbench/activity/fileDownload.do";
            });
        });
    </script>
</head>
<body>
    <input type="button" value="下载" id="fileDownloadBtn">
</body>
</html>

2.批量导出市场活动:

 Mapper:

<select id="selectAllActivities" resultMap="BaseResultMap">
  select a.id,u1.name as owner,a.name,a.start_date,a.end_date,a.cost,a.description,a.create_time,
         u2.name as create_by,a.edit_time,u3.name as edit_by
  from tbl_activity a
  join tbl_user u1 on a.owner=u1.id
  join tbl_user u2 on a.create_by=u2.id
  left join tbl_user u3 on a.edit_by=u3.id
  order by a.create_time desc
</select>

Controller:

@RequestMapping("/workbench/activity/exportAllActivities.do")
public void exportAllActivities(HttpServletResponse response) throws Exception{
    //调用service方法,查询所有的市场活动
    List<Activity> activityList = activityService.queryAllActivities();
    //创建excel文件,并且把activityList写到excel文件中
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("市场活动列表");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("ID");
    cell = row.createCell(1);
    cell.setCellValue("所有者");
    cell = row.createCell(2);
    cell.setCellValue("名称");
    cell = row.createCell(3);
    cell.setCellValue("开始日期");
    cell = row.createCell(4);
    cell.setCellValue("结束日期");
    cell = row.createCell(5);
    cell.setCellValue("成本");
    cell = row.createCell(6);
    cell.setCellValue("描述");
    cell = row.createCell(7);
    cell.setCellValue("创建时间");
    cell = row.createCell(8);
    cell.setCellValue("创建者");
    cell = row.createCell(9);
    cell.setCellValue("修改时间");
    cell = row.createCell(10);
    cell.setCellValue("修改者");

    //遍历activityList,创建HSSFRow对象,生成所有的数据行
    if(activityList!=null && activityList.size()>0){
        Activity activity = null;
        for(int i = 0;i < activityList.size();i++){
            activity=activityList.get(i);
            //没遍历一个activity,生成一行
            row = sheet.createRow(i+1);
            cell = row.createCell(0);
            cell.setCellValue(activity.getId());
            cell = row.createCell(1);
            cell.setCellValue(activity.getOwner());
            cell = row.createCell(2);
            cell.setCellValue(activity.getName());
            cell = row.createCell(3);
            cell.setCellValue(activity.getStartDate());
            cell = row.createCell(4);
            cell.setCellValue(activity.getEndDate());
            cell = row.createCell(5);
            cell.setCellValue(activity.getCost());
            cell = row.createCell(6);
            cell.setCellValue(activity.getDescription());
            cell = row.createCell(7);
            cell.setCellValue(activity.getCreateTime());
            cell = row.createCell(8);
            cell.setCellValue(activity.getCreateBy());
            cell = row.createCell(9);
            cell.setCellValue(activity.getEditTime());
            cell = row.createCell(10);
            cell.setCellValue(activity.getEditBy());
        }
    }

    //把生成的excel文件下载到客户端
    response.setContentType("application/octet-stream;charset=UTF-8");
    response.addHeader("Content-Disposition", "attachment;filename=activityList.xls");
    OutputStream out = response.getOutputStream();

    //直接内存到内存
    wb.write(out);
    wb.close();
    out.flush();//从tomcat里借用
}

前端;

//给批量导出按钮添加单击事件
$("#exportActivityAllBtn").click(function () {
   //发送同步请求
   window.location.href="workbench/activity/exportAllActivities.do";
});

文件上传演示:

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
    <base href="<%=basePath%>>">
    <title>文件上传</title>
</head>
<body>
<!--
    文件上传的表单三个条件:
    1.表单主键标签只能用:<input type="file">
    2.请求方式只能用:post
        post:参数通过请求头提交到后台;既能提交文件数据  ,又能提交二进制数据;对参数长度没有限制。相对安全;效率低
        get:参数通过请求头提交到后台;参数放在url后边;只能向后台提交文本数据;对参数有限制,数据不安全,效率高
    3.表单的编码格式只能用:multipart/form-data
      HTTP协议的规定,浏览器每次向后台提交参数,都会对参数进行统一编码;默认采用的编码格式是urlencoded,这种格式只能对文本数据进行编码
      文件上传的表单编码格式只能用multipart/form-data。

-->
    <form action="workbench/activity/fileUpload.do" method="post" enctype="multipart/form-data">
        <input type="file" name="myFile"><br>
        <input type="text" name="userName"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

Controller:

/**
 * 配置springmvc的文件上传解析器
 * @param userName
 * @param myFile
 * @return
 */
@RequestMapping("/workbench/activity/fileUpload.do")
@ResponseBody
public Object fileUpload(String userName, MultipartFile myFile)throws Exception{
    //把文本数据打印到控制台
    System.out.println("userName=" +userName);
    //把文件在服务指定的目录中生成一个同样的文件
    String originalFilename = myFile.getOriginalFilename();
    File file = new File("E:\\java\\SSM\\",originalFilename);
    myFile.transferTo(file);

    //返回响应
    ReturnObject returnObject = new ReturnObject();
    returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
    returnObject.setMessage("上传成功");

    return returnObject;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值