使用poi插件建立Excel表格,
导入poi插件pom文件

<!--poi依赖-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.15</version>
    </dependency>

建立excel

		//调用service查询所有市场活动
        List<Activity> activityList = activityService.queryAllActivities();
        //创建wb
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建页
        HSSFSheet sheet = wb.createSheet("市场活动列表");
        //创建行
        HSSFRow row = sheet.createRow(0);
        //创建第一行单元格
        HSSFCell cell = row.createCell(0);

向execl表格中添加数据

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;
            for(int i = 0; i < activityList.size(); i ++){

                activity = activityList.get(i);
                //每创建一个市场活动对象,就创建一行
                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());

            }
        }

选择要输出的文件目录

OutputStream os = new FileOutputStream("E:\\javanote\\project\\ssmCRM\\serverDir\\activities.xls");
        //创建excel
        wb.write(os);

        //关闭资源
        os.close();
        wb.close();

输出到浏览器

		//把生成的excel文件下载到浏览器
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置响应头信息
        response.addHeader("Content-Disposition","attachment;filename=activities.xls");
        OutputStream out = response.getOutputStream();
        byte[] buff = new byte[255];
        FileInputStream is = new FileInputStream("E:\\javanote\\project\\ssmCRM\\serverDir\\activities.xls");
        int len = 0;
        while((len = is.read(buff)) != -1) {
            out.write(buff, 0, len);
        }
        is.close();
        out.flush();

文件下载不可以发送ajax请求,而应该是同步请求,因为ajax只能发送json数据,不能发送文件

//给批量导出按钮绑定单击事件
		$("#exportActivityAllBtn").click(function (){
			window.location.href = "workbench/activity/exportAllActivities.do"
		})

优化:由于io流是非常宝贵的资源,且访问计算机磁盘速度是比较慢的,我们通过写到磁盘上 ,有从磁盘上读取导出到浏览器,需要耗费更多的时间和空间,因此可以之间从数据库读到数据就直接在内存中进行导出到浏览器的功能

//        OutputStream os = new FileOutputStream("E:\\javanote\\project\\ssmCRM\\serverDir\\activities.xls");
//        //创建excel
//        wb.write(os);
//
//        //关闭资源
//        os.close();
//        wb.close();

        //把生成的excel文件下载到浏览器
        response.setContentType("application/octet-stream;charset=UTF-8");
        //设置响应头信息
        response.addHeader("Content-Disposition","attachment;filename=activities.xls");
        OutputStream out = response.getOutputStream();
        byte[] buff = new byte[255];
//        FileInputStream is = new FileInputStream("E:\\javanote\\project\\ssmCRM\\serverDir\\activities.xls");
//        int len = 0;
//        while((len = is.read(buff)) != -1) {
//            out.write(buff, 0, len);
//        }
//        is.close();
        wb.write(out);
        wb.close();
        out.flush();

浏览器
在这里插入图片描述
上传的excel文件
在这里插入图片描述

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐