当我尝试打印包含五位小数的单元格中的内容时,该数字将自动舍入为3 d.p.这是我到目前为止:
Cell cellNum = sheet1.getCell(col,row);
WritableCell cell = sheet1.getWritableCell(col, row);
if (cell.getType() == CellType.NUMBER){
cellNum = sheet1.getCell(col,row);
NumberFormat fivedps = new NumberFormat("#.#####");
WritableCellFormat cellFormat = new WritableCellFormat(fivedps);
cell.setCellFormat(cellFormat);
String cellContent = cellNum.getContents();
System.out.print(cellContent + "\t");
}
else{
// Cell cellNum = sheet1.getCell(col,row);
String cellContent = cellNum.getContents();
System.out.print(cellContent + "\t");
检查Cell类型后,如果它是类型编号,则将Cell转换为NumberCell以读取它的值。对于您分享的示例,它应该是这样的:
if (cell.getType() == CellType.NUMBER){
NumberCell cellNum = (NumberCell) sheet1.getCell(col,row);
System.out.println(cellNum.getValue()); //this will return a double value
}
您可以使用类似的技术通过将其转换为DateCell来读取其他值,例如Date。希望这可以帮助!