我必须为学校项目打开一个测试日期。该测试日期是一个 excel(xlsx) 文件,其中包含数字、字母、日期和时间。但是使用下面的代码它读取excel文件,但我得到奇怪的数字,例如42538.0和1.153481443E9。日期之后。
public class Page4_Controller implements Initializable {
@FXML
private Button button1;
public void Button1Action(ActionEvent event) throws IOException {
//Initialize excel file
FileChooser fc = new FileChooser();
fc.getExtensionFilters().addAll(
new ExtensionFilter("Excel Files", "*.xlsx"));
File selectedFile = fc.showOpenDialog(null);
// Read file
readXLSXFile(selectedFile.getAbsolutePath());
}
public static void readXLSXFile(String excelFilePath) throws IOException {
FileInputStream fys = new FileInputStream(new File(excelFilePath));
//Create workbook instance that refers to .xlsx file
XSSFWorkbook wb = new XSSFWorkbook(fys);
//Create a sheet object to retrive the sheet
XSSFSheet sheet = wb.getSheetAt(0);
//That is for evalueate the cell type
FormulaEvaluator forlulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
DataFormatter df = new DataFormatter();
//Default data for database (comes later)
//String airport = sheet.getRow(1).getCell(1).getStringCellValue();
//Date date = sheet.getRow(2).getCell(1).getDateCellValue();
for (Row row : sheet) {
for (Cell cell : row) {
switch (forlulaEvaluator.evaluateInCell(cell).getCellType()) {
//If cell is a numeric format
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
System.out.print(df.formatCellValue(cell) + "\t");
break;
//If cell is a string format
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getRichStringCellValue() + "\t");
break;
}
}
System.out.println();
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
最佳答案
日期在 Excel 内部存储为 double 值,并且 DataFormatter.formatCellValue(Cell cell) 以字符串形式返回单元格的格式化值,无论单元格类型如何。因此,当您调用 df.formatCellValue(cell) 时,您将以字符串形式返回单元格的 double 值。
要将单元格值打印为日期,代码如下:
switch (cell.getCellType()) {
// ...
case CellType.NUMERIC:
// Check if a cell contains a date. Since dates are stored internally in Excel as double values we infer it is a date if it is formatted as such.
if (DateUtil.isCellDateFormatted(cell)) {
// Get the value of the cell as a date. Returns a java.util.Date.
System.out.println(cell.getDateCellValue());
} else {
System.out.println(cell.getNumericCellValue());
}
// ...
}
来源:
- https://poi.apache.org/spreadsheet/quick-guide.html#CellContents
- https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html
- https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
- https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DateUtil.html
编辑
仅包含时间的 Excel 单元格的日期部分固定为 1899 年 12 月 31 日。您可以使用此信息来检查单元格是时间还是日期:提取年份,如果是 1899 年,则它是时间单元格。
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
switch (cell.getCellType()) {
case CellType.NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date dateCellValue = cell.getDateCellValue();
String year = yearFormat.format(dateCellValue);
if (year.equals("1899"))
System.out.println(timeFormat.format(dateCellValue));
else
System.out.println(dateFormat.format(dateCellValue));
} else {
System.out.println(cell.getNumericCellValue());
}
}
关于java - 使用 Apache POI 在 java 中读取 excel (xlsx) 文件(得到奇怪的数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47715176/