excel导出简单示例(jxl jar包)

   @param title excel文件名
  @param excelTopName 表头中文名字(显示在第一行的中文表头内容)
  @param header 表头字段属性(根据该属性获取对应的属性值,表头内容作为key值,根据该key值循环获取内容)
  @param content 内容
  @param response 响应
1
public boolean downloadExcel(String title, String[] excelHeaderChName, 2 String[] header, List<Map<String, String>> content, 3 HttpServletResponse response) { 4 try { 5 String name = java.net.URLEncoder.encode(title, "UTF8"); 6 String filename = name 7 + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()); 8 filename = new String(filename.getBytes("UTF-8"), "ISO-8859-1"); 9 response.reset(); 10 response.setCharacterEncoding("utf-8"); 11 response.setContentType("application/msexcel");// 设置为下载application/x-download 12 response.setHeader("Content-Disposition", "inline;filename=\"" 13 + filename + ".xls\""); 14 OutputStream os = response.getOutputStream(); 15 16 WritableWorkbook wwb = Workbook.createWorkbook(os); 17 // 创建excel工作表,指定名字和位置 18 WritableSheet sheet = wwb.createSheet(title, 0); 19 20 // 生成表头(行宽) 21 for (int i = 0; i < header.length; i++) { 22 sheet.addCell(new Label(i, 0, excelHeaderChName[i])); 23 // 设置excel列宽 24 sheet.setColumnView(i, 15); 25 } 26 27 // 内容 28 for (int i = 0; i < content.size(); i++) { 29 for (int j = 0; j < content.get(i).size(); j++) { 30 sheet.addCell(new Label(j, i + 1, content.get(i).get( 31 header[j]) 32 + "")); 33 } 34 } 35 36 // 写入工作表 37 wwb.write(); 38 wwb.close(); 39 os.close(); 40 } catch (IOException | WriteException e) { 41 e.printstackTrace(); 42 } 43 return true; 44 }

 

2019-1-7:使用poi导出Excel文件到浏览器时,出现内容乱码的情况,尝试各种方式都不行,后来改用jxl方式来导出Excel,问题得到解决。 在本地运行不会出现乱码,
但在resin服务上始终乱码,查看了服务器上的编码格式是gbk格式,打印了日志查询出来的内容也没有乱码,不清楚什么原因。。。望小伙伴们解答疑惑



附上poi导出Excel代码

/**
* @Description: 导出Excel
* @param workbook
* @param sheetNum (sheet的位置,0表示第一个表格中的第一个sheet)
* @param sheetTitle (sheet的名称)
* @param headers (表格的列标题)
* @param result (表格的数据)
* @param out (输出流)
* @throws Exception
*/
public static void exportExcel(HSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers,
  List<List<String>> result) throws Exception {
  // 生成一个表格
  HSSFSheet sheet = workbook.createSheet();
  workbook.setSheetName(sheetNum, sheetTitle);
  // 生成一个样式
  HSSFCellStyle style = workbook.createCellStyle();
  // 设置这些样式
  style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
  style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  // 生成一个字体
  HSSFFont font = workbook.createFont();
  font.setColor(HSSFColor.BLACK.index);
  font.setFontHeightInPoints((short) 12);
  font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  // 把字体应用到当前的样式
  style.setFont(font);

  // 指定当单元格内容显示不下时自动换行
  style.setWrapText(true);

  // 产生表格标题行
  HSSFRow row = sheet.createRow(0);
  for (int i = 0; i < headers.length; i++) {
    HSSFCell cell = row.createCell((short)i);

    cell.setCellStyle(style);
    HSSFRichTextString text = new HSSFRichTextString(headers[i]);

    // tempContent = new String(tempContent.getBytes("UTF-8"), "ISO-8859-1");
    //tempContent = new String(tempContent.getBytes("ISO-8859-1"), "UTF-8");

    cell.setCellValue(new String(text.toString().getBytes("GBK"), "ISO-8859-1"));
  }
  // 遍历集合数据,产生数据行
  if (result != null) {
    int index = 1;
      for (List<String> m : result) {
        row = sheet.createRow(index);
        int cellIndex = 0;
        for (String str : m) {
          HSSFCell cell = row.createCell((short)cellIndex);
          if(null == str || str.equals("")) {
            cell.setCellValue("");
          }else {
            cell.setCellValue(str.toString());
          }
        log.info("####"+str.toString());
        cellIndex++;
      }
    index++;
  }
}

}

 

posted @ 2018-03-29 14:32  秋水秋色  阅读(1209)  评论(0编辑  收藏  举报