在Java中使用Apoche POI读取excel(xlsx)文件(获取奇怪的数字)

我必须打开一个学校项目的testingdate。 这个testdate是一个excel(xlsx)文件,包含数字,字母,date和时间。 但下面的代码读取Excel文件,但我得到奇怪的数字,如42538.01.153481443E9 。 在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) { } } 

date以双精度值forms存储在Excel中,DataFormatter.formatCellValue(单元格单元格) 单元格的格式化值作为string返回,无论单元格types如何 。 所以当你调用df.formatCellValue(cell)的时候,你会返回一个string作为单元格的double值。

要打印单元格值作为date代码是这样的:

 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单元格的date部分固定为1899年12月31日。您可以使用此信息来检查单元格是时间还是date:提取年份,如果是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中的date表示为“序列号”,小数点前的部分是1-1-1900之间的天数,小数点后的部分是date的小数部分,所以中午为0.5。 所以如果你自己得到这个号码,你必须手动进行转换。

POI可以为你做转换。 请参阅如何使用Apache POI读取具有Date的Excel单元格? 举一个例子。

这是我必须在Java中读取的Excel文件 在这里输入图像说明