导出为Excel


有时有一些需求需要导出表格数据为Excel,下面就谈一下这方面的内容。


正文

php怎么导出EXCEL文件呢?如果是ThinkPHP,可能会想到用PHPExcel,但是这个组件在数据量大时会导致内存溢出,那么有没有其他好方法呢?下面介绍一种:

function excelData( $datas, $tdtitle, $headtitle, $filename )
{
$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n
       xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\n
       xmlns=\"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>".$headtitle."</head>";
   $str .= $tdtitle;
   foreach ($datas as $key=> $rt )
{
$str .= "<tr>";
       foreach ( $rt as $k => $v )
{
$str .= "<td>{$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 );
}

使用方法:






参考资料

http://www.cnblogs.com/followyou/p/6143168.html


返回