【问题标题】:set Excel cell values of all type using Java使用 Java 设置所有类型的 Excel 单元格值
【发布时间】:2013-05-23 08:36:30
【问题描述】:

我需要将 Excel 行从一张表复制到另一张表。但是在复制而不是实际值之后,我得到了不同的值。具有字符串值但具有数值的单元格没有问题。 Excel 单元格可以包含任何类型的值。我的代码应该接受所有这些并将其复制到另一张纸上。在这种情况下请帮助我。

if(row.getCell(getLastCell)!=null && ((row.getCell(getLastCell).toString().equalsIgnoreCase(FAIL))|| (row.getCell(getLastCell).toString().equalsIgnoreCase("Invalid CR Statement"))))
            {
                failuresRow=failuresSheet.createRow(createNewRow++);
                for(int ii=0;ii<=getLastCell;ii++)
                {
                    failuresCell=failuresRow.createCell(ii);
                    failuresCell.setCellValue(row.getCell(ii)+"");
                    failuresCell.setCellType(row.getCell(ii).getCellType());
                    failuresCell.setCellStyle(row.getCell(ii).getCellStyle());
             }

            }

我知道 row.getCell(ii)+"" 是将单元格类型转换为字符串,但我想设置 setCellValue 以接受可以包含任何类型数据的单元格(例如:Date、Bollean、String、Number、 .........)

我已经尝试过使用 DataFormatter 和以下其他方式,但没有用。

  failuresCell.setCellValue(df.formatCellValue(row.getCell(ii)));


   switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                failuresCell.setCellValue(cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    failuresCell.setCellValue(cell.getDateCellValue());
                } else {
                    failuresCell.setCellValue(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                failuresCell.setCellValue(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                failuresCell.setCellValuec(cell.getCellFormula());
                break;
            default:
                // some code
        }

如果单元格的数值为 9200278,则输出为 1717。我不明白我在哪里做错了。在这种情况下请帮助我。

【问题讨论】:

  • 你在使用 apache poi 库吗?
  • 放置失败Cell.setCellType(row.getCell(ii).getCellType()); failuresCell.setCellStyle(row.getCell(ii).getCellStyle());在失败之前Cell.setCellValue(row.getCell(ii)+"");

标签: java excel


【解决方案1】:

这是因为您获取的是单元格而不是值。您可以使用以下方法代替row.getCell(int).toString()

row.getCell(int).getStringCellValue();
row.getCell(int).getNumericCellValue();
and etc.

在调用方法之前检查单元格的类型。这是一个例子:

if (cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
    row.getCell(int).getBooleanCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_ERROR) {
    row.getCell(int).getErrorCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
    row.getCell(int).getCellFormula();
} else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
    row.getCell(int).getNumericCellValue();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
    row.getCell(int).getStringCellValue();
}

【讨论】:

  • 太棒了!您建议的代码在我的应用程序中工作。非常感谢!!
【解决方案2】:

我只是事先将它们全部设置为字符串。

try {
        FileInputStream fileInputStream = new FileInputStream(excelTemplate);
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        for (int i=0; i < workbook.getNumberOfSheets(); i++) {
            for (Row row : workbook.getSheetAt(i)) {
                for (Cell cell : row) {
                    if (cell != null)
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多