For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2004 > PHP to create CSV that Excel can read









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PHP to create CSV that Excel can read
Phil Powell

2004-11-15, 8:55 pm

I am using PHP 4.3.2 to create a CSV file, however, Excel constantly
views it as a single-column spreadsheet with everything in quotes,
whereas OpenOffice Calc views it as a legitimate spreadsheet in
separate columns/cells.

PHP:

class ReportGenerator {

/**
* Generate content based upon type
*
* @access private
* @param mixed $type type of content (e.g. Excel, CSV, Word, etc)
* @return mixed formatted content
*/
function &generateContent($type) {                            // STATIC STRING METHOD
$result $this->getResult();

switch (
strtolower($type)) {
case 
'excel':
$delimiter '</td><td valign="top">';
$lb "\n</tr>\n<tr>";
break;
case 
'csv':
$delimiter ',';
$lb "\n";
break;
default: 
// DO NOTHING
break;
}

if (
$result && strcmp(strtolower($type), 'csv') != 0$content =
'<table><tr>';
if (
$result$content .= $this->generateHeaders($type) . $lb;

$isFirstRow true$isEndOfFirstRow true;

for (
$i 0$i < @sizeof($result); $i++) {
$header =  @array_keys(get_object_vars($result[$i])
);
if (
strcmp(strtolower($type), 'csv') != && $isFirstRow) {
$isFirstRow false;
$field '<td valign="top">';
}
for (
$j 0$j < @sizeof($header); $j++) {
if (
strcmp(strtolower($type), 'csv') == 0) {
$field .= '"' nl2br(preg_replace('/\r/'"\n",
preg_replace('/[\t]/'' 'str_replace('"''"',
$result[$i]->$header[$j])))) . '"';
} else {
$field .= nl2br(preg_replace('/\r/'"\n"preg_replace('/[\t]/',
' '$result[$i]->$header[$j])));
}
$content .= $field $delimiter;
$field '';
}
if (
strcmp(strtolower($type), 'csv') != && $isEndOfFirstRow) {
$isEndOfFirstRow false;
$content preg_replace('/<td valign="top">$/'''$content);
}
$content .= $lb;
$field '';
$isFirstRow true$isEndOfFirstRow true;
}

if (
$result && strcmp(strtolower($type), 'csv') != 0$content =
preg_replace('/<tr>$/'''$content) . '</table>';

return 
$content;

}

}



This class contains a method that will generate the CSV-formatted
content, however, it again fails to produce legitimate content in
Excel while producing just-fine content in OpenOffice.

Help!

Thanx
Phil
Sacs

2004-11-15, 8:55 pm

Phil Powell wrote:
> I am using PHP 4.3.2 to create a CSV file, however, Excel constantly
> views it as a single-column spreadsheet with everything in quotes,
> whereas OpenOffice Calc views it as a legitimate spreadsheet in
> separate columns/cells.
>

*snip*
> This class contains a method that will generate the CSV-formatted
> content, however, it again fails to produce legitimate content in
> Excel while producing just-fine content in OpenOffice.
>
> Help!
>
> Thanx
> Phil


Are you sending the appropriate http headers? I've used:

header ("Content-Type: application/msexcel");
header ("Content-Disposition: attachment; filename=\"filename.csv\"");

followed by a stream of comma separated text and it just works, in both
excel and oo.o

Be aware that you DONT want the first bytes in the csv to be "ID", Excel
treats the file as a .slk no matter what you tell it it really is. (THAT
took me a while to figure out! Doh!)

Sacs

Michael Fesser

2004-11-15, 8:55 pm

.oO(Sacs)

>Are you sending the appropriate http headers? I've used:
>
>header ("Content-Type: application/msexcel");


Shouldn't that be application/vnd.ms-excel instead?

According to <http://www.iana.org/assignments/med...es/application/>
application/msexcel is not a registered MIME type.

Micha
Sacs

2004-11-15, 8:55 pm

Michael Fesser wrote:
> .oO(Sacs)
>
>
>
>
> Shouldn't that be application/vnd.ms-excel instead?
>
> According to <http://www.iana.org/assignments/med...es/application/>
> application/msexcel is not a registered MIME type.
>
> Micha


Err, yes, it probably should be vnd.ms-excel :-)

application/msexcel does work though, for oo.o too, and thats the only
requirement I had at the time...

Thanks for pointing that out!

Sacs

Michael Vilain

2004-11-16, 3:55 am

In article <dfhip0lepgikfd8phfid9mb2l0tjao64me@4ax.com>,
Michael Fesser <netizen@gmx.net> wrote:

>
> Shouldn't that be application/vnd.ms-excel instead?
>
> According to <http://www.iana.org/assignments/med...es/application/>
> application/msexcel is not a registered MIME type.


THANK YOU for this URL! It's a great mystery to me if I'm using the
right mime type when I download things. This cleared up a lot. I do
note that Macintosh IE tends to use "octetstream" while all the other
browsers use "octet-stream". Anyone know why?

--
DeeDee, don't press that button! DeeDee! NO! Dee...



R. Rajesh Jeba Anbiah

2004-11-16, 8:55 am

phillip.s.powell@gmail.com (Phil Powell) wrote in message news:<b5476f2f.0411151426.4f947f0@posting.google.com>...
> I am using PHP 4.3.2 to create a CSV file, however, Excel constantly
> views it as a single-column spreadsheet with everything in quotes,
> whereas OpenOffice Calc views it as a legitimate spreadsheet in
> separate columns/cells.
>
> [PHP]

<snip>
> if (strcmp(strtolower($type), 'csv') == 0) {
> $field .= '"' . nl2br(preg_replace('/\r/', "\n",
> preg_replace('/[\t]/', ' ', str_replace('"', '"',
> $result[$i]->$header[$j])))) . '"';



I have no clue, why you use nl2br() here. Also, what's the point in
str_replace('"', '"',..) ??

Perhaps you may want to try <http://in2.php.net/fputcsv> (CVS?) or
<http://in2.php.net/fgetcsv#14788>. The later worked very well for me
in many cases.

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
John Dunlop

2004-11-16, 8:55 am

Michael Vilain wrote:

> I do note that Macintosh IE tends to use "octetstream" while
> all the other browsers use "octet-stream". Anyone know why?


'Octetstream' is an unregistered subtype of the application
top-level type, and should therefore be prefixed with 'x-';
'octet-stream' is the registered subtype. The former is
more likely to cause the browser to ask its user what to do.

Note that browsers use whatever MIME media type they're
given, unless they're trying to guess the media type of a
file to be uploaded.

--
Jock
Phil Powell

2004-11-16, 3:55 pm

ng4rrjanbiah@rediffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<abc4d8b8.0411160003.4febd736@posting.google.com>...
> phillip.s.powell@gmail.com (Phil Powell) wrote in message news:<b5476f2f.0411151426.4f947f0@posting.google.com>...
> <snip>
>
>
> I have no clue, why you use nl2br() here. Also, what's the point in
> str_replace('"', '"',..) ??
>
> Perhaps you may want to try <http://in2.php.net/fputcsv> (CVS?) or
> <http://in2.php.net/fgetcsv#14788>. The later worked very well for me
> in many cases.


Because. according to http://www.php.net, "fputcsv" is still in CVS,
and fgetcsv is useless to me since I'm doing a conversion to csv.

This works just fine again when opened in OpenOffice, but comes across
formatted incorrectly in Excel XP. I was told, however, you could
import the CSV instead.

Phil
John Sjouken

2004-11-16, 6:53 pm

Just maybe a stupid remark, but if you go to:
start => settings => control panel => regional options => numbers =>
list eperator and add ; (semicolon)you get it right

"John Dunlop" <usenet+2004@john.dunlop.name> wrote in message
news:MPG.1c03d3f44c8691ae9897c1@News.Individual.NET...
> Michael Vilain wrote:
>
>
> 'Octetstream' is an unregistered subtype of the application
> top-level type, and should therefore be prefixed with 'x-';
> 'octet-stream' is the registered subtype. The former is
> more likely to cause the browser to ask its user what to do.
>
> Note that browsers use whatever MIME media type they're
> given, unless they're trying to guess the media type of a
> file to be uploaded.
>
> --
> Jock



R. Rajesh Jeba Anbiah

2004-11-23, 3:56 pm

phillip.s.powell@gmail.com (Phil Powell) wrote in message news:<b5476f2f.0411160840.e182400@posting.google.com>...
<snip>
>
> Because. according to http://www.php.net, "fputcsv" is still in CVS,
> and fgetcsv is useless to me since I'm doing a conversion to csv.


Did you click both the links? *Read* the contents of the links?

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com