【问题标题】:Exporting specific columns from Mysql table to Excel将特定列从 Mysql 表导出到 Excel
【发布时间】:2011-08-16 21:59:01
【问题描述】:

我试图从一个表中导出一些特定的列,而不是全部带有自定义标题,但是当我从服务器下载它时,它说文件已损坏并且无法打开。我正在使用以下代码

// Functions for export to excel.
function xlsBOF() {
   echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
   return;
}
function xlsEOF() {
   echo pack("ss", 0x0A, 0x00);
   return;
}
function xlsWriteNumber($Row, $Col, $Value) {
   echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
   echo pack("d", $Value);
   return;
}


function xlsWriteLabel($Row, $Col, $Value ) {
   $L = strlen($Value);
   echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
   echo $Value;
   return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();
xlsWriteLabel(0,5,"LIST OF CLOSED LEADS.");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"INQUIRY NO."); 
xlsWriteLabel(2,1,"POSTED DATE");
xlsWriteLabel(2,2,"AGENT NAME");
xlsWriteLabel(2,3,"STATUS");
xlsWriteLabel(2,4,"CLIENT NAME");
xlsWriteLabel(2,5,"UNIT NUMBER");
xlsWriteLabel(2,6,"PROPERTY TYPE");
xlsWriteLabel(2,7,"AREA");
xlsWriteLabel(2,8,"COMMISSION RECEIVED");
xlsWriteLabel(2,9,"BREAK UP DETAIL");
xlsWriteLabel(2,10,"DESCRIPTION");
xlsWriteLabel(2,11,"ADMIN COMMENT");

$xlsRow = 4;

while($row=mysql_fetch_array($result)){

   $agent_query = "select Name from pf_agents where ID =".$row['agent_id'];
   $agent_result=mysql_query("$agent_query");
   $agent_row=mysql_fetch_array($agent_result);
   $agent_name = $agent_row['Name'];

   $prop_query = "select Title from pf_property_types where ID =".$row['prop_type'];
   $prop_result = mysql_query("$prop_query");
   $prop_row = mysql_fetch_array($prop_result);
   $prop_name = $prop_row['Title'];

   $area_query = "select Name from pf_master_dev where ID =".$row['Area'];
   $area_result = mysql_query("$area_query");
   $area_row = mysql_fetch_array($area_result);
   $area_name = $area_row['Name'];

   $break_up = strip_word_html($row['reason_closed']);
   $comment = strip_word_html($row['comment']);
   $admin_comment = strip_word_html($row['comment_admin']);
   $date_closed = date('d-m-Y', strtotime($row['updated_date']));

   xlsWriteLabel($xlsRow,0,$row['ID']);
   xlsWriteLabel($xlsRow,1,$date_closed);
   xlsWriteLabel($xlsRow,2,$agent_name);
   xlsWriteLabel($xlsRow,3,$row['status']);
   xlsWriteLabel($xlsRow,4,$row['name']);
   xlsWriteLabel($xlsRow,5,$row['closed_unit']);
   xlsWriteLabel($xlsRow,6,$prop_name);
   xlsWriteLabel($xlsRow,7,$area_name);
   xlsWriteLabel($xlsRow,8,$row['closed_amt']);
   xlsWriteLabel($xlsRow,9,$break_up);
   xlsWriteLabel($xlsRow,10,$comment);
   xlsWriteLabel($xlsRow,11,$admin_comment);

   $xlsRow++;
}
xlsEOF();

有什么方法可以修复它,或者我可以使用其他一些解决方案。

谢谢

【问题讨论】:

  • 为什么要设置 三个 Content-Type 标头?
  • @Mark 我已经在我的文件中添加了 xlsEOF(),我只显示了我的代码的第一部分
  • @Marcel 我不确定,但我认为有必要
  • 没有。如果你想输出一个 Excel 文件,它应该是 application/vnd.ms-excel 而不是别的。
  • @kakaajee - 实际上只有一种内容类型(最后一种)会发送到浏览器,因此不需要全部使用这三种;并且您实际上应该发送 application/vnd.ms-excel 的内容类型

标签: php mysql excel export


【解决方案1】:

试试这个 PEAR 扩展来创建 XLS 文件:Spreadsheet_Excel_Writer。至少,它有据可查。

【讨论】:

    【解决方案2】:

    有很多 PHP 库可以写入 Excel 文件。试试我自己的PHPExcel 或响应this SO question 列出的库之一

    编辑

    有一个使用 PHPExcel here 将数据从 MySQL 写入 Excel 文件的示例

    【讨论】:

    • 是的,这些可以使用,但是我必须从多个表中获取数据并显示在特定的自定义列下,就像在我的代码中我提到了列名,然后以相同的顺序显示数据在诡计循环中。有没有更简单的解决方案?
    • 这是一个非常简单的解决方案...这一切都取决于您的 SQL 查询。我怀疑您是否会发现比单个包含和大约 20 行代码更简单的东西。
    • 马克你是对的,但原因是,我找不到任何使用多个列标题的例子。如果您能指出或给出任何代码,那将是很大的帮助。
    • @kakaajee - 你的 SQL 查询有问题吗?还是将列标题写入 Excel?
    • @Mark,不,我的问题不在于 Query 。可能你没有仔细阅读我的问题描述“从表格中导出一些特定的列,而不是全部带有自定义标题”。我需要带有自定义标题的每一列,并且我想控制我得到的每个结果集,因为我从多个表中获取数据。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多