1. 程式人生 > >json字元陣列轉List+匯出Excel表格

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", "匯出結果失敗!");*/
            }
        }
    }