私はschoolprojectのtestdateを開く必要があります。このテスト日は、数字、文字、日付、時刻を含むExcel(xlsx)ファイルです。しかし、下のコードではExcelファイルが読み込まれますが、私は42538.0と1.153481443E9のような奇妙な数字を取得します。後日。進された後、ExcelでApoche POIを使用してjavaでexcel(xlsx)ファイルを読む(奇妙な数字を取得する)
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) {
}
}
42538.0は、日付(2016-06-17 00:00)とすることができます。セルタイプがDATEであるかどうかを確認し、それに応じて処理する必要があります。 – Stefan
日付は次のように書かれています:Date Found:17-Jun-2016、セルタイプはdateです。 –