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();
    }