Search for:
Jump to: 
Your Ad Here
Post new topic Reply to topic
Author Message
 Post subject: how to create xls file using mysql and php?
PostPosted: Thu Sep 25, 2008 5:48 am 
Offline

Joined: Fri May 09, 2008 4:10 am
Posts: 229
hi to all ,

how to create a .xls file using php and mysql database.

thanks in advance
---------------------
Anil Kumar :P


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 5:53 am 
Offline

Joined: Fri May 09, 2008 4:10 am
Posts: 229
hi ,
i got the solution for that script.


//EDIT YOUR MySQL Connection Info:
$DB_Server = "localhost"; //your MySQL Server
$DB_Username = "USER NAME"; //your MySQL User Name
$DB_Password = "PASSWORD"; //your MySQL Password
$DB_DBName = "DBNAME"; //your MySQL Database Name
$DB_TBLName = "TABLE NAME"; //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!
$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 .xls file: EDIT this if you want
$title = "Dump For Table $DB_TBLName from Database $DB_DBName 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());


//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character
$fp = fopen('products1.xls', "w");
$schema_insert = "";
$schema_insert_rows = "";
//start of printing column names as names of MySQL fields
for ($i = 1; $i < mysql_num_fields($result); $i++)
{
$schema_insert_rows.=mysql_field_name($result,$i) . "\t";
}
$schema_insert_rows.="\n";
echo $schema_insert_rows;
fwrite($fp, $schema_insert_rows);
//end of printing column names



//start while loop to get data
while($row = mysql_fetch_row($result))
{
//set_time_limit(60); // HaRa
$schema_insert = "";
for($j=1; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= strip_tags("$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 .= "\n";
//$schema_insert = (trim($schema_insert));
//print $schema_insert .= "\n";
//print "\n";

fwrite($fp, $schema_insert);
}
fclose($fp);


thanks
-------------------------
Anil Kumar :P


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 07, 2009 8:33 pm 
Offline

Joined: Tue Jul 07, 2009 8:26 pm
Posts: 1
Code works great but when open in Excel it says that products1.xls is different format from specified by file extension. Make sure not corrupt etc...

I continue and open and it does look fine.

Wonder if I way to fix so it doesnt show the error message.

(Also can columns widths be changed??)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2010 6:55 pm 
Offline

Joined: Thu Mar 04, 2010 6:50 pm
Posts: 1
asy1mpo wrote:
Code works great but when open in Excel it says that products1.xls is different format from specified by file extension. Make sure not corrupt etc...

I continue and open and it does look fine.

Wonder if I way to fix so it doesnt show the error message.

(Also can columns widths be changed??)

So,yesterday I was at my friend's birthday and saw a lot of my friend. But one of it told me about an interesting tool -repair Microsoft Excel. As he told me it recovered some of his old corrupted xls files for seconds and absolutely free. Moreover I tried it and was wondered,reason of it helped me quite easy. And showed me how it exports recovered data into a new Microsoft Excel file.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2010 5:08 am 
Offline

Joined: Fri May 09, 2008 4:10 am
Posts: 229
Hi,

This code perfectly working, once check that file contains full permissions or not , if not then it will give the errors.

Once add these line of code for download that file :

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=product1.xls");
header ('Content-Transfer-Encoding: binary');
header ('Content-Length: '.filesize('product1.xls'));
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');

Hope that problem may be solved.

Thank you,

Anil Kumar :P :P :P


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic    [ 5 posts ] 

1, 2, 3, 4, 5 ... 414
Recent topics  Replies   Views   Last post 
No new posts Add Slide Notes & Images as Logos in MS PowerPoint

by aspose » Tue May 21, 2013 4:03 am in Miscellaneous

0

6

Tue May 21, 2013 4:03 am

aspose View the latest post

No new posts Jquery ajax Pagination using php

by rajkumar » Tue Apr 19, 2011 11:14 am in Coding Repository

8

17614

Mon May 20, 2013 2:46 am

gfsdfg View the latest post

No new posts Ecommerce website development and website Design

by iweballey » Fri May 17, 2013 7:15 am in PHP-Code Help

0

260

Fri May 17, 2013 7:15 am

iweballey View the latest post

No new posts How to upgrade and migrate Joomal site

by mallikharjuna rao » Mon Jan 21, 2013 1:40 pm in Joomla

1

356

Thu May 16, 2013 9:53 am

sunitha12 View the latest post

No new posts Benefits of PHP

by hirephpexpert » Fri Sep 07, 2012 4:41 am in CakePHP

6

1593

Wed May 15, 2013 6:25 am

PrettyPrincesG View the latest post


Your Ad Here
cron