1

我需要将 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)+"" 是将 Cell 类型转换为字符串,但我想将 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。我不明白我在哪里做错了。在这种情况下请帮助我。

4

2 回答 2

2

这是因为您获取的是单元格而不是值。您可以使用以下方法代替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();
}
于 2013-05-23T08:51:27.447 回答
1

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

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();
    }
于 2015-05-14T20:00:07.350 回答