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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.