I have to open a testdate for a schoolproject. This testdate is a excel(xlsx) file that includes number, letter, date and time. But with the code below it reads the excel file, but i get weird numbers like 42538.0 and 1.153481443E9. after date.
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) {
}
}