Reputation: 315
I want to export data from mysql tables to excel sheet. I am using excel 2007. Once this code was working properly but today I am getting problem. Please guide me where I am doing wrong. I have huge data about 60,000 rows.
<?php
/*
Export MySQL to Excel using PHP & HTML tables
Author: Vlatko Zdrale, http://blog.zemoon.com
Look but don't touch :)
*/
include "mysql_connection.php";
//$dbTable = 'info'; // table name
$con=open_db_connection();
$sql = "select info_id, name, category_list.category, company_name, company_address, company_phone, date from info, city_list, category_list where city=cid and info.category=id";
$result = @mysql_query($sql) or die("Couldn't execute query:<br>".mysql_error().'<br>'.mysql_errno());
header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //define header info for browser
header('Content-Disposition:attachment; filename=information-'.date('Ymd').'.xlsx');
header('Pragma: no-cache');
header('Expires: 0');
echo '<table><tr>';
for ($i = 0; $i < mysql_num_fields($result); $i++) // show column names as names of MySQL fields
echo '<th>'.mysql_field_name($result, $i).'</th>';
print('</tr>');
while($row = mysql_fetch_row($result))
{
//set_time_limit(60); // you can enable this if you have lot of data
$output = '<tr >';
for($j=0; $j<mysql_num_fields($result); $j++)
{
if(!isset($row[$j]))
$output .= '<td> </td>';
else
$output .= "<td>$row[$j]</td>";
}
print(trim($output))."</tr>\t\n";
}
echo('</table>');
?>
Its very important please guide me. Thanks in advance.
Upvotes: 0
Views: 8212
Reputation: 212402
You will get that message because the file isn't an OfficeOpenXML xlsx file, but a file containing HTML markup with a .xlsx extension. You're telling Excel that the file is one format by the extension when it's really another, and it let's you know that the content doesn't match the extension. As long as it can read the HTML markup cleanly, it should still load successfully, but will always issue this message.
The more recent versions of Excel are more fussy about this than earlier versions.
If you want to get rid of the message, then either you rename your .xlsx as a .html file so that the extension matches the content (you'll need to import into Excel then, or moodify file associations so that html files are opened using Excel); or give it a tab-separated value file with a csv extension (that can be opened by double click), though you can't add any formatting using this option; or give it a real OfficeOpenXML .xlsx file
Upvotes: 1
Reputation: 10070
From your comment I'll guess something is messing up the markup.
In everything between <th></th>
and <tr></tr>
, try to use htmlentities
:
for ($i = 0; $i < mysql_num_fields($result); $i++) // show column names as names of MySQL fields
echo '<th>'.htmlentities(mysql_field_name($result, $i),ENT_COMPAT,"UTF-8").'</th>';
And
if(is_null($row[$j]))
$output .= '<td> </td>';
else
$output .= "<td>".htmlentities($row[$j],ENT_COMPAT,"UTF-8")."</td>";
Note that I'm assuming your encoding to be UTF-8.
Upvotes: 0
Reputation: 3609
Try to use "\t" between columns not rows, it makes the native offset/margin in Excel. Also better use for loop with curly-braces as a better standard to not confuse Yourself.
Upvotes: 0
Reputation: 1318
First of all: tell us something about the error!
I recommend you to do one of the two following steps (or both).
set_time_limit(60);
// increase this value to 300 (means the server doesn't timeout for 5 minutes)
ini_set('memory_limit','512M');
// increases the memory limit to 512 MB
Upvotes: 0
Reputation: 241
First, you should put the output in a buffer variable and not echo or print all the way.
After that you should read your own comment -> //set_time_limit(60); // you can enable this if you have lot of data
Upvotes: 0