实现文件导出功能(Excel文件形式):选择导出

准备工作

  • 给"批量导出"按钮添加单击事件,发送导出请求
  • 查询所有的市场活动
  • 创建一个excel文件,并且把市场活动写到excel文件中
  • 把生成的excel文件输出到浏览器(文件下载)

技术

所需依赖:

关于办公文档插件使用的基本思想:把办公文档的所有元素封装成普通的Java类,程序员通过操作这些类达到操作办公文档目的。

名称属性
文件HSSFWorkbook
HSSFSheet
HSSFRow
HSSFCell
样式HSSFCellStyle

使用apache-poi生成excel:

 <dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.15</version>
</dependency>

客户端选择要导出的市场活动,点击“选择导出”向后台发送请求

  • 在查询数据的时候把id封装在CheckBox复选框的value值中
    在这里插入图片描述

前台发送请求代码

先获取到要选择导出的市场活动列表的id集合,然后封装成id=xxx&id=xxx&id=xxx&…id=xxx,再将封装后的id作为参数发送到后端

//给选择导出按钮添加单击事件
		$("#exportActivityXzBtn").click(function () {
			/**收集参数
			 * 	获取列表中所有被选中的CheckBox(因为查询数据的时候把id值封装在CheckBox复选框的value值中)
			 *		htmlStr+="<td><input type=\"checkbox\" value=\""+object.id+"\" /></td>";
			 */
			var checkedIds = $("#tBody input[type='checkbox']:checked");
			if(checkedIds.size()==0){
				alert("请选择要导出的市场活动(至少选择一条)~~~~")
				return;
			}
			var ids = "";
			$.each(checkedIds,function () {//id=xxx&id=xxx&id=xxx&...&id=xxx&
				ids += "id="+this.value+"&";
			})
			ids = ids.substr(0,ids.length-1);//id=xxx&id=xxx&id=xxx&...&id=xxx
			//发送同步请求
			window.location.href='workbench/activity/exportActivityXzBtn.do?'+ids;
			//发送请求完后,把复选框取消选中状态
			$("#checkAll").prop("checked",false);
			//当导出市场活动信息表后复选框全部取消选中
			$("#tBody").find("input[type='checkbox']").prop("checked",false)
			//$("#tBody input[type='checkbox']:checked").prop("checkbox","false");
		});

ActivityMapper.java

/**
     * 根据id查询市场活动
     * @param id
     * @return
     */
    List<Activity> selectActivityByIds(String[] id);

ActivityMapper.xml

<!--/**
  * 根据id查询市场活动
  * @param id
  * @return
  */
  List<Activity> selectActivityByIds(String[] id);-->
  <select id="selectActivityByIds" resultMap="BaseResultMap" parameterType="string">
    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
    where a.id in
    <foreach collection="array" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
    order by a.create_time desc
  </select>

ActivityService.java

List<Activity> queryActivityByIds(String[] id);

ActivityServiceImpl.java


    @Override
    public List<Activity> queryActivityByIds(String[] id) {
        return activityMapper.selectActivityByIds(id);
    }

后台响应请求代码

/**
     * 选择导出市场活动
     *  *每次至少选择导出一条记录
     * 	*导出成功之后,页面不刷新
     * @param id
     * @return
     */
    @RequestMapping("/workbench/activity/exportActivityXzBtn.do")
    @ResponseBody
    public void exportActivityXzBtn(String[] id, HttpServletResponse response) throws IOException {
        List<Activity> activityList = activityService.queryActivityByIds(id);
        HSSFWorkbook workbook = new HSSFWorkbook();
        //sheet这一页的“页名称”
        HSSFSheet sheet = workbook.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("修改者");
		//循环遍历市场活动集合列表
        if(activityList!=null && activityList.size()>0){
            Activity activity = null;
            //遍历activityList,创建HSSFRown对象,生成所有数据行
            for(int i=0;i<activityList.size();i++){
                activity = activityList.get(i);

                //每次遍历出一个activity对象,创建数据行(生成一行)
                //i+1是因为第一行已经创建好了,是表头行
                row = sheet.createRow(i + 1);//第1+i行
                //每一行创建11列,每一列的数据从activity对象中获取
                cell = row.createCell(0);//第1+i行第一列
                cell.setCellValue(activity.getId());
                cell = row.createCell(1);//第1+i行第二列
                cell.setCellValue(activity.getOwner());
                cell = row.createCell(2);//第1+i行第三列
                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());
            }
            //根据workbook对象生成Excel文件
       /* OutputStream fileOutputStream = new FileOutputStream("F:\\360downloads\\filedownload\\activityList.xls");
        workbook.write(fileOutputStream);
        //释放资源
        fileOutputStream.close();*/
            workbook.close();
            /**
             * 把生成的Excel文件从服务器端下载到客户端
             */
            //1.设置响应类型
            response.setContentType("appplication/octet-stream;charset=UTF-8");
            //2.获取输出流
            OutputStream out = response.getOutputStream();
            //浏览器接收到响应请求信息后,默认情况下,直接在显示窗口打开响应信息,即使打不开,也会调用应用程序打开;
//            只有实在打不开,才会激活文件下载窗口
            //可以设置响应头信息,使浏览器接收到响应信息后,直接激活文件下载窗口,即使能打开也不打开
            response.addHeader("Content-Disposition","attachment;filename=activitySelectList.xls");
           /* //读取Excel文件(InputStream),把输出到到浏览器(OutPutStream)
            InputStream is = new FileInputStream("F:\\360downloads\\filedownload\\activityList.xls");
            byte[] buff = new byte[256];
            int len = 0;
            while ((len = is.read(buff))!=-1){
                out.write(buff,0,len);

            }
            System.out.println("len="+len);
            System.out.println("buff="+buff);*/
//        释放资源
            /*is.close();*/
            workbook.write(out);
            out.flush();
            System.out.println("文件下载完成~~~~~~~");
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值