Sha*_*ble 2 java format time apache-poi
我已经使用 Apahe POI 将时间输入到 Excel 文件中,如下所示
Time time = Time.valueOf("19:30:00");
CellStyle cellStyle1 = workbook.createCellStyle();
CreationHelper createHelper1 = workbook.getCreationHelper();
cellStyle1.setDataFormat(
createHelper.createDataFormat().getFormat("HH:MM AM/PM"));
cell = row.getCell(1);
System.out.println(time.toString());
cell.setCellValue(time);
cell.setCellStyle(cellStyle1);
Run Code Online (Sandbox Code Playgroud)
这导致 Excel 符合预期,但是存在以下不匹配情况,发现 Excel 的实际值和显示值不同 - 我怎样才能使它们相同,我是否使用了错误的方法来更新 Excel 时间格式的值

小智 5
在Excel中,日期和时间存储为浮点数,即自午夜01/01/1900以来的天数。
如果您要存储一些小于1.0- 的值,它将被解释为时间,否则解释为日期,例如:
0.5将等于12:00:005.5将等于05.01.1900 12:00:00要正确处理日期和时间,请使用org.apache.poi.ss.usermodel.DateUtil,例如您的示例可能如下所示:
double time = DateUtil.convertTime("19:30:00");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(
workbook.createDataFormat().getFormat("HH:MM AM/PM"));
cell.setCellValue(time);
cell.setCellStyle(cellStyle);
Run Code Online (Sandbox Code Playgroud)
Excel 的结果将如下所示:
假设问题与上述内容有关,Excel中的实际日期/时间值应该是双精度值,并且表示值应该基于您设置的样式/模式;假设目标是实现这种相似性,即19:30:00在公式和07:30 PM单元格中。
如果不是,并且目标是07:30 PM在这两种情况下都有 - 那么您只需要存储一个字符串值,而不是日期/时间。
| 归档时间: |
|
| 查看次数: |
1545 次 |
| 最近记录: |