Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set cell value to Date and apply default Excel date format?

I've been using Apache POI for some time to read existing Excel 2003 files programmatically. Now I have a new requirement to create entire .xls files in-memory (still using Apache POI) and then write them to a file at the end. The only problem standing in my way is the handling of cells with dates.

Consider the following code:

Date myDate = new Date(); HSSFCell myCell; // code that assigns a cell from an HSSFSheet to 'myCell' would go here... myCell.setCellValue(myDate); 

When I write the workbook containing this cell out to a file and open it with Excel, the cell is displayed as a number. Yes, I do realize that Excel stores its 'dates' as the number of days since January 1 1900 and that is what the number in the cell represents.

QUESTION: What API calls can I use in POI to tell it that I want a default date format applied to my date cell?

Ideally I want the spreadsheet cell to be displayed with the same default date format that Excel would have assigned it if a user had manually opened the spreadsheet in Excel and typed in a cell value that Excel recognized as being a date.

like image 279
Jim Tough Avatar asked Apr 26 '11 18:04

Jim Tough


People also ask

How do I change the permanent date format in Excel?

Change the date system in Excel You can change the date system by doing the following: Click File > Options > Advanced. Under the When calculating this workbook section, select the workbook that you want, and then select or clear the Use 1904 date system check box.

How do I change the default date format to MM DD YYYY in Excel?

If you want to change the date format for a specific cell or range of cells, you can do so by selecting the cells and then going to the 'Format Cells' dialog box. In the 'Number' tab, select 'Date' from the list of options and then choose the desired format from the 'Type' drop-down menu.

How do I lock a cell for a date format?

Use data validation in the cell range to allow only a date to be entered. Choose the Data tab and then select Data Validation/Allow/Date/Between/Enter Starting Date/Enter Finishing Date. While the dialogue box is open add an import message and an error message. Make sure that your error message is set to Stop.

How do I change the default time format in Excel?

Changing Windows Date / Time Defaults ► In all versions of Windows, access the Control Panel. Then: For Windows 10 and 8, under Clock, Language and Region, click Change date, time, or number formats. For Windows 7 and some older versions, from the Control Panel, click Region and Language or similar.


1 Answers

http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells

CellStyle cellStyle = wb.createCellStyle(); CreationHelper createHelper = wb.getCreationHelper(); cellStyle.setDataFormat(     createHelper.createDataFormat().getFormat("m/d/yy h:mm")); cell = row.createCell(1); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); 
like image 54
ninja Avatar answered Oct 27 '22 00:10

ninja