Home > php教程 > php手册 > Thinkphp使用PHPExcel实现导出Excel

Thinkphp使用PHPExcel实现导出Excel

WBOY
Release: 2016-06-07 11:43:30
Original
1444 people have browsed it

Thinkphp使用PHPExcel实现导出Excel
//导出Excel表格<br> public function export($data,$excelFileName,$sheetTitle){<br> <br>     $this->__construct();<br>     /* 实例化类 */<br>     $objPHPExcel = new PHPExcel(); <br>     <br>     /* 设置输出的excel文件为2007兼容格式 */<br>     //$objWriter=new PHPExcel_Writer_Excel5($objPHPExcel);//非2007格式<br>     $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);<br>     <br>     /* 设置当前的sheet */<br>     $objPHPExcel->setActiveSheetIndex(0);<br>     <br>     $objActSheet = $objPHPExcel->getActiveSheet();<br>     <br>     /*设置宽度*/<br>     $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);<br>     $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(60);<br>     <br>     <br>         <br>     /* sheet标题 */<br>     $objActSheet->setTitle($sheetTitle);<br>     <br>     $i = 2;<br>     foreach($data as $value)<br>     {<br>         /* excel文件内容 */<br>         $j = 'A';<br>         foreach($value as $value2)<br>         { <br> //            $value2=iconv("gbk","utf-8",$value2);<br>             $objActSheet->setCellValue($j.$i,$value2);<br>             $j++;<br>         }<br>         $i++;<br>     }<br>     <br>     <br>     /* 生成到浏览器,提供下载 */ <br>     ob_end_clean();  //清空缓存             <br>     header("Pragma: public");<br>     header("Expires: 0");<br>     header("Cache-Control:must-revalidate,post-check=0,pre-check=0");<br>     header("Content-Type:application/force-download");<br>     header("Content-Type:application/vnd.ms-execl");<br>     header("Content-Type:application/octet-stream");<br>     header("Content-Type:application/download");<br>     header('Content-Disposition:attachment;filename="'.$excelFileName.'.xlsx"');<br>     header("Content-Transfer-Encoding:binary"); <br>     $objWriter->save('php://output');<br> }所有文件已经打包,感兴趣可以下载测试,多多探讨

附件 Thinkphp+PHPExcel.zip ( 3.05 KB 下载:1139 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template