By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,204 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,204 IT Pros & Developers. It's quick & easy.

Assembling data for an .xls download file

Claus Mygind
100+
P: 571
I want to assemble data in a format which I can return as a downloadable .xls file.

I can create .csv files with no problem using "fputcsv", but the problem in using that construct for an .xls file is each row is terminated by comma.

I store a variable number of rows in an array like this
Expand|Select|Wrap|Line Numbers
  1.                     $cData[] =  $c["MRMRS"].chr(9).
  2.                                 $c["FIRST"].chr(9).
  3.                                 $c["LAST"].chr(9).
  4.                                 $c["name"].chr(9).
  5.                                 $c["addr1"].chr(9).
  6.                                 $c["addr2"].chr(9).
  7.                                 $c["city"].chr(9).
  8.                                 $c["st"].chr(9).
  9.                                 $c["zip"].chr(9).
  10.                                 $c["GIFTCOUNT"].chr(13).chr(10);
  11.  
Using the chr(9) to insert a tab between columns. Then I read the array with the fputcsv into a temp file like this
Expand|Select|Wrap|Line Numbers
  1.  
  2. fputcsv($fh, $cData);
  3.  
where $fh is my file handle to my temporary file which I then download.

The output I get is

"Mr. ......","Mr. .....","Mr. .....

Then I force the download like this.
Expand|Select|Wrap|Line Numbers
  1.                 header('Pragma: public');
  2.                 header('Expires: 0');
  3.                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  4.                 header('Cache-Control: public');
  5.                 header('Content-Description: File Transfer');
  6.                 //header('Content-Type: plain/text');
  7.                 header("Content-Type: $ctype"); 
  8.                 header('Content-Disposition: attachment; filename='.$outputFileName);
  9.                 header('Content-Transfer-Encoding: ascii');
  10.                 header('Content-Length: ' . filesize($fullOutputName));
  11.  
  12.                 @readfile($fullOutputName);
  13.  
where $fullOutputname is a reference to the csv file I created. Note that "$ctype" = "application/vnd.ms-excel"

Of course the "fputcsv" is the problem. How can I properly assemble the data other than using fputcsv? To eliminate the comma between rows.
Apr 9 '12 #1

✓ answered by Claus Mygind

It appears the real solution is to download and install phpExcel

Then it is easy to create a completed and formatted excel spread sheet.

Share this Question
Share on Google+
2 Replies


Dormilich
Expert Mod 5K+
P: 8,639
hm, isn’t the comma CSV’s row delimiter (like in Comma Separated Values)?

though you can pass any delimiter you like with fputcsv() … (as long as you pass a valid delimiter)
Apr 10 '12 #2

Claus Mygind
100+
P: 571
It appears the real solution is to download and install phpExcel

Then it is easy to create a completed and formatted excel spread sheet.
Apr 13 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.