我已经使用 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);
结果如预期的那样,但是发现了以下不匹配的情况 Excel 的实际值和显示值不同 - 我怎样才能使它们相同,我是否使用了错误的方法来更新 Excel 时间格式的值
最佳答案
在Excel中,日期和时间存储为 float ,即自午夜01/01/1900以来的天数。
如果您要存储一些小于1.0
的值 - 它将被解释为时间,否则解释为日期,例如:
0.5
将等于12:00:00
5.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);
结果 Excel 将如下所示:
<小时/>假设问题与上述内容有关,Excel 中的实际日期/时间值应该是 double 值,并且表示值应该基于您设置的样式/模式;假设目标是实现这种相似性,即公式中的 19:30:00
和单元格中的 07:30 PM
。
如果不是,并且目标是在这两种情况下都达到 07:30 PM
- 那么您只需要存储一个字符串值,而不是日期/时间。
关于java - 使用apache poi输入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52426370/