如何使用JSONArray和POI填充excel而不会出现错误警告

时间:2015-11-06 04:59:38

标签: java json apache-poi

使用POI生成excel并循环JSON数组以填充单元格中的值 我的JSON数组结构如下所示

[[1,"A","0","6","0","6"],[2,"B","3","3","0","6"],[3,"C","3","0","0","3"],[4,"D","4","0","0","4"],[5,"E","3","6","0","9"],[6,"F","3","6","0","9"],[7,"G","3","3","0","6"],[8,"H","3","0","0","3"],[9,"I","3","3","0","6"],[10,"J","2","0","0","2"],[11,"L","3","0","0","3"],[12,"M","3","0","0","3"],[13,"N","3","0","0","3"],[14,"O","3","0","0","3"],[15,"P","3","0","0","3"],[16,"R","3","0","0","3"]]

这个数组像这样循环

for(int i=0;i<jsonArray.length();i++){
            Row row = sheet.createRow(rowCount);
            JSONArray array = jsonArray.getJSONArray(i);
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            for(int j=0;j<array.length();j++) {
                Cell  cell = row.createCell(j);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                cell.setCellValue(array.getString(j));
            }

}

这里excel中的数据是动态的,它可能包含数字或任何东西,但是正在打印它像ex cell.setCellValue(array.getString(j))那样导致像excel中的东西一样的警告(iet这个单元格中的数字前面是一个撇号)生成的excel可能包含数字float,String甚至是double。我如何解决这个问题,请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:1)

方法Cell.setCellValue()有许多种类,即根据您传入的参数类型,它将创建文本单元格,数字单元格,布尔值或任何其他支持类型。

当您传入字符串时,POI会在Excel工作簿中创建一个文本单元格。如果在填充单元格之前将字符串转换为数字(POI使用double以允许浮动值),它将被写为数字单元格,它应该除去Excel中的警告。

有关详细信息,请参阅javadoc