java在使用POI的非日期格式单元格中丢失日期准确性
我有一个日期为20160831的电子表格
到目前为止,我一直在使用:
if (Cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
// parse the date (irrelevant to this question as ^^ is false)
} else {
Cell.setCellType(Cell.CELL_TYPE_STRING);
// do some work with Cell.getStringCellValue();
}
}
但是,该字符串是从单元格返回的。在本例中,getStringCellValue()不是“20160831”,而是“20160930”
我已经打印了手机。getNumericCellValue();返回2.016093E7。这导致我在转换为字符串时丢失了“31”中的“1”
我曾尝试使用DataFormatter,希望能够显示“与Excel中显示的数据完全相同的数据”。(https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/DataFormatter.html#formatCellValue(org.apache.poi.ss.usermodel.Cell))
我试图使用手机。getCellDateValue(),但效果更糟
我尝试使用DecimalFormat达到同样的效果
我尝试使用NumberToTextConverter达到同样的效果
我总是在“31”中失去我的“1”。我发现这是手头上最麻烦的问题,尽管我在这个日期也得到了不正确的月份值(任何关于这方面的见解都会非常有用)
除了在解析之前手动将电子表格导出到csv文件之外,我找不到解决此问题的方法。同时,由于DateUtils通常返回true,因此大量其他文件与上面的代码配合得非常好
编辑:
我正在添加完整的块(这并不比我最初发布的内容多多少),以及电子表格的屏幕截图和打印输出的副本
private Transitional parseValueForTransitionalWithPropertyAndVector(Cell cell,
Transitional transitional, Property property, Vector vector) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() | cellDeterminedToHoldASerialiazedDate");
Calendar calendar = new GregorianCalendar();
Date date = null;
try {
date = cell.getDateCellValue();
calendar.setTime(date);
if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYSTART) {
transitional.setPayPeriodStart(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payStart[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
} else if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYEND) {
transitional.setPayPeriodEnd(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payEnd[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
} else if (property.getPropTypeKey() == TransitionalHelper.PROP_TYPE_KEY.PAYDATE) {
transitional.setPayDate(calendar);
logger.log(Level.INFO,
"parseValueForTransitionalWithPropertyAndVector() payDate[MM/DD/YYYY]: "
+ calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + "/"
+ calendar.get(Calendar.YEAR));
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
} else {
// Added to debug >>
System.out.println("cell.getNumericCellValue(): " + cell.getNumericCellValue());
DataFormatter formatter = new DataFormatter();
System.out.println("DataFormatter output: " + formatter.formatCellValue(cell));
System.out.println("DecimalFormat output: " + new DecimalFormat("0").format( cell.getNumericCellValue()));
System.out.println("NumberToTextConverter output: " + NumberToTextConverter.toText(cell.getNumericCellValue()));
cell.setCellType(Cell.CELL_TYPE_STRING);
System.out.println("cell.getStringCellValue(): " + cell.getStringCellValue());
// <<
transitional = parseValueForTransitionalWithPropertyAndVector(cell.getStringCellValue(),
transitional, property, vector);
}
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
transitional = parseValueForTransitionalWithPropertyAndVector(cell.getStringCellValue(),
transitional, property, vector);
}
return transitional;
}`
请澄清:
过渡对象是解析后的值所附加到的对象。属性定义如何解析单元格中的值。向量是一个地图,显示了在哪里找到值
输出:
cell.getNumericCellValue(): 2.016093E7
DataFormatter output: 20160930
DecimalFormat output: 20160930
NumberToTextConverter output: 20160930
cell.getStringCellValue(): 20160930
电子表格中相关单元格的屏幕截图(我无法上载整个电子表格):
共 (0) 个答案