codeigniter导出到excel在localhost上运行良好,服务器上出现错误

这是我的export_to_excel助手的代码:

function export_to_excel($query, $filename='exceloutput') { $headers = ''; // just creating the var for field headers to append to below $data = ''; // just creating the var for field data to append to below $obj =& get_instance(); $fields = $query->list_fields(); if ($query->num_rows() == 0) { echo '<p>The table appears to have no data.</p>'; } else { foreach ($fields as $field) { $headers .= $field . "\t"; } foreach ($query->result() as $row) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); header("Content-type: application/x-msexcel; charset=utf-8"); header("Content-Disposition: attachment; filename=$filename.xls"); echo "$headers\n$data"; } } 

我在本地主机和服务器上得到不同的结果。 当我在localhost中运行代码时,它输出正确的结果没有问题,但是当我在服务器上运行代码时,它给出了和localhost相同的结果, 但是它增加了两行(excel行)包含错误,如下所示:

 <br /> <b>Fatal error</b>: ob_start() [&lt a href='ref.outcontrol'&gt ref.outcontrol&lt/a&gt]: Cannot use output buffering in output buffering display handlers in <b>/home/username/public_html/Codeigniter_website/system/core/Exceptions.php</b> on line <b>181</b><br /> 

任何解决scheme

这几乎是一个大项目,这是我所见过的本地和服务器之间的唯一区别。

解决scheme是确保输出和parsing在所需输出之后停止。

可以通过exit;来完成exit; echo "$headers\n$data";