codeigniter导出到excel在本地主机上运行良好,在服务器上出现错误
这是我的导出到excel帮助程序的代码:codeigniter导出到excel在本地主机上运行良好,在服务器上出现错误,excel,codeigniter,export,export-to-excel,ob-start,Excel,Codeigniter,Export,Export To Excel,Ob Start,这是我的导出到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 =&a
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";
}
}
函数导出到excel($query,$filename='exceloutput')
{
$headers='';//只是为要附加到下面的字段头创建变量
$data='';//只是为要附加到下面的字段数据创建变量
$obj=&get_instance();
$fields=$query->list_fields();
如果($query->num_rows()==0){
echo“该表似乎没有数据。”;
}否则{
foreach($fields作为$field){
$headers.=$field.“\t”;
}
foreach($query->result()作为$row){
$line='';
foreach($行作为$值){
如果((!isset($value))或($value==“”){
$value=“\t”;
}否则{
$value=str_replace(“,“,”,$value);
$value=“.”.$value.“.”“\t”;
}
$line.=$value;
}
$data.=修剪($line)。“\n”;
}
$data=str_replace(“\r”,”,$data);
标题(“内容类型:应用程序/x-msexcel;字符集=utf-8”);
标题(“内容处置:附件;文件名=$filename.xls”);
回显“$headers\n$data”;
}
}
我在localhost和服务器上得到不同的结果。当我在localhost中运行代码时,它会毫无问题地输出正确的结果,但当我在服务器上运行代码时,它给出的结果与在localhost中相同,但它又添加了两行(excel行),其中包含以下错误:
<br />
<b>Fatal error</b>: ob_start()
[< a href='ref.outcontrol'> ref.outcontrol</a>]:
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 />
致命错误:ob_start()
[< a href='ref.outcontrol'> ref.outcontrol</a>]:
无法在中的输出缓冲显示处理程序中使用输出缓冲
/home/username/public_html/Codeigniter_网站/system/core/Exceptions.php
在线181
有什么解决办法吗
这几乎是一个大型项目,这是我所看到的本地和服务器之间的唯一区别。解决方案是确保输出和解析在所需输出之后停止
可以通过将
退出;
放在echo“$headers\n$data”
之后来完成此操作。解决方案是确保在所需的输出之后停止输出和解析
将
exit;
放在echo“$headers\n$data”之后;
尝试添加exit;
放在echo“$headers\n$data”之后;
?感谢您提供此解决方案。但最好找到更好的解决方案。如果我没有找到任何解决方案,它将是唯一的解决方案。+1.我甚至在echo”之后添加了return TRUE;
$headers\n$data";代码>但仍然没有运气。这个想法是基于你的评论。无论如何,谢谢。为什么这是一个糟糕的解决方案?这是确保输出在所需点停止的一种常见方法——为什么这里不适用?我在自己的CI项目中使用相同的方法。好的,请将其作为回复发布,以便我也可以接受。尝试添加exit代码>在回显“$headers\n$data”之后代码>?感谢您提供此解决方案。但最好找到更好的解决方案。如果我没有找到的话,那将是唯一的一个+1.我甚至添加了returntrue代码>在回显“$headers\n$data”之后代码>但仍然没有运气。这个想法是基于你的评论。无论如何,谢谢。为什么这是一个糟糕的解决方案?这是确保输出在所需点停止的一种常见方法——为什么这里不适用?我在自己的CI项目中使用相同的方法。好的,请将其作为回复发布,以便我也可以接受。