简单的导出excel功能实现

js函数

var url = "/file/downloadRecordExcel?ids="+(要传递的ids,用逗号隔开);
    openUrl(url);

后台代码

    @RequestMapping(value = "/downloadRecordExcel")
    @ResponseBody
    public void downloadRecordReport(HttpServletRequest req, HttpServletResponse res,
                               @RequestParam Map<Object, Object> paramMap) throws Exception {

        // 创建一个webbook,对应一个Excel文件
        XSSFWorkbook wb = new XSSFWorkbook();
        // 在webbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = wb.createSheet("人员名册");
        sheet.setDefaultRowHeightInPoints(25);
        sheet.setColumnWidth(0, 10 * 256);
        sheet.setColumnWidth(1, 10 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        sheet.setColumnWidth(3, 25 * 256);
        sheet.setColumnWidth(4, 10 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        sheet.setColumnWidth(7, 30 * 256);
        sheet.setColumnWidth(8, 15 * 256);
        sheet.setColumnWidth(9, 15 * 256);
        sheet.setColumnWidth(10, 20 * 256);
        // 在sheet中添加表头第0行
        XSSFRow row = sheet.createRow((int) 0);
        // 创建单元格,并设置值表头 设置表头居中
        XSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(XSSFCellStyle.ALIGN_CENTER);
        style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        row.setRowStyle(style);
        // 添加列
        row.createCell((short) 0).setCellValue("序号");
        row.getCell(0).setCellStyle(style);
        row.createCell((short) 1).setCellValue("姓名");
        row.getCell(1).setCellStyle(style);
        row.createCell((short) 2).setCellValue("性别");
        row.getCell(2).setCellStyle(style);
        row.createCell((short) 3).setCellValue("身份证号");
        row.getCell(3).setCellStyle(style);
        row.createCell((short) 4).setCellValue("学历");
        row.getCell(4).setCellStyle(style);
        row.createCell((short) 5).setCellValue("毕业院校");
        row.getCell(5).setCellStyle(style);
        row.createCell((short) 6).setCellValue("专业");
        row.getCell(6).setCellStyle(style);
        row.createCell((short) 7).setCellValue("劳动合同起止时间");
        row.getCell(7).setCellStyle(style);
        row.createCell((short) 8).setCellValue("所在部门");
        row.getCell(8).setCellStyle(style);
        row.createCell((short) 9).setCellValue("工作岗位");
        row.getCell(9).setCellStyle(style);
        row.createCell((short) 10).setCellValue("联系方式");
        row.getCell(10).setCellStyle(style);

        //根据id 从数据库查询相关记录

        List<RecordModel> list = recordService.getList(paramMap);
        if(list!=null){
            for(int i = 0;i<list.size();i++){
                row = sheet.createRow((int) i + 1);
                RecordModel fee = list.get(i);
                row.createCell((short) 0).setCellValue(i+1);
                row.getCell(0).setCellStyle(style);
                row.createCell((short) 1).setCellValue(fee.getName());
                row.getCell(1).setCellStyle(style);
                row.createCell((short) 2).setCellValue(fee.getSex());
                row.getCell(2).setCellStyle(style);
                row.createCell((short) 3).setCellValue(fee.getIdCardNo());
                row.getCell(3).setCellStyle(style);
                row.createCell((short) 4).setCellValue(fee.getEducation());
                row.getCell(4).setCellStyle(style);
                row.createCell((short) 5).setCellValue(fee.getSchool());
                row.getCell(5).setCellStyle(style);
                row.createCell((short) 6).setCellValue(fee.getSpeciality());
                row.getCell(6).setCellStyle(style);
                row.createCell((short) 7).setCellValue(fee.getContractOnOff());
                row.getCell(7).setCellStyle(style);
                row.createCell((short) 8).setCellValue(fee.getDept());
                row.getCell(8).setCellStyle(style);
                row.createCell((short) 9).setCellValue(fee.getPost());
                row.getCell(9).setCellStyle(style);
                row.createCell((short) 10).setCellValue(fee.getTel());
                row.getCell(10).setCellStyle(style);

            }
        }
        String fileName = "人员名册.xlsx";
        if (req.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
            fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// firefox浏览器
        } else {
            fileName = URLEncoder.encode(fileName, "UTF-8");// 其他浏览器包括IE浏览器和google浏览器
        }
        fileName = "\"" + fileName + "\"";
        res.setContentType("application/vnd.ms-excel");
        res.setHeader("Content-disposition", "attachment;filename=" + fileName);
        OutputStream ouputStream = res.getOutputStream();
        wb.write(ouputStream);
        ouputStream.flush();
        ouputStream.close();
    }

上面注释写得很清楚了,一下子就明白了的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值