我需要将 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。我不明白我在哪里做错了。在这种情况下请帮助我。