php 原生生成xls和csv

参考文献:http://blog.csdn.net/jaray/article/details/12967035


1、生成xls

核心代码

//开始输出
                     $excel = new excel();//引入类文件,实例化类
                     header('Content-Type: text/html; charset=utf-8');
                     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=".$filename.".xls");
                     header("Content-Transfer-Encoding: binary ");
                     $excel->xlsBOF();
                     //输出标题         
                     for ($i=0;$i<$filedsnum;$i++)
                         {
                              $excel->write_excel_line(0, $i, $title[$i]);
                         }   
                     for ($sh=0;$sh<$rownum;$sh++)
                         {
                              $rec=$conn->get_Fetch_Row($sh);
                              for ($i=0;$i<$filedsnum;$i++)
                                  {
                                       if (!in_array($i,$type_text))
                                           $excel->write_excel_line(($sh+1), $i, $rec[$fileds[$i]]);       
                                       else #强制用字符输出
                                           $excel->write_excel_line(($sh+1), $i, $rec[$fileds[$i]],true);       
                                  }
                         }
                     $excel->xlsEOF();   
                     exit;

对应的类文件excel.php

<?php
//excel导出类
CLASS excel
{
     //excel文件头开始
     function xlsBOF() 
         {
              echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
              return;
         }   
     //excel文件结束   
     function xlsEOF() 
         {
              echo pack("ss", 0x0A, 0x00);
              return;
         }
     //中文数据转换
     function format( $STR )
         {
              /*
              $STR = str_replace( "\"", "", $STR );
              if ( strpos( $STR, "," ) )
                  {
                       $STR = "\"".$STR."\"";
                  }
              */
              $STR = iconv( "utf-8", "gb2312", $STR );
              return $STR;
         }
     //存储单元格是数字
     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;
         }
     //写入内容
     // $label_true 强制用字符存储
     function write_excel_line($hang,$lie,$val,$label_true=false)
         {        
              if (preg_match("/[\x7f-\xff]/", $val))
                  {
                       $this->xlsWriteLabel($hang,$lie, $this->format($val)); 
                  }          
              else if(is_numeric($val) && !$label_true)
                  {
                       $this->xlsWriteNumber($hang,$lie,$val);
                  }
              else
                  {
                       $this->xlsWriteLabel($hang,$lie,$val);
                  }
         }
}
?>




2、生成csv文件

核心代码


                    header('Content-Type: text/html; charset=gbk');     
                     header("Content-disposition: attachment;filename=".$filename.".csv");
                     header("Content-type: text/x-csv");
                     header("Pragma: no-cache");
                     header("Expires: 0");
                     //输出标题     
                     $header_csv="";   
                     for ($i=0;$i<$filedsnum;$i++)
                         {
                              $title[$i]=@iconv("utf-8","gbk",$title[$i]);
                              $title[$i]=@iconv("gbk","utf-8",$title[$i]);
                              $header_csv=$header_csv."\"".$title[$i]."\","; 
                         }
                     $header_csv=SUBSTR($header_csv,0,STRLEN($header_csv)-1)."\n";       
                     echo $header_csv;
                     for ($sh=0;$sh<$rownum;$sh++)
                         {
                              $rec=$conn->get_Fetch_Row($sh);
                              for ($i=0;$i<$filedsnum-1;$i++)
                                  {
                                       $rec[$fileds[$i]]=@iconv("utf-8","gbk",$rec[$fileds[$i]]);
                                       $rec[$fileds[$i]]=@iconv("gbk","utf-8",$rec[$fileds[$i]]);
                                       $detail_csv=$detail_csv."\"".$rec[$fileds[$i]]."\",";           
                                  }
                              $detail_csv=$detail_csv."\"".$rec[$fileds[$i]]."\"\n"; 
                              ECHO  $detail_csv;     
                              $detail_csv="";
                         }
                     EXIT;




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值