[英]Read excel (xlsx) file in java using Apoche POI (getting weird numbers)
我必須為一個學校項目打開一個測試日期。 此測試日期是一個包含數字、字母、日期和時間的 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中,並且DataFormatter.formatCellValue(Cell cell) 以String形式返回單元格的格式化值,而與單元格類型無關 。 因此,當您調用df.formatCellValue(cell)時,您將以字符串形式返回單元格的雙精度值。
要將單元格值打印為日期,代碼是這樣的:
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());
}
// ...
}
資源:
編輯
僅包含時間的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());
}
}
Excel中的日期表示為“序列號”,其中小數點分隔符前面的部分是自1-1-1900以來的天,小數點后的部分是該天的分數,因此中午為0.5。 因此,如果您自己獲得數字,則必須手動進行轉換。
POI可以為您進行轉換。 請參閱如何使用Apache POI讀取具有Date的Excel單元格? 舉個例子
這是因為指數數。 試試這個。
BigInteger x = new BigDecimal("406770000244E+12").toBigInteger();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.