Solved

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

Posted on 2011-09-27
11
495 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
  • 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
 
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 108

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now