PHP export and write Excel files_PHP tutorial

WBOY
Release: 2016-07-14 10:12:06
Original
905 people have browsed it

<?php
//  PHPExcel  需要下载  官网:http://www.codeplex.com/PHPExcel
header("Content-Type: text/html; charset=utf-8");

require_once &#39;./PHPExcel.php&#39;;
require_once &#39;./PHPExcel/IOFactory.php&#39;;
require_once &#39;./PHPExcel/Reader/Excel5.php&#39;;


/**
*	读取Excel表格
*	@param $filePath   Excel文件路径
*	@param $field		需要保存的字段		array(&#39;id&#39;,&#39;username&#39;,&#39;password&#39;)
*	@param $column		读取Excel那列		array(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;)
*	@examlpe 
	
	$filePath = &#39;1.xls&#39;;
	$field = array(&#39;id&#39;, &#39;username&#39;, &#39;password&#39;);
	$column = array( &#39;A&#39;, &#39;B&#39;, &#39;C&#39;);

	readExcel($filePath,$field,$column);
*/
function readExcel($filePath,$field,$column){
	$objReader = PHPExcel_IOFactory::createReader(&#39;Excel5&#39;);//use excel2007 for 2007 format
	$objPHPExcel = $objReader->load($filePath); //$filename可以是上传的文件,或者是指定的文件
	$sheet = $objPHPExcel->getSheet(0); 
	$highestRow = $sheet->getHighestRow(); // 取得总行数 
	$highestColumn = $sheet->getHighestColumn(); // 取得总列数
	
	
	for($j=1;$j<=$highestRow;$j++)
	{
		$colData = array();
		$count = count($column);
		for($i=0;$i<$count;$i++){
			
			$colData[$field[$i]] = $objPHPExcel->getActiveSheet()->getCell($column[$i].$j)->getValue();//获取A列的值
			
		}
		
		$excelData[] = $colData;
		
	}
	
	return $excelData;
}





/**
*	导出数据为excel表格
*	@param $data    一个二维数组,结构如同从数据库查出来的数组
*	@param $title   excel的第一行标题,一个数组,如果为空则没有标题
*	@param $filename 下载的文件名
*	@examlpe 
	exportexcel($arr,array(&#39;id&#39;,&#39;账户&#39;,&#39;密码&#39;,&#39;昵称&#39;),&#39;文件名!&#39;);
*/
function exportexcel($data=array(),$title=array(),$filename=&#39;report&#39;){
	header("Content-type:application/octet-stream");
	header("Accept-Ranges:bytes");
	header("Content-type:application/vnd.ms-excel");  
	header("Content-Disposition:attachment;filename=".$filename.".xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	//导出xls 开始
	if (!empty($title)){
		foreach ($title as $k => $v) {
			$title[$k]=iconv("UTF-8", "GB2312",$v);
		}
		$title= implode("\t", $title);
		echo "$title\n";
	}
	if (!empty($data)){
		foreach($data as $key=>$val){
			foreach ($val as $ck => $cv) {
				$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
			}
			$data[$key]=implode("\t", $data[$key]);
			
		}
		echo implode("\n",$data);
	}
}
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477238.htmlTechArticle?php// PHPExcel needs to be downloaded from the official website: http://www.codeplex.com/PHPExcelheader(Content-Type : text/html; charset=utf-8);require_once ./PHPExcel.php;require_once ./PHPExcel/IOFactory.p...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!