json字符数组转List+导出Excel表格

 首先是json字符数组转List集合对象

  String jsonString = custIcCardDubboService.getExcelFailData(keyName);
  List<CardExcelVo> excelVoList = JSON.parseArray(jsonString, CardExcelVo.class);
  /**
     * 导出上传ic卡号Excel接口
     *
     * @param keyName:传一个uuid,即redis的key
     * @return Excel表格
     */
    @RequestMapping(value = "/exportData", method = RequestMethod.GET)
    @ResponseBody
    public Object exportData(String keyName) {
        Map<String, Object> resultMap = new HashMap<>();
        String jsonString = custIcCardDubboService.getExcelFailData(keyName);
        List<CardExcelVo> excelVoList = JSON.parseArray(jsonString, CardExcelVo.class);
        if (null != excelVoList && excelVoList.size() > 0) {
            Map<String, CardExcelVo> hashMap = new HashMap<>(16);
            for (CardExcelVo cardExcelVo : excelVoList) {
                hashMap.put(StringUtils.getUUID(), cardExcelVo);
            }
            try {
                exportempExl(hashMap);
            } catch (IOException e) {
                logger.error("导出上传ic卡号{}", e);
                resultMap.put("msg", "导出失败!");
            }
        }
        return resultMap;
    }
 /**
     * 导出Excel方法
     *
     * @param map
     * @throws IOException
     */
    public void exportempExl(Map<String, CardExcelVo> map) throws IOException {
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=exportIcExcel.xls");
        response.setContentType("application/x-msdownload");
        OutputStream os = response.getOutputStream();
        // 创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = null;
        //map不为空
        int j = 0;
        int index = 0;
        if (null != map && map.size() > 0) {
            for (String cust : map.keySet()) {
                if (index == 0) {
                    sheet = wb.createSheet("IC卡导入结果信息表");
                } else if (index % 30000 == 0) {
                    index++;
                    sheet = wb.createSheet("IC卡导入结果信息表" + j);
                }
                index++;
                HSSFRow row1 = sheet.createRow(0);
                row1.createCell(0).setCellValue("IC卡号*");
                row1.createCell(1).setCellValue("业主姓名*");
                row1.createCell(2).setCellValue("身份证号码");
                row1.createCell(3).setCellValue("联系方式");
                row1.createCell(4).setCellValue("到期时间(年-月-日)");
                row1.createCell(5).setCellValue("结果");
                //设置宽度
                HSSFRow row = sheet.createRow(index);
                CardExcelVo vo = map.get(cust);
                if (!StringUtils.isExistBlank(vo.getCardNo())) {
                    row.createCell(0).setCellValue(vo.getCardNo());
                    sheet.setColumnWidth(0, vo.getCardNo().getBytes().length * 2 * 256);
                }
                if (!StringUtils.isExistBlank(vo.getCustName())) {
                    row.createCell(1).setCellValue(vo.getCustName());
                    sheet.setColumnWidth(1, vo.getCustName().getBytes().length * 2 * 256);

                }
                if (!StringUtils.isExistBlank(vo.getCertificateId())) {
                    row.createCell(2).setCellValue(vo.getCertificateId());
                    sheet.setColumnWidth(2, vo.getCertificateId().getBytes().length * 2 * 256);
                }
                if (!StringUtils.isExistBlank(vo.getMobile())) {
                    row.createCell(3).setCellValue(vo.getMobile());
                    sheet.setColumnWidth(3, vo.getMobile().getBytes().length * 2 * 256);
                }
                if (!StringUtils.isExistBlank(vo.getEndTime())) {
                    row.createCell(4).setCellValue(vo.getEndTime());
                    sheet.setColumnWidth(4, vo.getEndTime().getBytes().length * 256);
                }
                row.createCell(5).setCellValue(ExcelConstant.Excel_Emport_Result);
                //设置宽度
                sheet.setColumnWidth(5, ExcelConstant.Excel_Emport_Result.getBytes().length * 256);
            }
            try {
                wb.write(os);
                os.close();
                os.flush();
             /*   result.put("status", 1);
                result.put("msg", "导出结果成功!");*/
            } catch (Exception e) {
              /*  result.put("status", 0);
                result.put("msg", "导出结果失败!");*/
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/riju4713/article/details/83687859
今日推荐