Solved

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

Posted on 2011-09-27
11
518 Views
Last Modified: 2012-08-14
Hello!

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!!
Chelsea
0
Comment
Question by:h3rm1t9536
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 4

Assisted Solution

by:mustang83
mustang83 earned 500 total points
ID: 36708399
to export to Excel view this page. Its a good example.

http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8


.......


To export to csv in php see code below

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



$query = "SELECT... 
    FROM...."; 

$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.
0
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 36708438
0
 

Author Comment

by:h3rm1t9536
ID: 36708665
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:mustang83
ID: 36708911
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36713899
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.
0
 

Accepted Solution

by:
h3rm1t9536 earned 0 total points
ID: 36715860
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!!
0
 

Author Comment

by:h3rm1t9536
ID: 36716065
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.
0
 
LVL 4

Expert Comment

by:mustang83
ID: 36716066
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36716379
Did you mean CVS or CSV?  These are both terms of art and completely different things.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 36902298
Starting auto-close process to implement the recommendations of the participating Expert(s).
 
modus_operandi
EE Admin
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

688 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