Forum Moderators: coopster

Message Too Old, No Replies

Trouble with a force-downloaded excel file

excel keeps chopping off initial zeros in cells

         

louponne

12:58 am on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



okay, this is a continuation of a thread [webmasterworld.com] where jatar_k nicely helped me understand how to force-download an excel file via php.

I have the script working fine. But if a variable that does into a cell is only digits and has initial zeros, as in a zip-code such as "00123", then excel "assumes" it's a nmuber, and chops off those zeros. The data sent to excel all has quotes around it, and I just can't figure out how to avoid this other than to add a letter to that field, but a zip code of "x00123" just isn't the best-looking solution.

I've tried opening up the file directly via excel, using different file options, no go.

Anyone run into this before and figure out a workaround?

jatar_k

5:01 am on Jan 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



unfortunately it is primarily excels problem

whenever I work with numbers that need leading zeros I use csv files and open them in a text editor. I have had some other problems with excel randomly formatting what it interprets as numbers. It really is a pain and I haven't yet found a work around for it, aside from not using excel. ;)

dmorison

5:10 am on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using a leading apostrophe - eg, "'00005"

It certainly works if you're just typing straight into Excel - the apostophe is stripped and the cell will be treated as text format even if all characters are digits.

Stripeman

8:54 am on Jan 4, 2005 (gmt 0)

10+ Year Member



may i see your code you have for the 'force download'?

IM sure that your using something far more sophisticated.. but this is what im using and I have no probs...

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache"); // HTTP/1.0

header("Content-Type: application/vnd.ms-excel");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
?>

louponne

3:50 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using a leading apostrophe - eg, "'00005"

Thanks, but nope - I just tried and I do get the zeros, but I also get the apostrophe :(

may i see your code you have for the 'force download'?

You bet:

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename.".xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $data;

Hanu

4:48 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



Use
"=\"$value\""
when writing the value. For example,
="00002"
It must be double quotes, single quotes don't work.

louponne

12:17 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, Hanu, but yes, I'm already doing that:

$item = '"' . $item . '"' . "\t";

So I'm afraid that's not the solution :(

claus

12:53 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> "=\"$value\""

I believe the formula is:

=TEXT($value;"00000")

(perhaps "text" is "format" - i recall that from earlier excel versions)

louponne

5:42 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for sticking with me on this one, everyone!

I believe the formula is:

=TEXT($value;"00000")

(perhaps "text" is "format" - i recall that from earlier excel versions)

I'm a bit confused with that one - isn't that Excel Basic? I'm writing to this "pseudo-file" using php, so I'm not sure how I could use =TEXT($value;"00000") or similar...

Stripeman

6:51 am on Jan 5, 2005 (gmt 0)

10+ Year Member



louponne ...

Does my example work at all? or do you have the same prob with it?

Hanu

10:27 am on Jan 5, 2005 (gmt 0)

10+ Year Member



Thanks, Hanu, but yes, I'm already doing that:

$item = '"' . $item . '"' . "\t";

No, you're not. There is no equals sign. It should be

$item = '="' . $item . '"' . "\t";

Sometimes reading posts properly helps, you know. I wouldn't have posted, if I hadn't tried it out.

claus

10:45 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> I'm writing to this "pseudo-file" using php, so I'm not sure how I could use =TEXT($value;"00000") or similar...

You could write that to the cell in stead of the raw $value. It's an Excel formula that means that the cell will always have five numbers, even when the first ones are zeroes. It will show up in the spreadsheet as text formatted numbers, like this:

00001, 00012, 00123, 01234, 12345

Hanus method will do exactly the same (with the extra equals sign), only, with that method you have to put the leading zeroes inside the quotes. With the formula, you can omit the leading zeroes and they'll be inserted by the spreadsheet (This might not be good if you require that the leading zeroes exist physically in the cell - if you need that, you should make them part of $value vith both methods).

Hanu

6:13 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



Thanks claus, for your addition. The original poster doesn't seem to care anymore, though. Sigh!

louponne

1:42 am on Jan 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, what do you mean I don't care?! I'm just not connected 100% of the time! :(

Many thanks, *both* of you for your help and patient explanations - I'll get to implementing that quickly! :)

louponne

2:48 am on Jan 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tried the equal sign, and yes, I get 01234 in the excel cell, but the actual content of the cell is
="01234"

it's a bit disconcerting to see that in the cell, but it seems to work in, say, an MSWord mail-merge. I'll have to determine if there are other issues involved with the cell displaying the numbers but having actually other content.

Thanks again everyone for your help; sorry I wasn't quicker at responding! :)

Hanu

11:57 am on Jan 7, 2005 (gmt 0)

10+ Year Member



The equals sign indicates an excel formula for which a different syntax applies, e.g. text strings are indicated by quotation marks. The only issue I can think of is users typing in new values. As they would type them without the = sign, any leading zeroes will be stored in the cell but not displayed. Other than that, I think ="00001" is equivalent to 00001.

Also, don't worry about responding slowly. That wasn't my concern. It's just that I usually put a little effort into answering a question. Although my answer posts may be short, they are still valid. If they get ignored or not read properly I can sometimes get a bit grumpy. I'll buy you a beer sometime, ok?

louponne

7:01 pm on Jan 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks again for the explanation, Hanu :)

and yep, a good beer sounds great :)