Apache POI实现excel表格导出

导出部分代码:

        try {
            resp.setHeader("Content-disposition", "attachment;filename="
                    + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xls");    //设置文件头编码格式
            resp.setContentType("application/vnd.ms-excel");//设置类型
            resp.setHeader("Cache-Control", "no-cache");//设置头
            resp.setDateHeader("Expires", 0);//设置日期头

            HSSFWorkbook book = new HSSFWorkbook();
            HSSFSheet sheet = book.createSheet(sheetName);
            CellStyle cellStyle = book.createCellStyle();
            cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd"));

            // 设置列名
            Row row = sheet.createRow(0);
            for (int i = 0; i < columnNames.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(columnNames[i]);
            }
            //设置表格内容
            int rowCount = 1;
            for (int i = 0; i < exportList.size(); i++) {
                EmployeeExcelData data = exportList.get(i);
                row = sheet.createRow(rowCount);
                rowCount++;
                Cell cell1 = row.createCell(0);
                cell1.setCellValue(data.getDeptId());
                Cell cell2 = row.createCell(1);
                cell2.setCellValue(data.getDeptName());
                Cell cell3 = row.createCell(2);
                cell3.setCellValue(data.getActualName());
                Cell cell4 = row.createCell(3);
                cell4.setCellValue(data.getUsername());
                Cell cell5 = row.createCell(4);
                cell5.setCellValue(data.getPhone());
                Cell cell6 = row.createCell(5);
                cell6.setCellValue(data.getRankIndex());
                Cell cell7 = row.createCell(6);
                cell7.setCellValue(data.getRuleName());
                Cell cell8 = row.createCell(7);
                cell8.setCellValue(data.getMail());
            }
            book.write(resp.getOutputStream());

            resp.getOutputStream().flush();
            resp.getOutputStream().close();
        } catch (Exception e) {
            resp.reset();
            resp.setContentType("application/json");
            resp.setCharacterEncoding("utf-8");
            System.out.println("导出失败");
        }
    }

office文件所对应的的 Content-type类型总结:
https://blog.csdn.net/gp_911014/article/details/108103428

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值