【发布时间】:2012-09-21 07:52:07
【问题描述】:
我想将数据从 mysql 表导出到 excel 表。我正在使用 excel 2007。一旦这段代码正常工作,但今天我遇到了问题。请指导我哪里做错了。我有大约 60,000 行的庞大数据。
<?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>');
?>
这很重要,请指导我。提前致谢。
【问题讨论】:
-
你也有 Microsoft Access 吗?
-
你遇到了什么问题?
-
@JMK 是的,我有 Microsoft Access 2007
-
@Passerby 它正在创建 .xlsx 文件并要求下载,但是当我在下载后打开文件时出现错误“无法打开文件文件格式或扩展名无效”