?
Solved

Export php content to CSV

Posted on 2009-02-12
3
Medium Priority
?
861 Views
Last Modified: 2012-06-27
Hi I want a button on our page that exports the following line to CSV format as that is how it is generated

echo $location.",".$appt['first_name']." ".$appt['last_name'].",".$apdate.",".$aptime.",".$appttype.",".$tel.",2 <br/>";

The code generates something similar to
Home,John Smith,11/11/2009,18:30:00,Appt,07707742558,2
 
0
Comment
Question by:alex4544
3 Comments
 
LVL 5

Accepted Solution

by:
-null- earned 750 total points
ID: 23621082
Hi there

In order to get the browser to download the data as csv, you need to output http headers before you write the data.  The header you need sets the content disposition, which tells the browser it is a file download and to pop up a save as box:

header("Content-disposition: attachment; filename=myData.csv");

This php must be called BEFORE you output anything to the browser, ie. before you use echo, or output any static html.  You can choose the name of the csv file by changing 'myData.csv'. When you run this code, a dialogue will ask to open or save.  If you click open, excel or openoffice will fire up and display the data.

hth

-null-
0
 
LVL 18

Expert Comment

by:Hube02
ID: 23621115
Since you are exporting for excel, you can use something like the following. It will also do a force download of the file with the xls extension will open in Excel for import when double clicked (on windows)

This is a snippet of code from something I use to export from a database. You'll probably need to make some minor adjustments to take into account the desired file name and the text you wish to output.






    header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false); // required for certain browsers
    header('Content-Type: application/vnd.ms-excel');
    $date = date('Y_m_d');
    $filename = strtolower(str_replace(' ', '_', $site['name'])).'_'.$date.'.xls';
    header('Content-Disposition: attachment; filename='.$filename.';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.strlen($output));
    echo $output;
    exit;

Open in new window

0
 
LVL 1

Expert Comment

by:fraunholz
ID: 23621571
header('Pragma: public'); // required
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false); // required for certain browsers
    header('Content-Type: application/vnd.ms-excel');
    $date = date('Y_m_d');
    $filename = strtolower(str_replace(' ', '_', $site['name'])).'_'.$date.'.xls';
    header('Content-Disposition: attachment; filename='.$filename.';' );
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.strlen($output));
    foreach ($apptResult as $appt) {
      echo $location.",".$appt['first_name']." ".$appt['last_name'].",".$apdate.",".$aptime.",".$appttype.",".$tel.",2";
      echo "\n";
    }
    exit;
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question