472,122 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

PHP to create CSV that Excel can read

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') != 0 && $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') != 0 && $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;

}

}
[/PHP]

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
Jul 17 '05 #1
9 21753
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

Jul 17 '05 #2
.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/media-types/application/>
application/msexcel is not a registered MIME type.

Micha
Jul 17 '05 #3
Michael Fesser wrote:
.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/media-types/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

Jul 17 '05 #4
In article <df********************************@4ax.com>,
Michael Fesser <ne*****@gmx.net> wrote:
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/media-types/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...

Jul 17 '05 #5
ph**************@gmail.com (Phil Powell) wrote in message news:<b5*************************@posting.google.c om>...
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
Jul 17 '05 #6
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
Jul 17 '05 #7
ng**********@rediffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<ab**************************@posting.google. com>...
ph**************@gmail.com (Phil Powell) wrote in message news:<b5*************************@posting.google.c om>...
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.


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
Jul 17 '05 #8
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" <us*********@john.dunlop.name> wrote in message
news:MP************************@News.Individual.NE T...
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

Jul 17 '05 #9
ph**************@gmail.com (Phil Powell) wrote in message news:<b5*************************@posting.google.c om>...
<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.


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

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Steve Richter | last post: by
reply views Thread by Johnny Fugazzi | last post: by
27 posts views Thread by jeniffer | last post: by
4 posts views Thread by =?Utf-8?B?UHJpeWE=?= | last post: by
11 posts views Thread by =?Utf-8?B?UGV0ZXIgSw==?= | last post: by
4 posts views Thread by =?Utf-8?B?Sm9zaW4gSm9obg==?= | last post: by
15 posts views Thread by patf | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.