使用 POI 从 Excel 工作表中获取日期 [英] get Date from Excel Sheet using POI
问题描述
我使用以下代码使用 POI 从 Excel 表中获取日期,
I have get the date from excel sheet using POI using the below code,
if (DateUtil.isCellDateFormatted(currentCell))
{
Date date = currentCell.getDateCellValue();
cellValue = date.getDate() + "/" + (date.getMonth() + 1) + "/" + (1900 + date.getYear());
}
Excel 表格的日期格式默认为 MM/dd/yy.但是如果我使月份值大于 12(即 >= 13),则格式更改为 dd/MM/yy.
By default the date format of excel sheet is MM/dd/yy. But if I make the month value is greater than 12 (i.e >= 13), the format is changed to dd/MM/yy.
通过上面的代码,如果我将日期指定为 10/11/2010,则将月份指定为 10,将日期指定为 11.如果将其指定为 15/10/2010,则月份为 10,日期为 15.
By the above code,If I give date as 10/11/2010, it gives month as 10 and date as 11. If I give it like 15/10/2010, I got month as 10 and date as 15.
但我需要维护一种格式,即 dd/MM/yyyy.我怎样才能做到这一点?
But I need to maintain the one format that is dd/MM/yyyy. How can I achieve this?
推荐答案
使用 SimpleDateFormat 来格式化日期,因为currentCell.getDateCellValue()) 返回一个日期.
Use SimpleDateFormat to format the date because "currentCell.getDateCellValue()) returns a Date.
if (DateUtil.isCellDateFormatted(currentCell))
{
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
cellValue = = sdf.format(currentCell.getDateCellValue());
} catch (ParseException e) {
e.printStackTrace();
}
}
这应该会阻止正在发生的自动转换.
This should prevent the automatic conversion that is taking place.
这篇关于使用 POI 从 Excel 工作表中获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!