Assembling data for an .xls download file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Assembling data for an .xls download file

    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
    Code:
    					$cData[] =  $c["MRMRS"].chr(9).
    								$c["FIRST"].chr(9).
    								$c["LAST"].chr(9).
    								$c["name"].chr(9).
    								$c["addr1"].chr(9).
    								$c["addr2"].chr(9).
    								$c["city"].chr(9).
    								$c["st"].chr(9).
    								$c["zip"].chr(9).
    								$c["GIFTCOUNT"].chr(13).chr(10);
    Using the chr(9) to insert a tab between columns. Then I read the array with the fputcsv into a temp file like this
    Code:
    			
    fputcsv($fh, $cData);
    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.
    Code:
    				header('Pragma: public');
    				header('Expires: 0');
    				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    				header('Cache-Control: public');
    				header('Content-Description: File Transfer');
    				//header('Content-Type: plain/text');
    				header("Content-Type: $ctype"); 
    				header('Content-Disposition: attachment; filename='.$outputFileName);
    				header('Content-Transfer-Encoding: ascii');
    				header('Content-Length: ' . filesize($fullOutputName));
    
    				@readfile($fullOutputName);
    where $fullOutputname is a reference to the csv file I created. Note that "$ctype" = "applicatio n/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.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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)

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      It appears the real solution is to download and install phpExcel

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

      Comment

      Working...