linger308 发表于 2009-3-21 21:21:06

CI中导出数据到EXCEL表格问题

IE下不能导出,FIREFOX下正常。请高手指教!!
代码如下
function index()
{
      header("Content-Type:application/vnd.ms-excel");
      header("Content-type:charset=utf-8");
      header("Content-Disposition:attachment;filename=用户报警记录.xls")
   header("Pragma:no-cache");
      header("Expires:0");
$sep="\t";
$crlf="\n";
      $title= "用户的报警记录,日期:".date("Y-m-dH:i:s");
      echo$title.$crlf.$crlf;
      $sql="select city,danwei,user,u_sn,mac,cpu,hddid,ip,ipout,time fromcdb_upass_faildetail";
      $query=$this->db->query($sql);
      echo "地市".$sep."单位名称".$sep."用户".$sep."优盘序列号".$sep."MAC".$sep."CPU".$sep."硬盘ID".$sep."内网IP".$sep."外网IP".$sep."时间".$sep;
echo$crlf;
          foreach ($query->result() as $row) {
               $line = '';
               foreach($row as $value) {                                          
                         $value = str_replace("\r\n","",$value);
                         $line.=iconv("UTF-8","GB2312",$value).$sep;
                  }
                  echo$line.$crlf;
               }         
}

Hex 发表于 2009-3-22 02:06:31

报什么错儿?

linger308 发表于 2009-3-22 06:08:24

在IE下,提示 无法下载此文件,无法打开该站点或请求的站点不可用或找不到。关闭此对话框,重新刷新此页面,又可以导出EXCEL。不知是什么原因?但在火狐下正常。

Hex 发表于 2009-3-22 13:18:29

可能是输出的 header 不对,firefox 和 ie 所认识的 header 不一样吧。

linger308 发表于 2009-3-22 18:05:02

本帖最后由 linger308 于 2009-3-22 18:11 编辑

下载个excelplugins ,安装后运行的效果一样,插件名to_excel_pi.php,路径:system\plugins下,插件代码为:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function to_excel($query, $filename='exceloutput',$fields=FALSE)
{
   $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();   
   if (!$fields) {
          $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) {                                          
                         $value = str_replace("\r\n","",$value);
                         $line.=iconv("UTF-8","GB2312",$value)."\t";
                  }
                  $data .=$line."\r";
               }      
          header("Content-type: application/x-msdownload");
          header("Content-Disposition: attachment; filename=$filename.xls");
          echo "$headers\n$data";
   }
}
程序文件
<?php
class Userbaoeexcel extends Controller
{
       function Userbaoeexcel()
         {
      parent::Controller();
         }
       function index()
      {
         $this->load->plugin('to_excel');
         $sql="select city,danwei,user,u_sn,mac,cpu,hddid,ip,ipout,time from      cdb_upass_faildetail";
            $query=$this->db->query($sql);
            to_excel($query);
       }
}
?>
运行的结果在FIREFOX下正常导出数据到EXCEL,奇怪的是在IE下输入http://localhost/116cii/index.php/Userbaoeexcel/index 每次都提示出错,出错内容如下,再次刷新提示同样错误。
http://www.prayaya.com/test_cn/download/aa.jpg
若输入localhost/116cii/index.php/Userbaoeexcel/index去掉http://头,运行一次提示出错,出错内容同上,在次刷新可以导出。此问题困扰小弟两天了,真切希望高手给以指导,诚谢。

Hex 发表于 2009-3-22 23:25:08

看来是你本地环境的问题了,换一台电脑试试

linger308 发表于 2009-3-23 07:15:56

在服务器上和在本地测试都是一样的错误!!!

linger308 发表于 2009-3-23 07:31:15

终于找到问题了 原来是版本问题 使用1.7.1版本一直都提示这个问题。改用1.6.3版本就正常使用了,估计使用1.7.1版本应该使用输出类$this->output->set_output();接管输出的控制权输出下载或导出EXCEL,但不会使用。

Hex 发表于 2009-3-23 10:06:46

呵呵,楼主可以用 Fiddler 这样的东西监控一下 HTTP 请求,看看到底有什么不一样。

heraldic 发表于 2009-12-3 14:18:51

使用to_excel这款插件,如何避免汉字乱码?
页: [1] 2
查看完整版本: CI中导出数据到EXCEL表格问题