Current location: Hot Scripts Forums » Programming Languages » PHP » Export Mysql to Excel

Export Mysql to Excel

Closed Thread
  #1 (permalink)  
Old 07-11-05, 08:01 AM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Export Mysql to Excel

Ok I am using php and mysql and I would like to know of any scripts where I can export mysql into an excel file.

Basically this is whats happening. I am making a company website. People can upload pictures to the server and the filename gets entered into the database. What I wanted to do as well is when a user decides to view something in excel file, I also wanted so the image will also appear in the excel file when its being export from mysql. Is this possible. I made a page where the admin can create catalogs for clients.
__________________
Visit my Online Blog
http://www.robslounge.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #2 (permalink)  
Old 07-11-05, 10:27 AM
snaip's Avatar
snaip snaip is online now
iNET Interactive
 
Join Date: Nov 2003
Posts: 975
Thanks: 1
Thanked 3 Times in 3 Posts
Here's a tutorial to get you started:
http://www.phpfreaks.com/tutorials/114/0.php
__________________
Regards,
Ahmad Permessur
Team HotScripts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #3 (permalink)  
Old 07-11-05, 11:42 AM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
That script doesnt work
__________________
Visit my Online Blog
http://www.robslounge.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #4 (permalink)  
Old 07-11-05, 11:56 AM
DetroitGuy DetroitGuy is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Exporting to the binary format that Excel uses isn't possible as far as I know, but you can export it to a tab delimited text file that will open in Excel.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #5 (permalink)  
Old 07-11-05, 12:32 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
hey there....

What do you mean by binary format. Anyways with that script that I checked out in phpfreaks, the excel file shows nothing.. It just shows a blank file
__________________
Visit my Online Blog
http://www.robslounge.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #6 (permalink)  
Old 07-11-05, 01:04 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,029
Thanks: 14
Thanked 34 Times in 33 Posts
Try this

Try this, you'll probably need to hack it a bit but it works perfectly for me.

Code:
/*
--Dynamic Excel or Word File from MySQL--
php-doc-xls-gen for php/MySQL: (.doc or .xls dumper):

This script takes the contents of a MySQL table and dumps it to
either a dynamically-generated MS Word File (file with ending '.doc')
or a dynamically-generated MS Excel File (file with ending '.xls').

Prerequisites:  You Must have MS Word and/or MS Excel installed on
the same computer as your web browser for this to work (although
the script can be placed on any Unix/Linux server, you have to access it using
a browser on a Windows machine with either Word or Excel installed).

How to use:
1)edit the MySQL Connection Info below for your MySQL connection
  & for the name of the MySQL table that you would like to make the dump for
2)save this file somewhere on your server
3)link to this file from another page:
  a)for Word dumps:
    <a href="this_file_name.php?w=1">link to word dump</a>
  b)for Excel dumps:
    <a href="this_file_name.php">link to excel dump</a>
  --or else--
  create a Bookmark to this page (include any of the optional parameters
  described below as part of the query string for the bookmarked URL)
4)how to reuse this code to create a dump for ANY MySQL table on your server:
  a)comment-out this line below under MySQL Connection Info:
    //$DB_TBLName = "your_table_name";
  b)include the name of your MySQL table in links (or bookmarks) to this page
    as an extra parameter:
    ie: for word dump--
    <a href="this_file_name.php?w=1&DB_TBLName=your_table_name">link to word dump</a>
    ie: for excel dump--
    <a href="this_file_name.php?DB_TBLName=your_table_name">link to excel dump</a>
  c)all of the above also holds true for the name of the Database:
    you could pass along the name of the Database as a parameter to this script
	in order to use it on many different databases on your server:
	comment out //$DB_DBName = "your database"; in this script below 
	and then link to this file like:
	"this_file_name.php?$DB_DBName=your_database&DB_TBLName=your_table_name..."
5)if you're resourceful, you could also pass the sql statement to be used for this
  script as a parameter: "this_file_name.php?sql=..."
  but you might have to URL-ENCODE your sql statement before passing it to this script,
  and then URL-DECODE it in the beginning of this script for it to work.

To change the formatting of the Word or Excel File generated:
change the respective parts of the coding for the word or the excel file that format
the database info sent to the browser.  Most useful for this are the escape characters
for tabs ('\t') & line returns ('\n').  Experiment with these until you get the formatting
that you desire.

If you're going to be using this script with SSL, please see the comments marked
'A NOTE ABOUT USING THIS SCRIPT WITH SSL' found below!

This code is freeware (GPL).  Please feel free to do with it what you'd like.
Comments, bugs, fixes to:
churmtom@hotmail.com

Originally: Nov. 25th, 2001
Updated:    May  12th, 2002
Updated:    July  1st, 2002
Updated:    Jan	 19th, 2003 - SSL Fix for MSIE

Thanks to Josue & Steven d.B. for helping point out
improvements for this code!
*/

//EDIT YOUR MySQL Connection Info:
$DB_Server = "localhost";    	//your MySQL Server 
$DB_Username = ""; 		//your MySQL User Name 
$DB_Password = "";    	//your MySQL Password 
$DB_DBName = "";    		//your MySQL Database Name 


// get the right table/year
if(substr($table, 0, 3) == "ev_"){$table = "$ev_year";} 
if(substr($table, 0, 3) == "jn_"){$table = "$jrn_year";} 
if(substr($table, 0, 3) == "cs_"){$table = "$cs_year";} 

 $DB_TBLName = "$table";   		//your MySQL Table Name 


//$DB_TBLName,  $DB_DBName, may also be commented out & passed to the browser
//as parameters in a query string, so that this code may be easily reused for
//any MySQL table or any MySQL database on your server

//DEFINE SQL QUERY:
//you can use just about ANY kind of select statement you want - 
//edit this to suit your needs!


// switch column name if exporting the 'pm_post' table.
if ($table == "pm_post"){
	$userkey = "groupname";
}else{
	$userkey = "uemail";
}

// if column name exists, export it
 if ($table != ""){

  $DB_TBLName = "$table"; 
  $sql = "Select * from $DB_TBLName WHERE $userkey = '$userkey'";
 }else{
  $DB_TBLName = "$trk"; 
  $sql = "Select * from $DB_TBLName";  

 }


//Optional: print out title to top of Excel or Word file with Timestamp
//for when file was generated:
//set $Use_Titel = 1 to generate title, 0 not to use title
$Use_Title = 1;
//define date for title: EDIT this to create the time-format you need
$now_date = date('m-d-Y H:i');
//define title for .doc or .xls file: EDIT this if you want
$title = "Data from the '$ttype' for account #$userID. Exported from the XXXXXXXXXXXXX System on $now_date";


/*
Leave the connection info below as it is:
just edit the above.

(Editing of code past this point recommended only for advanced users.)
*/
//create MySQL connection
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
    or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
    or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect)
    or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());

//if this parameter is included ($w=1), file returned will be in word format ('.doc')
//if parameter is not included, file returned will be in excel format ('.xls')
if (isset($w) && ($w==1)){
$file_type = "msword";
$file_ending = "doc";
}
else {
$file_type = "vnd.ms-excel";
$file_ending = "xls";
}
//header info for browser: determines file type ('.doc' or '.xls')
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=$ttype.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");

/*
A NOTE ABOUT USING THIS SCRIPT WITH SSL:
==============================================
To get this script to work properly in MSIE,
do the following:

//delete this header:
header("Pragma: no-cache");

//and add these headers just after the "Expires: 0" header:
header("Keep-Alive: timeout=15, max=100");
header("Connection: Keep-Alive");         
header("Transfer-Encoding: chunked");

Thanks to Christopher Owens for this!
==============================================
*/

/*    Start of Formatting for Word or Excel    */

if (isset($w) && ($w==1)) //check for $w again
/*    FORMATTING FOR WORD DOCUMENTS ('.doc')   */
{
//create title with timestamp:
if ($Use_Title == 1){
echo("$title\n\n");
}
//define separator (defines columns in excel & tabs in word)
$sep = "\n"; //new line character

    while($row = mysql_fetch_row($result))
    {
        //set_time_limit(60); // HaRa
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
        //define field names
        $field_name = mysql_field_name($result,$j);
        //will show name of fields
        $schema_insert .= "$field_name:\t";
            if(!isset($row[$j])) {
                $schema_insert .= "NULL".$sep;
                }
            elseif ($row[$j] != "") {
                $schema_insert .= "$row[$j]".$sep;
                }
            else {
                $schema_insert .= "".$sep;
                }
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        //end of each mysql row
        //creates line to separate data from each MySQL table row
        print "\n----------------------------------------------------\n";
}
}
else
/*    FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
{
//create title with timestamp:
if ($Use_Title == 1){
echo("$title\n");
}
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character

//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
//end of printing column names

//start while loop to get data
/*
note: the following while-loop was taken from phpMyAdmin 2.1.0.
--from the file "lib.inc.php".
*/
    while($row = mysql_fetch_row($result))
    {
        //set_time_limit(60); // HaRa
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
            if(!isset($row[$j]))
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
            else
                $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
		//following fix suggested by Josue (thanks, Josue!)
		//this corrects output in excel when table fields contain \n or \r
		//these two characters are now replaced with a space
		$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "\n";
    }
}
?>
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #7 (permalink)  
Old 07-11-05, 02:35 PM
mcrob mcrob is offline
Coding Addict
 
Join Date: Jul 2004
Posts: 266
Thanks: 0
Thanked 0 Times in 0 Posts
Hey there...

I know exactly where you got that script from...

http://fundisom.com/phparadise/php/d...mySQL_to_excel

It works great tho! What Im trying to do is this for example

id | movies | games | title

in the excel file Im trying to add more spaces in the field names.
__________________
Visit my Online Blog
http://www.robslounge.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #8 (permalink)  
Old 07-12-05, 05:09 AM
bannme bannme is offline
Newbie Coder
 
Join Date: Jun 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
I donno this one is what you want or not, I get is from
Code:
http://www.weberdev.com/get_example-3701.html
Quote:
So you want to allow your users to get some data directly into their excel.... no problem.

All you need to do it organize your data in rows where each data field is delimited by a comma or
some other acceptable delimiter and send the correct header to the browser.

Suppose you want to send something like :

Car Type Car Model Number of Cars
Mazda 323 5
Mazda 6 4
Mazda MPV 6


all you need to do is :
PHP Code:
<? 
header 
("Content-type: application/csv\nContent-Disposition: \"inline; filename=Report.csv\""); 
Echo
"Car Type,Car Model,Number of Cars\n"
Echo
"Mazda,323,5\n"
Echo
"Mazda,6,4\n"
Echo
"Mazda,MPV,6\n"
?>
I recommand that you better check out their site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
  #9 (permalink)  
Old 07-12-05, 06:49 AM
klyau23 klyau23 is offline
New Member
 
Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Converting Excel to Mysql

Hi All,

Wondering on how to convert excel spreadsheet to MYSQL database easily without incurring any cost? Visit this web site

http://excel2mysql.f2g.net

By following simple and intuitive steps, you will be able to get a mysql database. But, get a free copy of phpMyadmin from the internet first. It's simple to get it, just type phpmyadmin at the google search engine or visit http://www.phpmyadmin.net/home_page/.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Closed Thread

Bookmarks


Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
When I used "application/vnd.ms-excel" to export table's content to Excel file. It wo miaomiaoga ASP 2 08-02-07 08:53 AM
excel to mysql buzzby PHP 5 10-13-05 08:56 AM
click in image then export to excel angela ASP 3 09-02-05 06:53 PM
UPDATE: MySQL Auto Backup & Export v1.1 Beyonder General Advertisements 2 03-21-05 02:05 PM
Export to excel file asp traceMe ASP 2 08-01-04 09:07 PM


All times are GMT -5. The time now is 09:10 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.