簡體   English   中英

在單元格上使用 apache poi DataFormatter 時獲取錯誤的日期格式

[英]Getting wrong date Format while using apache poi DataFormatter on cell

我已經使用 Apache poi DataFormatter 類來獲取與 Excel 表中給出的日期相同的日期。 但在下面的情況下,它失敗了。

例如,如果我將日期指定為 2017 年 9 月 19 日,則它會變為 17 年 9 月 19 日。

我使用下面的代碼來獲取與 Excel 表中給出的日期值相同的日期值。

DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);  
XSSFRow row = sheet.getRow(0);
XSSFCell cell = row.getCell((short)0);
short x = wb.createDataFormat().getFormat("dd/m/yyyy;@");
CellStyle dateCellFormat = wb.createCellStyle();
dateCellFormat.setDataFormat(x);

cell.setCellStyle(dateCellFormat);
DataFormatter df = new DataFormatter();
String stringCellValue = df.formatCellValue(cell).toString();
System.out.println(stringCellValue);

此代碼為輸入19/12/2017打印19/12/2017 ,為19/09/2017打印19/9/2017 也許您可以調整我的代碼以使您的代碼正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM