【问题标题】:Exporting to Excel in Java using Apache POI使用 Apache POI 在 Java 中导出到 Excel
【发布时间】:2014-12-07 19:10:05
【问题描述】:

我使用 Apache POI 以 excel 格式导出数据。导入顺利,但当我尝试打开文档时,我收到此消息“Excel 在文档“toto.xls”中遇到不可读的内容。您要恢复此工作簿的内容吗?如果此工作簿的来源是可靠的点击是。”

下面是导出excel的java代码

    public void exportExcel() throws IOException {

    // Creation workbook vide
    XSSFWorkbook workbook = new XSSFWorkbook();

    // Creation d'une feuille vierge
    XSSFSheet sheet = workbook.createSheet("Participant");
    List<toto> totoList = this.getAllToto;
    int indiceMap = 2;
    Map<String, Object[]> data = new TreeMap<String, Object[]>();
    data.put("1", new Object[] { "name", "surname" });

    for (TotoBean l : totoList) {
        data.put(Integer.toString(indiceMap),
                new Object[] { l.getName(), l.getSurname() });
        indiceMap++;
    }
    // Iteration sur la map data et ecriture dans dans la feuille excel
    Set<String> keyset = data.keySet();
    int rownum = 0;
    for (String key : keyset) {
        Row row = sheet.createRow(rownum++);
        Object[] objArr = data.get(key);
        int cellnum = 0;
        for (Object obj : objArr) {
            Cell cell = row.createCell(cellnum++);
            if (obj instanceof String)
                cell.setCellValue((String) obj);
            else if (obj instanceof Integer)
                cell.setCellValue((Integer) obj);
        }
    }

    DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    String dateToday = dateFormat.format(new Date());

    // Ecriture du fichier excel comme attachement
    ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
    workbook.write(outByteStream);
    byte[] outArray = outByteStream.toByteArray();
    String fileOut = "Liste-toto[" + this.getCity() + "]"
            + dateToday + ".xlsx";
    HttpServletResponse response = (HttpServletResponse) FacesContext
            .getCurrentInstance().getExternalContext().getResponse();

    response.setContentType("application/vnd.ms-excel");
    response.setContentLength(outArray.length);
    response.setHeader("Content-Disposition", "attachment; filename=\""
            + fileOut + "\"");

    OutputStream outStream = response.getOutputStream();
    outStream.write(outArray);

    outStream.flush();
    outStream.close();
}

【问题讨论】:

  • 我认为 XSSFSheet 创建的是 .xslx 文档,而不是 .xls 文档
  • 感谢您的回复,但 xslx 无法识别为 excel 文档
  • 对不起,我的意思当然是 xlsx...
  • 是的,我同意你的看法,它也是我生成的文件 "Liste-toto.xlsx" 的扩展名,但是如果没有之前显示包含在我的发布。
  • 你的 cell.setCellValue((String) obj);可能会在单元格中放入一些不允许的字符?我认为 Excel 有点困惑,当你有 , 或 .作为单元格内容。

标签: java excel jsf apache-poi


【解决方案1】:

您可以考虑“以管理员身份”打开它。根据这个(https://superuser.com/questions/401714/how-do-i-resolve-the-error-excel-found-unreadable-content-in-filename),它可以解决。

【讨论】:

    【解决方案2】:

    你的问题是这两行不匹配

    XSSFWorkbook workbook = new XSSFWorkbook();
    

    response.setContentType("application/vnd.ms-excel");
    

    如果你真的想生成一个.xls旧式Excel工作簿,你需要把第一行改成HSSFWorkbook而不是XSSFWorkbook

    如果您确实要生成.xlsx Excel 工作簿,则第二行的内容类型必须是正确的 .xlsx 类型,即:

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    

    【讨论】: