?
Solved

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

Posted on 2011-09-27
11
Medium Priority
?
523 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 2000 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
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 111

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 111

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

770 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