tags:

views:

493

answers:

3

Hello!

I'm trying to write an excel file from php, and I need that some cells to have date type; do you have any ideas which is the format code for that type of cell?

I'm using the code from http://pizzaseo.com/php-excel-creator-class to generate the excel files.

Thanks.

A: 

Here's the reference document for the Excel binary format from Microsoft, this gives you all the information regarding the various cell types:

Microsoft Excel 97-2007 Binary File Format

Jon
How is that document going to help the questioner? Have you actually read it? There's no such thing as a "date" cell type. In fact that document doesn't talk in terms of "cell types" anyway. It documents a large number of record types. See page 21; that's all it says about cell contents apart from the actual record layouts. It doesn't mention "date" at all. NUMBER, RK, and MULRK records are used for floating-point numbers (*including* dates)
John Machin
I'm well aware that there isn't a date cell type thank you very much... and yes I've read it, it helped greatly when it came to POI related work, I assumed the same would apply with a PHP framework - seeing as it is the definitive source for all things Excel related. Thanks for your constructive criticism though...
Jon
oh and the negative downvote... ;)
Jon
So if you knew there was no "date cell type" why didn't you tell the OP that instead of pointing him at a large document that would not answer his question? Whether the OP is using POI/PHP/Perl/Python doesn't matter; the answer is the same, and the answer is NOT in that document. That document is NOT the definitive source to all things Excel related. It's not even the definitive source for the XLS file format; there are glaring gaps and sillinesses.
John Machin
*sigh* ok, my mistake in thinking it might help... I suggest you vent your frustration regarding the omissions to Microsoft though instead of me... : ) regards...
Jon
A: 

http://www.codeplex.com/PHPExcel might be mopre complete solution.

Csaba Kétszeri
+1  A: 

Your question is a little vague, and I know little about PHP, but I'm the maintainer of a similar Python package ... so here's an attempt to help:

Excel doesn't differentiate at the BIFF record level between numbers and dates. The only way that an XLS reader can tell whether a date was intended by the writer is to parse the "number format" that the writer associated (indirectly) with the cell in question.

I presume that you are using this Format function to set the num_format of what the package calls a "format" (which includes not only "number format" but alignment, background, borders, ...):

function setNumFormat($num_format)

$num_format should be an Excel "number format" (e.g. for a money amount you might use "$0.00"). For a date you just use whichever you prefer of the standard date formats Excel provides, or you could use a customised format like "yyyy-mm-dd hh:mm:ss" (handy when debugging).

Then you do:

worksheet->write($row, $col, $value, $format)

Where $format is as described above. For a date, $value should be a floating point day-number where 1900-03-01T00:00:00 is represented as day-number 61.0 and you should avoid earlier dates because ... [long rant omitted], and if there's a "datemode" or "1904" option available, ensure that you are using the (should be default) 0 or False or "no, thanks" option.

John Machin