Tôi đang cố gắng tạo một trang tính Excel bằng cách sử dụng POI api trong Java. Trong Excel Work Sheet tôi muốn có một ô chỉ với TIME. Bằng cách thiết lập điều này, chúng ta có thể bao gồm các tế bào trong tổng kết của cột particuluar như chúng ta làm trong cột số. Đối với điều này, chúng ta cần định dạng ô theo Thời gian >> 13:30:55. (Định dạng nội bộ là 'h: mm: ss; @'). Và chúng ta cần phải loại bỏ phần ngày tháng khỏi ô.Đặt thời gian trong Excel bằng cách sử dụng POI
Khi tôi đọc ô giá trị ô bằng cách sử dụng POI, nó sẽ trở lại thành 'Sun Dec 31 01:00:00 IST 1899' (Khi tôi đặt giá trị là 1:00), chỉ mục định dạng ô là 166 và chuỗi định dạng ô là 'h: mm: ss; @'.
Sau khi thiết lập định dạng và kiểu được đọc từ excel và giá trị ô là 1800-December-31 và với giá trị thời gian, excel mới hiển thị ô là '######' (lỗi) và giá trị ô được đặt là '-1'. Dưới đây là mã tôi đã sử dụng. Tôi đã bỏ lỡ bất cứ điều gì? Có thể đặt giá trị theo yêu cầu của tôi không.
InputStream is = new BufferedInputStream(new FileInputStream("<FileName>"));
XSSFWorkbook wb = new XSSFWorkbook(is);
is.close();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = sheet.getRow(2);
XSSFCell cell = row.getCell(18);
System.out.println("ExcelFileReader main cell.getDateCellValue() : '" + cell.getDateCellValue() + "'");
System.out.println("ExcelFileReader main cell.getCellStyle().getDataFormat() : '" + cell.getCellStyle().getDataFormat() + "'");
System.out.println("ExcelFileReader main cell.getCellStyle().getDataFormat() : '" + cell.getCellStyle().getDataFormatString() + "'");
XSSFRow row1 = sheet.createRow(21);
XSSFCell cell1 = row1.createCell(2);
cell1.setCellStyle(cell.getCellStyle());
cell1.setCellValue(cell.getDateCellValue());
Calendar dummy = Calendar.getInstance();
dummy.setLenient(false);
dummy.set(Calendar.YEAR, 1899);
dummy.set(Calendar.MONTH, Calendar.DECEMBER);
dummy.set(Calendar.DATE, 31);
dummy.set(Calendar.HOUR, 00);
dummy.set(Calendar.MINUTE, 00);
dummy.set(Calendar.SECOND, 00);
dummy.set(Calendar.MILLISECOND, 00);
Calendar cc = Calendar.getInstance();
XSSFRow row2 = sheet.createRow(25);
XSSFCell cell2 = row2.createCell(2);
dummy.set(Calendar.HOUR, cc.get(Calendar.HOUR));
dummy.set(Calendar.MINUTE, cc.get(Calendar.MINUTE));
dummy.set(Calendar.SECOND, cc.get(Calendar.SECOND));
dummy.set(Calendar.MILLISECOND, cc.get(Calendar.MILLISECOND));
System.out.println("ExcelFileReader main dummy : '" + dummy.getTime() + "'");
cell2.setCellValue(dummy.getTime());
CellStyle style = wb.createCellStyle();
DataFormat df = wb.createDataFormat();
style.setDataFormat(df.getFormat("[h]:mm:ss;@"));
cell2.setCellStyle(style);
FileOutputStream fos = new FileOutputStream(new File("<New Excel file>"));
wb.write(fos);
fos.close();
System.out.println("ExcelFileReader DONE");
Sau đây là đầu ra của chương trình.
ExcelFileReader main cell.getDateCellValue() : 'Sun Dec 31 00:15:00 IST 1899'
ExcelFileReader main cell.getCellStyle().getDataFormat() : '166'
ExcelFileReader main cell.getCellStyle().getDataFormat() : 'h:mm:ss;@'
ExcelFileReader main dummy : 'Sun Dec 31 11:32:24 IST 1899'
ExcelFileReader DONE
###### có thể được hiển thị nếu cột thời gian không đủ rộng –