Jump to content

How would you use PHP to send MySql data to an excel spreadsheet?


DeepakJ

Recommended Posts

I want to make a table with all of my MySQL data in excel but am not quite sure about how to do it. Essentially, there will be a button on a form that you click and then excel will popup with all the information in there. Help would be greatly appreciated.

Line 1.  SELECT a,b,a+b INTO OUTFILE "/tmp/result.csv"

---> Selecting which data to process (Which Fields to collect information from)

---> Save collected data into result.csv (File)

Line 2.  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

Line 3.  LINES TERMINATED BY "\n"

---> I've never used these commands, But I assume they are for data parsing ..

---> Looks like it says: any text that may be enclosed in ' ', and seperated by , and \n signfying the end of a line.

Line 4.  FROM mytable

 

---> The Table which holds the feilds, you are collecting data from .. (See line1)

Just try adding the header function and export your data as tab delimited.

 

// The header is the most important part

header('Content-type: application/ms-excel'); header('Content-Disposition: attachment; filename=FILENAME.xls');

 

// after connecting to your DB

$rows = mysql_fetch_row($result);

 

// Return your result as tab delimited

echo $rows[0]."\t";

echo $rows[1]."\t";

echo $rows[2]."\t";

echo $rows[3]."\t";

echo $rows[4];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.