java实现excel导出

工具类!!!

public static SXSSFWorkbook createWorkBook(List<Map<String, Object>> list, String[] keys, String[] columnNames) {
        SXSSFWorkbook wb = new SXSSFWorkbook(15);
        Sheet sheet = wb.createSheet(((Map)list.get(0)).get("sheetName").toString());

        for(int i = 0; i < keys.length; ++i) {
            sheet.setColumnWidth((short)i, 5355);
        }

        Row row = sheet.createRow(0);
        CellStyle cs = wb.createCellStyle();
        CellStyle cs2 = wb.createCellStyle();
        Font f = wb.createFont();
        Font f2 = wb.createFont();
        f.setFontHeightInPoints((short)10);
        f.setColor(IndexedColors.BLACK.getIndex());
        f.setBoldweight((short)700);
        f2.setFontHeightInPoints((short)10);
        f2.setColor(IndexedColors.BLACK.getIndex());
        cs.setFont(f);
        cs.setBorderLeft((short)1);
        cs.setBorderRight((short)1);
        cs.setBorderTop((short)1);
        cs.setBorderBottom((short)1);
        cs.setAlignment((short)2);
        cs2.setFont(f2);
        cs2.setBorderLeft((short)1);
        cs2.setBorderRight((short)1);
        cs2.setBorderTop((short)1);
        cs2.setBorderBottom((short)1);
        cs2.setAlignment((short)2);

        int i;
        for(i = 0; i < columnNames.length; ++i) {
            Cell cell = row.createCell(i);
            cell.setCellValue(columnNames[i]);
            cell.setCellStyle(cs);
        }

        for(i = 1; i < list.size(); ++i) {
            Row row1 = sheet.createRow(i);

            for(int j = 0; j < keys.length; ++j) {
                Cell cell = row1.createCell(j);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(StringEscapeUtils.unescapeHtml4(((Map)list.get(i)).get(keys[j]) == null ? " " : ((Map)list.get(i)).get(keys[j]).toString()));
                cell.setCellStyle(cs2);
            }
        }

        return wb;
    }
public static Exception export(HttpServletResponse response, List<Map<String, Object>> list, String fileName, String[] keys, String[] columnNames) throws IOException {
        SXSSFWorkbook wb = createWorkBook(list, keys, columnNames);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        Exception var9;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] content = os.toByteArray();
            InputStream is = new ByteArrayInputStream(content);
            response.reset();
            response.setContentType("applicationnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + DateUtil.getFormatCurrentDate("yyyyMMddHHmmss") + ".xls").getBytes(), "iso-8859-1"));
            ServletOutputStream out = response.getOutputStream();
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];

            int bytesRead;
            while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }

            return null;
        } catch (Exception var17) {
            log.error("导出工具类ExcelUtil报错,错误名称:" + var17.getMessage());
            var9 = var17;
        } finally {
            if (bis != null) {
                bis.close();
            }

            if (bos != null) {
                bos.close();
            }

        }

        return var9;
    }

调用类

public Map<String,Object> export(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String,Object>> exportList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("sheetName","sheet");//创建一个sheet
        exportList.add(map);//exportList是存放所有的数据
        Map<String,Object> condition = getParameterMap(request);
        dataExecuteMap(condition,request,MenuFunCode.SOUND_RECORD_MENU_CODE,MenuFunCode.EXPORT_FUNC);
        // 获取到所有的数据
        List<PhoneAttachDownLoadModel> records = phoneAttachDownLoadService.findPageByCondition(condition);
        if (records.size()>0){
            List<Map<String,Object>> mapList=new ArrayList<>();
            // 遍历数据 暂存到一个map中
            for (PhoneAttachDownLoadModel record:records) {
                Map<String,Object> map1=new HashMap<>();
                map1.put("outterAppId",record.getOutterAppId());
                map1.put("caseNo",record.getCaseNo());
                map1.put("customerName",record.getCustomerName());
                // 将map1中的数据存放到List<Map>中
                exportList.add(map1);
            }
        }
        String fileName = "工单录音";
        String[] columnNames = {"工单号", "案件号", "客户姓名"};// 列名
        String[] keys = {"outterAppId", "caseNo", "customerName"};// map中的key
        // 导出
        Exception exception = null;
        try {
            exception = ExcelUtil.export(response, exportList, fileName, keys, columnNames);
            if (null != exception) {
                log.error("导出方法报错", exception);
                return returnResult(exception);
            }
        } catch (IOException e) {
            log.error("导出报错", e);
            return returnResult(e);
        }
        return null;
    }

注意:导出excel为什么不能用ajax请求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值