phpexcel 的前端封装类

说到phpexcel,做php开发的童鞋可能都不陌生的,强大的excel表格导出类。它到底有多强大呢?可以导出xls,xlsx(唯一见到的可以导出excel2007的php类),html,pdf,csv等。还可以对xml模板进行编辑然后保存,还可以在excel里面设置图片,画表格,字体的大小,字体表格的颜色等,强大得你都想想不到。但phpexcel导出的pdf实在不敢让人恭维。最近研究了下,写了一个类可以同时导出xls,xlsx,csv,html。主要对其进行了前端的封装。现贴代码上来。



<?php
/**
 * 导出excel2003
 * @author tengzhaorong@gmail.com
 * 2011-10-25
 */
//@todo cvs格式输出中文乱码 有没有必要同时生成html excel2003 excel2007 	
/** Error reporting */
error_reporting ( E_ALL );
header ( 'Content-Type:text/html;charset=gbk' );
date_default_timezone_set ( 'Asia/Shanghai' );

/** PHPExcel */
require_once 'excel/PHPExcel.php';

class Excel {

	private $_objPHPExcel; //excel 句柄

	
	private $_savePath = "attach"; //保存目录

	
	//private $_type = array ( "xls" ); //文件类型

	
	private $_suffix = array ( 'xls' , 'xlsx' , 'html' ,'htm','csv');

	private $letter = array ( 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'M' , 'N' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' );

	function __construct () {


		$this->_objPHPExcel = new PHPExcel ();
	}

	/**
	 * Enter 设置头.
	 *
	 * @param array $data
	 */
	function setMenu ( $data ) {

		if ( ! is_array ( $data ) ) {
			$data = ( array ) $data;
		}
		
		foreach ( $data as $k => $v ) {
			//$v=iconv("utf-8","gb2312",$v);
			$this->_objPHPExcel->setActiveSheetIndex ( 0 )->setCellValue ( $this->letter [$k] . "1", $v );
		}
	}

	/**
	 * Enter 设置属性.
	 *
	 * @param array $data
	 */
	function setProperties ( $data ) {

		if ( ! is_array ( $data ) ) {
			$data = ( array ) $data;
		}
		if ( in_array ( "creator", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setCreator ( $data ['creator'] );
		
		}
		if ( in_array ( "lastModifiedBy", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setLastModifiedBy ( $data ['lastModifiedBy'] );
		
		}
		if ( in_array ( "title", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setTitle ( $data ['title'] );
		
		}
		if ( in_array ( "subject", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setSubject ( $data ['subject'] );
		
		}
		if ( in_array ( "description", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setDescription ( $data ['description'] );
		
		}
		if ( in_array ( "keywords", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setKeywords ( $data ['keywords'] );
		
		}
		if ( in_array ( "category", array_keys ( $data ) ) ) {
			
			$this->_objPHPExcel->getProperties ()->setCategory ( $data ['category'] );
		
		}
	}

	/**
	 * Enter 设置数据
	 *
	 * @param array $data
	 */
	function setData ( $data ) {

		if ( ! is_array ( $data ) ) {
			$data = ( array ) $data;
		}
		foreach ( $data as $k => $v ) {
			foreach ( $v as $index => $value ) {
				//$value=iconv("utf-8","gb2312",$value);
				$this->_objPHPExcel->setActiveSheetIndex ( 0 )->setCellValue ( $this->letter [$index] . ($k + 2), $value );
			}
		}
	}

	/**
	 * Enter 设置标题
	 *
	 * @param string $title
	 */
	function setTitle ( $title ) {

		$this->_objPHPExcel->getActiveSheet ()->setTitle ( $title );
	
	}

	/**
	 * Enter 保存文件
	 *
	 * @param string $filename
	 * @param array $type
	 */
	function save ( $type,$filename = "") {

		if ( ! $filename ) {
			$filename = time ();
		}
		
		if(!is_array($type)){
			$type=(array)$type;
		}
		
		foreach ( $type as $k => $v ) {
			
			
			if ( in_array ( $v,$this->_suffix ) ) {
				
				$suffix = $v;
			}
			else {
				continue;
			}
			$path = $this->_savePath . "/" . $filename . "." . $suffix;
			
			switch ($v) {
				case "htm":
				case "html" :
					
					$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, "HTML" );
					$objWriter->save ( $path );
					
					break;
				case "xls" :
					
					$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, "Excel5" );
					$objWriter->save ( $path );
					
					break;
				case "xlsx" :
					
					$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, "Excel2007" );
					$objWriter->save ( $path );
					
					break;
				case "csv" :
					
					$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'CSV' )->setDelimiter ( ',' )->setEnclosure ( '' )->setLineEnding ( "\r\n" )->setSheetIndex ( 0 );
					
					break;
				
				default :
					
					print( "sorry!".$v." not support" );
			
			}
		
		}
	
	}

	/**
	 * Enter 浏览器输出
	 *
	 * @param string $filename
	 * @param string $type
	 */
	
	function output ($type="xls",$filename = "") {

		if ( ! $filename ) {
			$filename = time ();
		}
		
		if ( in_array ( $type, $this->_suffix  ) ) {
			$suffix = $type;
		
		}
		else {
			$suffix = "xls";
		}
		switch ($type) {
			case "xls" :
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="' . $filename . '.' . $suffix . '"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'Excel5' );
				break;
			case "xlsx" :
				header ( 'Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' );
				header ( 'Content-Disposition: attachment;filename="' . $filename . '.' . $suffix . '"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'Excel2007' );
				break;
			case "html" :
				header ( "Content-Type:HTML text data" );
				header ( 'Content-Disposition: attachment;filename="' . $filename . '.' . $suffix . '"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'HTML' );
				break;
			case "csv" :
				//header ( "Content-type:text/csv" );
				Header('Content-Type: application/msexcel;charset=gbk');
				header ( 'Content-Disposition:attachment;filename="' . $filename . '.' . $suffix . '"' );
				//header ( 'Cache-Control:must-revalidate,post-check=0,pre-check=0' );
				//header ( 'Expires:0' );
				//header ( 'Pragma:public' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'CSV' );
				break;
			default :
				header ( "Content-Type:HTML text data" );
				header ( 'Content-Disposition: attachment;filename="' . $filename . '.' . $suffix . '"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $this->_objPHPExcel, 'HTML' );
				break;
		}
		
		$objWriter->save ( 'php://output' );
	}

	/**
	 * Enter 设置存储目录
	 *
	 * @param string $path
	 */
	function setAttachDir ( $path ) {

		if ( ! is_dir ( $path ) ) {
			
			mkdir ( $path, 755 );
		}
		if(!is_writable($path)){
			chmod($path, 755);
		}
		$this->_savePath = $path;
	
	}
}

实现的功能不多,目前只可以简单的同时导出几种格式的文件。


下面为实例代码

$data [0] = array ( '什么' , '20' );
		$data [1] = array ( '小刚' , '20' );
		$data [2] = array ( '小开' , '20' );
	//数据
		$excel = new Waf_excel ();
		$excel->setProperties ( array ( "title" => '不神奇' ) );
		//设置属性 可设置的属性有 creator  lastModifiedBy title  subject  description  keywords  	category。
		$excel->setMenu ( array ( '乱码' , "年纪" ) );//设置菜单栏目
		$excel->setData ( $data );//设在数据
		$excel->setTitle ( "一个神奇的网站" );//设置显示的标题
		$excel->setAttachDir ( "backs" );//设置数据保存的目录 默认情况为 当前目录下的 attach
		$excel->output ("csv", "kk" );//直接下载导出 不生存文件 csv格式 文件名为kk
		
		$excel->save (array("csv"), "kk" )//直接保存到文件夹里,可以同时保存csv html xls xlsx 格式,第一个参数为保存的格式,可为数组也可为字符串,第二参数为文件名。
phpexcel 下载地址http://phpexcel.codeplex.com/releases/view/45412


展开阅读全文

没有更多推荐了,返回首页