Mingo
Mingo

Reputation: 113

ColdFusion: cfspreadsheet localized date format

My question is basically the same as this one, but I'm not in a position to change the date format in the Excel file:

If I format the cell as date english (NZ), ie Date Type "*14/03/2001", it displays right in the spreadsheet, but when I try to upload it switched the day and month. But If I change the format to a custom "dd/mm/yyyy" format [it works fine].

So the question is this: Can I change the way <cfspreadsheet /> handles date formats? Or even better, get a Date object directly from the Excel import.

EDIT:

I found a solution by using POI:

<cfset fileIS = createObject( "java", "java.io.FileInputStream" ).init( "#request.site.sImportPath#\#variables.file#" ) />
<cfset excelFS = createObject( "java", "org.apache.poi.poifs.filesystem.POIFSFileSystem" ).init( fileIS ) />
<cfset workBook = CreateObject( "java", "org.apache.poi.hssf.usermodel.HSSFWorkbook" ).init( excelFS ) />
<cfset sheet = workBook.getSheet( "mySheetName" ) />

<cfset myDateValue = sheet.getRow( 20 ).getCell( 2 ).getDateCellValue() />

When using getDateCellValue() you get the actual date as a usable ColdFusion date back. It would've been nice if <cfspreadsheet /> did this natively.

Upvotes: 4

Views: 888

Answers (1)

Mingo
Mingo

Reputation: 113

I found a solution by using POI:

<cfset fileIS = createObject( "java", "java.io.FileInputStream" ).init( "#request.site.sImportPath#\#variables.file#" ) />
<cfset excelFS = createObject( "java", "org.apache.poi.poifs.filesystem.POIFSFileSystem" ).init( fileIS ) />
<cfset workBook = CreateObject( "java", "org.apache.poi.hssf.usermodel.HSSFWorkbook" ).init( excelFS ) />
<cfset sheet = workBook.getSheet( "mySheetName" ) />

<cfset myDateValue = sheet.getRow( 20 ).getCell( 2 ).getDateCellValue() />

When using getDateCellValue() you get the actual date as a usable ColdFusion date back. It would've been nice if did this natively.

Upvotes: 2

Related Questions