导出excel 各 cvs 的方法
Posted 小V_chen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出excel 各 cvs 的方法相关的知识,希望对你有一定的参考价值。
public function orderExcelExport($data,$filename=‘simple.xls‘){ ini_set(‘max_execution_time‘, ‘0‘); Vendor(‘phpExcel.PHPExcel‘); $filename=str_replace(‘.xls‘, ‘‘, $filename).‘.xls‘; $phpexcel = new \PHPExcel(); $phpexcel->getProperties() ->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); $phpexcel->getActiveSheet()->fromArray($data); $phpexcel->getActiveSheet()->setTitle(‘Sheet1‘); $phpexcel->setActiveSheetIndex(0); header(‘Content-Type: application/vnd.ms-excel‘); header("Content-Disposition: attachment;filename=$filename"); header(‘Cache-Control: max-age=0‘); header(‘Cache-Control: max-age=1‘); header (‘Expires: Mon, 26 Jul 1997 05:00:00 GMT‘); // Date in the past header (‘Last-Modified: ‘.gmdate(‘D, d M Y H:i:s‘).‘ GMT‘); // always modified header (‘Cache-Control: cache, must-revalidate‘); // HTTP/1.1 header (‘Pragma: public‘); // HTTP/1.0 $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, ‘Excel5‘); $objwriter->save(‘php://output‘); exit; }
导出cvs
public function create_csv($data,$filename=‘simple.csv‘){ // 防止没有添加文件后缀 $filename=str_replace(‘.csv‘, ‘‘, $filename).‘.csv‘; Header( "Content-type: application/octet-stream "); Header( "Accept-Ranges: bytes "); Header( "Content-Disposition: attachment; filename=".$filename); $outputBuffer = fopen("php://output", ‘w‘); foreach($data as $val) { foreach ($val as $key => $val2) { $val[$key] = iconv(‘utf-8‘, ‘gbk‘, $val2); // CSV的Excel支持GBK编码,一定要转换,否则乱码,转成gbk以兼容office乱码的问题 } fputcsv($outputBuffer, $val); } fclose($outputBuffer); }
以上是关于导出excel 各 cvs 的方法的主要内容,如果未能解决你的问题,请参考以下文章