java poi导出下载excel报表或下载到指定路径

1. 数据库查出数据导出excel浏览器并下载

    /**
     * 导出下载excel
     * @param list 导出数据
     * @throws Exception
     */
    public static void exportExcel(HttpServletResponse response, List<Student> list) throws Exception {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        XSSFWorkbook wb = new XSSFWorkbook();
        //标题行抽出字段
        String[] title = {"序号", "学号", "姓名", "性别", "入学时间", "住址", "手机号", "其他信息"};
        //设置sheet名称,并创建新的sheet对象
        Sheet stuSheet = wb.createSheet();
        //获取表头行
        Row titleRow = stuSheet.createRow(0);
        //创建单元格,设置style居中,字体,单元格大小等
        CellStyle style = wb.createCellStyle();
        Cell cell = null;
        //把已经写好的标题行写入excel文件中
        for (int i = 0; i < title.length; i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        //把从数据库中取得的数据一一写入excel文件中
        Row row = null;
        for (int i = 0; i < stuList.size(); i++) {
            //创建list.size()行数据
            row = stuSheet.createRow(i + 1);
            //把值一一写进单元格里
            //设置第一列为自动递增的序号
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(stuList.get(i).getStuId());
            row.createCell(2).setCellValue(stuList.get(i).getStuName());
            row.createCell(3).setCellValue(stuList.get(i).getGender());
            //把时间转换为指定格式的字符串再写入excel文件中
            row.createCell(4).setCellValue(stuList.get(i).getEnterTime());
            row.createCell(5).setCellValue(stuList.get(i).getAddress());
            row.createCell(6).setCellValue(stuList.get(i).getPhone());
            row.createCell(7).setCellValue(stuList.get(i).getOtherInfo());

        }
        //设置单元格宽度自适应,在此基础上把宽度调至1.5倍
        for (int i = 0; i < title.length; i++) {
            stuSheet.autoSizeColumn(i, true);
            stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
        }   
        //设置文件名
        String fileName = sdf1.format(new Date()) + "测试报表导出" + ".xlsx";
        //下载
        response.reset();
        response.setContentType("application/octet-stream; charset=utf-8");//以流的形式对文件进行下载
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));//对文件名编码,防止文件名乱码
        wb.write(response.getOutputStream());
        response.getOutputStream().close();

    }

2.数据库查出数据导出excel到指定目录

 /**   
     * 导出excel到指定路径
     * @param stuList 从数据库中查询需要导入excel文件的信息列表
     * @return 返回生成的excel文件的路径
     * @throws Exception
     */
    public static void exportExcel(HttpServletResponse response, List<Student> stuList) throws Exception {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        XSSFWorkbook wb = new XSSFWorkbook();
        //标题行抽出字段
        String[] title = {"序号", "学号", "姓名", "性别", "入学时间", "住址", "手机号", "其他信息"};
        //设置sheet名称,并创建新的sheet对象
        Sheet stuSheet = wb.createSheet();
        //获取表头行
        Row titleRow = stuSheet.createRow(0);
        //创建单元格,设置style居中,字体,单元格大小等
        CellStyle style = wb.createCellStyle();
        Cell cell = null;
        //把已经写好的标题行写入excel文件中
        for (int i = 0; i < title.length; i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }
        //把从数据库中取得的数据一一写入excel文件中
        Row row = null;
        for (int i = 0; i < stuList.size(); i++) {
            //创建list.size()行数据
            row = stuSheet.createRow(i + 1);
            //把值一一写进单元格里
            //设置第一列为自动递增的序号
            row.createCell(0).setCellValue(i + 1);
            row.createCell(1).setCellValue(stuList.get(i).getStuId());
            row.createCell(2).setCellValue(stuList.get(i).getStuName());
            row.createCell(3).setCellValue(stuList.get(i).getGender());
            //把时间转换为指定格式的字符串再写入excel文件中
            row.createCell(4).setCellValue(stuList.get(i).getEnterTime());
            row.createCell(5).setCellValue(stuList.get(i).getAddress());
            row.createCell(6).setCellValue(stuList.get(i).getPhone());
            row.createCell(7).setCellValue(stuList.get(i).getOtherInfo());

        }
        //设置单元格宽度自适应,在此基础上把宽度调至1.5倍
        for (int i = 0; i < title.length; i++) {
            stuSheet.autoSizeColumn(i, true);
            stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
        }
        //获取配置文件中保存对应excel文件的路径,本地也可以直接写成 D:\testDir 路径
        String folderPath = "D:\\testDir";
        //创建上传文件目录
        File folder = new File(folderPath);
        //如果文件夹不存在创建对应的文件夹
        if (!folder.exists()) {
            folder.mkdirs();
        }
        //设置文件名
        String fileName = sdf1.format(new Date()) + "测试报表导出" + ".xlsx";
        String savePath = folderPath + File.separator + fileName;
        OutputStream fileOut = new FileOutputStream(savePath);
        wb.write(fileOut);
        fileOut.close();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值