02 poi导致中文excel

  • poi导致中文excel

    List<Map<String, Object>> exportList = dsjService.getExportList(dsjDetail);
    String[][] map = {
      {"序号","ID"},
      {"需求类型","XQ_TYPE"},
      {"标题","TITLE"},
      {"正文","GD_INFO"},
      {"工单发起时间","CREATE_DATE_SHOW"},
      {"发起人","OPER_NAME"},
      {"发起人号码","OPER_MOBILE"},
      {"区县","ORG_1_NAME"},
      {"分局","ORG_2_NAME"},
      {"当前环节","CURRENT_TASK"},
      {"当前处理人","CURRENT_OPER_NAME"},
      {"当前处理人号码","CURRENT_OPER_MOBILE"},
      {"归档时间","FINISH_DATE_SHOW"},
      {"环节名称","TASK_NAME"},
      {"回复人号码","ROUTE_OPER_MOBILE"},
      {"回复人名称","ROUTE_OPER_NAME"},
      {"回复时间","DEAL_DATE_SHOW"},
      {"回复内容","DEAL_CONTENT"}};
    
    //开始输出
    HSSFWorkbook wb = new HSSFWorkbook();//定义一个excel
    
    HSSFFont headFont = wb.createFont();
    headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    headFont.setColor(HSSFColor.BLACK.index);
    headFont.setFontName("微软雅黑");
    headFont.setFontHeightInPoints((short)10);
    
    HSSFCellStyle headStyle = wb.createCellStyle();
    headStyle.setFont(headFont);
    headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    headStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    
    HSSFFont bodyFont = wb.createFont();
    bodyFont.setColor(HSSFColor.BLACK.index);
    bodyFont.setFontName("微软雅黑");
    bodyFont.setFontHeightInPoints((short)9);
    
    HSSFCellStyle bodyCell = wb.createCellStyle();
    bodyCell.setFont(bodyFont);
    bodyCell.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    bodyCell.setAlignment(HSSFCellStyle.ALIGN_LEFT);
    bodyCell.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    bodyCell.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    bodyCell.setBorderTop(HSSFCellStyle.BORDER_THIN);
    bodyCell.setBorderRight(HSSFCellStyle.BORDER_THIN);
    
    HSSFSheet sheet = wb.createSheet("Sheet1");
    HSSFCell cell = null;
    
    /********************************输出第一行合并表头 begin**************************************/
    HSSFRow row = sheet.createRow((short) 0);
    
    for (int i = 0; i < map.length; i++) {
      cell = row.createCell((short)i);
      cell.setCellStyle(headStyle);
      cell.setCellValue(new HSSFRichTextString(map[i][0]));
    }
    /********************************输出第一行合并表头 end**************************************/
    
    /********************************输出数据列表 begin**************************************/
    for (int i = 0; i < exportList.size(); i++) {
      row = sheet.createRow((short) (i+1));
      Map<String, Object> exportMap = exportList.get(i);
    
      for (int j = 0; j < map.length; j++) {
        cell = row.createCell((short)j);
        cell.setCellStyle(bodyCell);
        cell.setCellValue(new HSSFRichTextString(str.get(exportMap, map[j][1])));
      }
    }
    
    String file_name = new DateHelper().getToday("yyyyMMddHHmmss") + ".xls";
    //导出excel
    String path = "D:\\test\\" + file_name;
    BufferedOutputStream bos = null;
    BufferedInputStream bis = null;
    
    try {
      FileOutputStream fout = new FileOutputStream(path);
      wb.write(fout);
      fout.close();
    
      //fileUrl = java.net.URLEncoder.encode(fileUrl, "UTF-8");
      HttpServletResponse response = this.getResponse();
    
      response.setContentType("application/x-download");
      response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("倒三角.xls", "UTF-8"));
    
      bis = new BufferedInputStream(new java.io.FileInputStream(path));
      bos = new BufferedOutputStream(response.getOutputStream());
    
      byte[] buff = new byte[1024];
      int bytesRead;
      while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
        bos.write(buff, 0, bytesRead);
      }
    } catch (Exception e) {
    } finally {
      try {
        bis.close();
        bos.close();
      } catch (Exception e) {
      }
    }
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值