Button to export table to excel file and csv file and button to print table


I have a table generated from mySQL database and would like to create 3 buttons on that web page:
1. To print web page with print preview given
2. To export table to excel file
3. To export table to csv file

I have tried numerous functions found on the web and none seem to work. Please could someone show some light on to this?!

Many Many Thanks in Advance!!
Who is Participating?
h3rm1t9536Connect With a Mentor Author Commented:
I got the code to export to cvs from http://davidvielmetter.com/tricks/howto-convert-an-html-table-to-csv-using-php/

Thanks so much for all your advise though!!
mustang83Connect With a Mentor Commented:
to export to Excel view this page. Its a good example.



To export to csv in php see code below

header ("Content-type: application/csv\nContent-Disposition: \"inline; filename=yourfilename.csv\""); 

$query = "SELECT... 

$result = mysql_query($query) or die("Query failed : " . mysql_error()); 
echo "ID,Name,Age\r\n"; //header 
while($row = mysql_fetch_row($result)){ 
echo "$row[0],$row[1],$row[2]\r\n"; //data 

Open in new window


To print you use javascript

<input type="button" value="Print this page" onClick="window.print()"> or <a href="javascript:window.print()">Print this page</a>

You cannot print preview as this is a function in a browser and is different in each.
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

h3rm1t9536Author Commented:
HI mustang83

For the export to csv I would like to create a button that will create the file when clicked... how would I be able to do that? What would be the stucture of the functions exactly and how would I call them?

For the print I tried that before but it only prints the text on the page it doesn't keep the style (html and css) of the page. Basically I have a table which calls all the rows from my database and displays them nicely, but when I try to print using your function it only prints the text not the table or the colors or structure is kept... Do you know of a function that can print the page exactly as the end user see's it?

Thanks so much for your help!

Hi tapanpattanaik

Sorry but I dont know C# or ASP.NET - only know php, JavaScript, SQL and html any suggestions in those languages would be helpful - Many Thanks for trying to help though
Just create another page with the above code into it. with your queries etc. Then just link your button to this page. It will just throw up a download/save as dialog box which will allow the user to download the csv.

ok the print problem is a problem because the function above it essentially exactly the same as the user click print on the browser. If you are losing formatting then that due to the page not being printer friendly. What i have down to get away with that is to create another page with again does all your queries and formats it together so its print friendly.

I would then use the a button to pop up a small window which opens your new page. Your new page has script in it which will print the page and close after it is finished.

You need to play around with the print button in your browser to make the page printer friendly.
Ray PaseurCommented:
The .csv file type is almost universally associated with Excel.  So if you write your file to a CSV you have covered at least two of the issues completely.
h3rm1t9536Author Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for h3rm1t9536's comment http:/Q_27343078.html#36715860

for the following reason:

I got the perfect solution from a website... The link is above - which explained the perfect way of how to export data to csv.
The author asked for information on how to export to excel and how to print as well as how to export to csv.

We gave him answers for all his problems. I have personally tried all the answers other than the c solution and they work.

I dont think its fair for us to spend our time trying to help without any recognition or points.
Ray PaseurCommented:
Did you mean CVS or CSV?  These are both terms of art and completely different things.
Starting auto-close process to implement the recommendations of the participating Expert(s).
EE Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.