Rblogs

大概尝试有四种方法

一、思路:拼接成表格转化为浏览器输出

/* 
*处理Excel导出 
*@param $datas array 设置表格数据 
*@param $titlename string 设置head 
*@param $title string 设置表头 
*/ 
public function excelData($datas,$titlename,$title,$filename){ 
    $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>"; 
    $str .="<table border=1><head>".$titlename."</head>"; 
    $str .= $title; 
    foreach ($datas  as $key=> $rt ) 
    { 
        $str .= "<tr>"; 
        foreach ( $rt as $k => $v ) 
        { 
            //防止乱码
            $str .= "<td>iconv("UTF-8","GB2312",{$v})</td>"; 
        } 
        $str .= "</tr>\n"; 
    } 
    $str .= "</table></body></html>"; 
    header( "Content-Type: application/vnd.ms-excel; name=\'excel\'" ); 
    header( "Content-type: application/octet-stream" ); 
    header( "Content-Disposition: attachment; filename=".$filename ); 
    header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); 
    header( "Pragma: no-cache" ); 
    header( "Expires: 0" ); 
    exit( $str ); 
} 

$dataResult = array();      //todo:导出数据(自行设置) 
$headTitle = "XX保险公司 优惠券赠送记录"; 
$title = "优惠券记录"; 
$headtitle= "<tr style=\'height:50px;border-style:none;><th border=\"0\" style=\'height:60px;width:270px;font-size:22px;\' colspan=\'11\' >{$headTitle}</th></tr>"; 
$titlename = "<tr> 
               <th style=\'width:70px;\' >合作商户</th> 
               <th style=\'width:70px;\' style=\'vdn.ms-excel.numberformat:@\' >会员卡号</th> 
               <th style=\'width:70px;\'>车主姓名</th> 
               <th style=\'width:150px;\'>手机号</th> 
               <th style=\'width:70px;\'>车牌号</th> 
               <th style=\'width:100px;\'>优惠券类型</th> 
               <th style=\'width:70px;\'>优惠券名称</th> 
               <th style=\'width:70px;\'>优惠券面值</th> 
               <th style=\'width:70px;\'>优惠券数量</th> 
               <th style=\'width:70px;\'>赠送时间</th> 
               <th style=\'width:90px;\'>截至有效期</th> 
           </tr>"; 
           $filename = $title.".xls"; 
       $this->excelData($dataResult,$titlename,$headtitle,$filename);     

这个方法PHP在输出EXCEL表格时,经常会碰到如下问题,如有些物料编号是数字0开头的,产生EXCEL后,会发现数字0会丢失;还有类似"1-3"这种纯文本格式的导出后会自动转化为日期型格式。

我们可以通过如下方式去规定数据保存的格式:
        1)文本:vnd.ms-excel.numberformat:@
        2)日期:vnd-ms-excel.numberformat:yyyy/mm/dd
        3)数字:vnd-ms-excel.numberformat:#,##0.00
        4)货币:vnd-ms-excel.numberformat:$#,##0.00
        5)百分比:vnd-ms-excel.numberformat:#0.00%
 
二、使用phpexcel ,引入该类就行了,但是数据量过万的时候,就很容易崩溃了
三、导出cvs文件
header(\'Content-Type: application/vnd.ms-excel\');
  header(\'Content-Disposition: attachment;filename="user.csv"\');
  header(\'Cache-Control: max-age=0\');
  // 从数据库中获取数据,为了节省内存,不要把数据一次性读到内存,从句柄中一行一行读即可

  $sql = \'select * from tbl where ……\';

  $stmt = $db->query($sql);

  // 打开PHP文件句柄,php://output 表示直接输出到浏览器

  $fp = fopen(\'php://output\', \'a\');

  // 输出Excel列名信息

  $head = array(\'姓名\', \'性别\', \'年龄\', \'Email\', \'电话\', \'……\');

  foreach ($head as $i => $v) {

  // CSV的Excel支持GBK编码,一定要转换,否则乱码

  $head[$i] = iconv(\'utf-8\', \'gbk\', $v);

  }

  // 将数据通过fputcsv写到文件句柄

  fputcsv($fp, $head);

  // 计数器

  $cnt = 0;

  // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小

  $limit = 100000;

  // 逐行取出数据,不浪费内存

  while ($row = $stmt->fetch(Zend_Db::FETCH_NUM)) {

  $cnt ++;

  if ($limit == $cnt) { //刷新一下输出buffer,防止由于数据过多造成阻塞问题

  ob_flush();

  flush();


  $cnt = 0;

  }

  foreach ($row as $i => $v) {

  $row[$i] = iconv(\'utf-8\', \'gbk\', $v);

  }

  fputcsv($fp, $row);

  } 

这种方法大约处理50万左右的数据

四、写入内存下载

public function exportAction()
    {
        set_time_limit(0);
        ini_set(\'memory_limit\', \'1024M\');
        //获取数据
        $data="你的数据";  //二维数组,可以分页读取,一次读多少条
        $excelFilename="download/allOrderExcel.csv";
        $header=array();
        $header[]=iconv("UTF-8","GB2312","订单ID");
        $header[]=iconv("UTF-8","GB2312","类型");
        $header[]=iconv("UTF-8","GB2312","状态");
        $header[]=iconv("UTF-8","GB2312","个数");
        $header[]=iconv("UTF-8","GB2312","备注");
        $header[]=iconv("UTF-8","GB2312","创建时间");
        $header[]=iconv("UTF-8","GB2312","更新时间");
        $header[]=iconv("UTF-8","GB2312","完成时间");
        $header[]=iconv("UTF-8","GB2312","地址");
        $header[]=iconv("UTF-8","GB2312","用户名");
        $header[]=iconv("UTF-8","GB2312","真实姓名");
        $header[]=iconv("UTF-8","GB2312","手机号");
        $headerFile = implode(\',\', $header);
        file_put_contents($excelFilename,$headerFile."\n");
        if(!$handle=fopen($excelFilename,\'a\')){
            echo "文件打开失败";
            exit;
        }
        foreach ($data as $val){
            //将每个数组,拼接成字符串,写入文件
            $filecontent[]=implode(\',\',$val);
        }
        foreach ($filecontent as $content){
            //遍历数组,一条一条写入
            fwrite($handle,$content."\n");
        }
        fclose($handle);
        header("Content-type: application/octet-stream");
        header(\'Content-Disposition: attachment; filename="\' . basename($excelFilename) . \'"\');
        header("Content-Length: ". filesize($excelFilename));
        readfile($excelFilename);
        exit;
    }

 

数字显示不完整?变成科学计数法了?

这个问题是因为Excel显示数字时,如果数字大于12位,它会自动转化为科学计数法;如果数字大于15位,它不仅用于科学技术费表示,还会只保留高15位,其他位都变0。
解决这个问题:
只要把数字字段后面加上显示上看不见的字符即可,字符串前面或者结尾加上制表符"\t".
php 程序可以这样判断,注意一定是"\t",不是\'\t\'.(双引号)

分类:

技术点:

相关文章:

  • 2021-07-16
  • 2021-09-26
  • 2021-08-01
  • 2018-10-25
  • 2021-09-07
  • 2021-10-29
  • 2021-12-08
  • 2021-07-03
猜你喜欢
  • 2020-03-14
  • 2021-10-17
  • 2021-05-06
  • 2021-08-06
  • 2021-12-15
  • 2021-05-09
相关资源
相似解决方案