码迷,mamicode.com
首页 > 其他好文 > 详细

导出excel 各 cvs 的方法

时间:2018-04-26 18:31:16      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:支持   expires   reac   tac   nbsp   doc   bytes   date   off   

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 的方法

标签:支持   expires   reac   tac   nbsp   doc   bytes   date   off   

原文地址:https://www.cnblogs.com/vania/p/8953803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!