将MYSQL表导出为excel
我有一些用于将MYSQL表导出为Excel格式的PHP脚本,但是在导出表后我遇到了一些问题。从db获得的数据只显示一行。将MYSQL表导出为excel
这下面的脚本:
$dbc=mysql_connect(_SRV,_ACCID,_PWD) or die(_ERROR15.": ".mysql_error());
$db=mysql_select_db("qdbase",$dbc) or die(_ERROR17.": ".mysql_error());
$sQuery = "SELECT id, Line, Model,Lot_no,
COUNT(Serial_number) AS Qty,
SUM(S), SUM(A), SUM(B), SUM(C),
(SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number) AS QP,
ROUND((SUM(S) + SUM(A) + SUM(B)*0.4 + SUM(C)*0.1)/COUNT(Serial_number)*1000,2) AS PPM
FROM `inspection_report`";
$rResult = mysql_query($sQuery) or die();
$count = mysql_num_fields($rResult);
// fetch table header
$header = '';
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($rResult, $i)."\t";
}
// fetch data each row, store on tabular row data
while($row = mysql_fetch_row($rResult)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
$data = str_replace("\r", "", $data);
if ($data == "") {
$data = "\nno matching records found\n";
}
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
// output data
echo $header."\n".$data;
?>
那旁边,如何使一些边境此表?
如果我想这样做:echo“”;哪个正确的地方 说,不能让我的脚本成为 错误?
如果youre goign做到这一点作为一个HTML表格,然后我将仅仅指刚输出liek你通常右表...例如,你可以做:
$rResult = mysql_query($sQuery) or die();
$count = mysql_num_fields($rResult);
$html = '<table border="1"><thead><tr>%s</tr><thead><tbody>%s</tbody></table>';
$thead = '';
$tbody = '';
$line = '<tr>%s</tr>';
for ($i = 0; $i < $count; $i++){
$thead .= sprintf('<th>%s</th>',mysql_field_name($rResult, $i));
}
while(false !== ($row = mysql_fetch_row($rResult))){
$trow = '';
foreach($row as $value){
$trow .= sprintf('<td>%s</td>', $value);
}
$tbody .= sprintf($line, $trow);
}
header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: attachment; filename=exportfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
print sprintf($html, $thead, $tbody);
exit;
特殊照顾只得到一个行,因为您使用的聚合函数没有GROUP BY
子句。尝试添加一个GROUP BY
,以便将这些“条目”中的每一个与“Lot_no
”之类的特定组或任何想要报告的内容相关联。
至于边界,你不能这样做CSV
虽然readbale由Excel,实际上并不是一个Excel
格式文件与所有额外的格式为边界。要使用格式化等内容,您需要输出本机Excel或可以加载到Excel中的html表格。如果你需要这样做,请看phpexcel
。
你实际上并没有从表中拉出任何东西。您只有一个查询,它正在拉取标题信息。运行一个像“select * from tableName”那样的sql语句来获取剩余的内容,然后对该查询执行while和for循环。 PHP /浏览器通常不能很好地运行编程语言,XML和CSV之外的项目。我会更加关注这两者中的任何一个。新版本的Excel可以读取XML(并且可以在Internet Explorer中以XML格式打开),而csv更广泛地用于在不同类型的数据库/电子表格之间共享。有关XML输出的一个示例,您将不得不在数组上使用fputcsv()函数。例如:
$query = mysql_fetch_row($rResult);//the query of the table
// use 'select * from table' to list everything
$xls = fopen('file.csv', 'a'); //opens document for writing and attempts to create if file does not exist
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($xls); //always close the file when done
两者都是很脏的出口方式,但工作。
试试这个代码工作对我来说:如果我想要做的
<?php
$db_name = "db_name";
$db_password = "pass";
$db_link = mysql_connect("localhost", "root", $db_password);
mysql_select_db($db_name, $db_link);
mysql_query("SET NAMES UTF8");
$table = "table_name";
function assoc_query_2D($sql, $id_name = false){
$result = mysql_query($sql);
$arr = array();
$row = array();
if($result){
if($id_name == false){
while($row = mysql_fetch_assoc($result))
$arr[] = $row;
}else{
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$arr[$id] = $row;
}
}
}else return 0;
return $arr;
}
function query_whole_table($table, $value = '*'){
$sql = "SELECT $value FROM $table";
return assoc_query_2D($sql);
}
$export_str = "";
$result = query_whole_table($table);
foreach($result as $record){
$export_str .= implode(";",$record) . "\n";
}
// add time to fileName
$date = time();
file_put_contents($date.'_'.$table."_export.csv", $export_str);
?>
:回声 “”;哪个正确的地方,它不能让我的脚本成为错误? – klox 2010-11-27 08:09:17
耶...我想这样做。 – klox 2010-11-27 09:38:47