java 如何使用apache poi在excel中将日期设置为日期类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11257595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set date as type date in excel with apache poi?
提问by cremersstijn
I am using apache poi 3.8 to create an excel file. This excel file needs to contain some dates.
我正在使用 apache poi 3.8 创建一个 excel 文件。这个 excel 文件需要包含一些日期。
I am trying to write a date to the excel file with as format the excel type "date". But i always get a type "custom". I need to use the the type "date", so it will be localized based on the users settings.
我正在尝试将日期写入 excel 文件,格式为 excel 类型“日期”。但我总是得到一个“自定义”类型。我需要使用“日期”类型,因此它将根据用户设置进行本地化。
I have tried the following:
我尝试了以下方法:
Apache POI localized Date into Excel cell
But it doens't work.
但它不起作用。
This is the code that I have:
这是我拥有的代码:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");
XSSFDataFormat df = wb.createDataFormat();
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(df.getFormat("d-mmm-yy"));
XSSFCell cell = sheet.createRow(0).createCell(0);
Calendar c = Calendar.getInstance();
c.set(2012,3-1,18);
cell.setCellValue( c.getTime() );
cell.setCellStyle(cs);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("c:\temp\dates-sworkbook.xlsx");
wb.write(fileOut);
fileOut.close();
Which format should I use?
我应该使用哪种格式?
Thanks
谢谢
回答by Gagravarr
Dateis one of the special, built in formats. If you want to use that one explicitly, then you need to pick it explicitly. (Almost all formats used in Excel are custom ones, that render how you specify, but there are a few special ones)
日期是一种特殊的内置格式。如果你想明确使用那个,那么你需要明确地选择它。(几乎所有 Excel 中使用的格式都是自定义格式,它们呈现您指定的方式,但有一些特殊格式)
POI has the full list of special, built in formats (which is much smaller than the list of formats in the dropdowns in recent copies of Excel!) in BuiltinFormats. If you make sure you set one of those, and not your own custom format string, it should behave exactly as you expect
POI 在BuiltinFormats 中有完整的特殊内置格式列表(比最近的 Excel 副本中的下拉列表中的格式列表小得多!)。如果您确保设置其中之一,而不是您自己的自定义格式字符串,它的行为应该完全符合您的预期
回答by Lowb
Don't really know if you solved this already or not but here's a code that would solve it. It actually solved my problem.
不知道你是否已经解决了这个问题,但这里有一个可以解决它的代码。它实际上解决了我的问题。
CreationHelper createHelper = wb.getCreationHelper();
...
cs.setDataFormat(createHelper.createDataFormat().getFormat("yourformat"));
Taken from http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells
取自 http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells
回答by Marcus
I understand your problem. I resolved using DateUtil of Apache Poi.
我理解你的问题。我使用 Apache Poi 的 DateUtil 解决了这个问题。
Cell cell = yourRow.createCell(yourIndex);
cell.setCellValue(DateUtil.getExcelDate(yourDate);
Enjoy !!
享受 !!
回答by Arjun Marati
It is resolved using below:
使用以下方法解决:
DataFormatter fmt = new DataFormatter();
Object Excel1data=Excel.readExcel(Excel1, "Sheet3", rownumber, columnnumber1);
String valueAsInExcel1 = fmt.formatCellValue((Cell) Excel1data);
Object Excel2Data=Excel.readExcel(Excel2, "Sheet2", getrownumber, columnnmumber2);
String valueAsInExcel2 = fmt.formatCellValue((Cell) Excel2Data);
org.testng.Assert.assertEquals(valueAsInExcel1, valueAsInExcel2,"Incorrect data in RowNumber:"+getrownumber+" ColumnNumber:"+columnnmumber2);
columnnumber1++;