Table of Contents
php 基于phpexcel制作的导出excel类,phpexcelexcel
想用PHP导出excel表格,但是php环境是51*的,没办法用PHPExcel开源类,有啥办法?
PHPExcel生成Excel不可以读取
Home php教程 php手册 php 基于phpexcel制作的导出excel类,phpexcelexcel

php 基于phpexcel制作的导出excel类,phpexcelexcel

Jun 13, 2016 am 09:24 AM
excel form

php 基于phpexcel制作的导出excel类,phpexcelexcel

最近要写个项目,由于项目中导出excel太多,因此写个类便于使用。

<?php
/*
*@使用方法。
*引入类库。
* $excel = news excelC();
*$excel->fileName = '文件名称';//设置文件名称,默认为时间戳
*$excel->format = '2007';//文件类型,默认为2007,其他为excel5
*$record = array(
	'delKey'=>array('id','addTime','status'),//如果数据$data中有不需要显示的列,可以在此说明。删除不需要导出的键值
	'sort'			=>array(
					'keyName'=>array('subjectName','flag'),//按keyName列排序,如果不存在则不排序。
					//'reorder'=>'DESC',//排序方式,DESC为倒序,ASC为正序。如果keyName存在则排序keyName,如果不存在则按数组的键名排序,如果reorder不存在则不排序
				),//排序   如果sort存在则排序,否则不排序,如果keyName存在则按设置排序,如果不存在则按字符排序,如果reorder不存在或为空或为DES则正序,等于DESC为倒序。
	'excelStyle'	=>array(
						'setName'=>'Arial',//字体样式
						'setSize'=>'12',//字体大小
					),//表格全局样式
	'title'			=>array('tableName'=>'学科列表','center'=>true,'direction'=>'right','merge'=>'2','setSize'=>'30'),//标题,center垂直,direction为合并方向。right,left,up,down。 merge为合并几个单元格,setSize为字体大小
	'data'			=>array(
						array('tableName'=>'科目名称','width'=>'30','setName'=>'宋体','setSize'=>'20','background'=>'red','textColor'=>'white','bold'=>true,'underline'=>true,'borderColor'=>'cyan','center'=>true,),
						array('tableName'=>'学科','width'=>'50','center'=>true),//颜色表是:black,white,red,green,blue,yellow,magenta,cyan
					),//表名称 tableName为名称,width为表格宽度,setName为字体样式,background为背景颜色,textColor为字体颜色,bold为加粗,underline为下划线,borderColor为边框颜色.
	'merge'			=>array(
						//'flag'=>array('keyword'=>'初','direction'=>'right','merge'=>'2'),merge的键值为需要处理数据数组的键值,keyword为如果存在此关键字才执行其他样式操作,如果keyword不存在则执行所有键值为flag的单元格。
						'all'=>array('width'=>'30','setName'=>'宋体','setSize'=>'20','background'=>'red','textColor'=>'white','bold'=>true,'underline'=>true,'borderColor'=>'cyan','center'=>true,),
					),//
);//导出配置


*$excel->export($record,$data);//$record为导出配置,$data为数据库的数据,$data可以为数组,也可以为对象。
*
*
*
*/
$address = dirname(dirname(__FILE__)).'/PHPExcel';
include $address.'/PHPExcel.class.php';
include $address.'/PHPExcel/Writer/Excel2007.php';
include $address.'/PHPExcel/Writer/Excel5.php';
include $address.'/PHPExcel/IOFactory.php';

/****************************
*生成excel文档。
*/

class excelC {
	
	public $format = '2007';//转换格式,默认为2007版本,其他版本,请输入不是2007的数字
	
	public $fileName;//文件名称默认为时间戳。
	
	
	private $objExcel;
	
	private $letters;
	
	public function __construct()
    {
		$this->fileName = time();
		
		$this->fileTitle = '导出数据';
		
		$this->objExcel = new PHPExcel();
		$this->letters = $this->letter();
	}
	
	
	//导出excel的属性
	private function attribute(){
		
		
		$this->objExcel->getProperties()->setCreator("力达行有限公司");//创建人
		
		$this->objExcel->getProperties()->setLastModifiedBy("力达行有限公司");//最后修改人
		
		$this->objExcel->getProperties()->setTitle("导出数据");//标题
		
		$this->objExcel->getProperties()->setSubject("导出数据");//题目
		
		$this->objExcel->getProperties()->setDescription("数据导出");//描述
		
		$this->objExcel->getProperties()->setKeywords("office 导出");//关键字
		
		$this->objExcel->getProperties()->setCategory("excel");//种类
	}
	
	
	
	//设置表(如果只有一个sheet可以忽略该函数,将默认创建。)
	private function sheet(){
		
		$this->objExcel->setActiveSheetIndex(0);//设置当前的表
		
		$this->objExcel->getActiveSheet()->setTitle('excel');//设置表名称。
	}
	
	
	/***************************
	*导出excel
	*@attr $record为表头及样式设置
	*@attr $data为需要导出的数据
	*/
	public function export($record=array(),$data=array()){
		if(!$data)return false;
		if(!is_array($record))return false;//表样式及其他设置
		
		//处理获取到的数据
		$data = $this->maniData($record,$data);
		
		//获取整体样式。
		$this->excelData($record,$data);
		
		
		//$this->objExcel->getActiveSheet()->setCellValue('A1', '季度'); 
		
		$this->down();//导出下载
	}
	
	
	/*
	*处理表格
	*/
	private function excelData(&$record,&$data){
		$this->attribute();//设置属性
		$this->sheet();//设置表
		$this->whole($record);//设置整体样式
		$this->tableHeader($record);//设置表格头。
		$this->tableContent($record,$data);//设置表格
		$this->excelTitle($record,2);//设置标题
	}
	
	/*
	*设置表格整体样式
	*/
	private function whole(&$record){
		if(!array_key_exists('excelStyle',$record))return false;
		
		$excelStyle = $record['excelStyle'];
		
		$default = $this->objExcel->getDefaultStyle();
		
		if(array_key_exists('setName',$excelStyle))
			$default->getFont()->setName($excelStyle['setName']);//设置字体样式
			
		if(array_key_exists('setSize',$excelStyle))
			$default->getFont()->setSize($excelStyle['setSize']);//设置字体大小
	}
	
	/*
	*设置标题
	*/
	private function excelTitle($record,$num){
		$titleL = $this->letters[0];
		if(!array_key_exists('title',$record))return false;
		$this->appOintStyle($titleL ,1,$record['title']);
		
	}
	
	/*
	*设置表格头。
	*/
	private function tableHeader($record){
		if(!array_key_exists('data',$record))return false;
		$objExcel = $this->objExcel;
		$letters = $this->letters;
		
		if(!is_array($record['data']))return false;
		
		$i = 0;
		$hang = 2;
		foreach($record['data'] as $k=>$v){
		
			$this->appOintStyle($letters[$i],$hang,$v);
				
			$i++;
		}
		
	}
	
	private function setCellValue($letter,$data){
	
		if(@$data)
			$this->objExcel->getActiveSheet()->setCellValue($letter, $data);//填充值
		
		return $this;
	}
	
	private function getColumnDimension($letter,$data){
	
		if(@$data)
			$this->objExcel->getActiveSheet()->getColumnDimension($letter)->setWidth($data);//设置宽度 
			
		return $this;
	}
	
	private function setName($letter,$data){
	
		if(@$data)
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFont()->setName($data);//设置字体
			
		return $this;
	}
	
	private function setSize($letter,$data){
	
		if(@$data)
			 $this->objExcel->getActiveSheet()->getStyle($letter)->getFont()->setSize($data);//设置字体大小
			
		return $this;
	}
	
	private function background($letter,$data){
	
		if(@$data){
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFill()->getStartColor()->setARGB($this->backColor($data));
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置背景色样式,无样式将不显示背景色。
		}
			
		return $this;
	}
	
	private function textColor($letter,$data){
	
		if(@$data){
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFont()->getColor()->setARGB($data);//字体颜色
		}
			
		return $this;
	}
	
	private function setBold($letter,$data){
	
		if(@$data){
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFont()->setBold(true);//加粗
		}
			
		return $this;
	}
	
	private function setUnderline($letter,$data){
	
		if(@$data){
			$this->objExcel->getActiveSheet()->getStyle($letter)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);//下划线
		}
			
		return $this;
	}
	
	private function border($letter,$data){
	
		if(@$data){
			$styleThinBlackBorderOutline = array(
						   'borders' => array (
								 'outline' => array (
									   'style' => PHPExcel_Style_Border::BORDER_THIN, //设置border样式
									   'color' => array ('argb' => $data),          //设置border颜色
								),
						  ),
					);
			$this->objExcel->getActiveSheet()->getStyle($letter)->applyFromArray($styleThinBlackBorderOutline);
		}
			
		return $this;
	}
	
	/*
	*合并
	*/
	private function mergeCells($letters,$hang,$direction,$merge){
		
		$merge = $merge-1;
		
		if($merge > 0 && $direction){
		
			//print_r($this->letters);
			$l = array_flip($this->letters);
			$ln = $l[$letters];
			
			switch ($direction)
			{
				case 'left':
					$signal = $this->letters[($ln-$merge)].$hang.':'.$letters.$hang;
				break;
				case 'right':
					$signal = $letters.$hang.':'.$this->letters[($ln+$merge)].$hang;
				break;
				case 'up':
					$signal = $letters.($hang-$merge).':'.$letters.$hang;
				break;
				case 'down':
					$signal = $letters.$hang.':'.$letters.($hang+$merge);
				break;
				default:
					$signal = '';
			}
			
			if($signal){
				$this->objExcel->getActiveSheet()->mergeCells($signal);
			}
			
		}
		
		return $this;
	}
	
	/*
	*垂直居中
	*/
	private function setVertical($letter,$data){
		
		if($data){
			$this->objExcel->getActiveSheet()->getStyle($letter)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
			$this->objExcel->getActiveSheet()->getStyle($letter)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		}
		return $this;
		
	}
	
	
	
	
	
	
	
	
	
	/*
	*设置颜色
	*/
	private function backColor($color){
		
		$array = array(
			'black'	=> 'FF000000',	//	System Colour #1 - Black
			'white'	=> 'FFFFFFFF',	//	System Colour #2 - White
			'red'	=> 'FFFF0000',	//	System Colour #3 - Red
			'green'	=> 'FF00FF00',	//	System Colour #4 - Green
			'blue'	=> 'FF0000FF',	//	System Colour #5 - Blue
			'yellow'	=> 'FFFFFF00',	//	System Colour #6 - Yellow
			'magenta'	=> 'FFFF00FF',	//	System Colour #7- Magenta
			'cyan'	=> 'FF00FFFF',	//	System Colour #8- Cyan
		);
		if(array_key_exists($color,$array)){
			return $array[$color];
		} else {
			return false;
		}
					
	}
	/*
	*设置表
	*/
	private function tableContent(&$record,&$data){
		$objExcel = $this->objExcel;
		$letters = $this->letters;
		
		if(array_key_exists('merge',$record))
			$merge = $record['merge'];
		else
			$merge = '';
					
		$hang = 2;
		foreach($data as $k=>$v){
			$i=0;
			$hang++;
			
			foreach($v as $kk=>$vv){
				$this->setCellValue($letters[$i].$hang, $vv);//设置内容
				$this->Appoint($kk,$vv,$letters[$i],$hang,$merge);
				$i++;
			}
			
		}
	}
	
	/*
	*设置表指定样式
	*/
	private function Appoint($kk,$vv,$letters,$hang,$merge){
		if(!$merge)return false;
		if(array_key_exists($kk,$merge)){
			$v = $merge[$kk];
			if(array_key_exists('keyword',$v)){
			
				if(strpos($vv,$v['keyword']) > -1){
					$this->appOintStyle($letters,$hang,$v);
				}
				
			} else {
				$this->appOintStyle($letters,$hang,$v);
			}
		} else if(array_key_exists('all',$merge)){
			$v = $merge['all'];
			if(array_key_exists('keyword',$v)){
			
				if(strpos($vv,$v['keyword']) > -1){
					$this->appOintStyle($letters,$hang,$v);
				}
				
			} else {
				$this->appOintStyle($letters,$hang,$v);
			}
		}
	}
	
	/*
	*终极样式
	*/
	private function appOintStyle($letters,$hang,$v){
	
		$this
				->setCellValue($letters.$hang,@$v['tableName'])
				->getColumnDimension($letters,@$v['width'])
				->setName($letters.$hang,@$v['setName'])
				->setSize($letters.$hang,@$v['setSize'])
				->background($letters.$hang,@$v['background'])
				->textColor($letters.$hang,$this->backColor(@$v['textColor']))
				->setBold($letters.$hang,@$v['bold'])
				->setUnderline($letters.$hang,@$v['underline'])
				->border($letters.$hang,$this->backColor(@$v['borderColor']))
				->mergeCells($letters,$hang,@$v['direction'],@$v['merge'])
				->setVertical($letters.$hang,@$v['center']);
	}
	
	/*
	*应为字母列表
	*/
	public function letter(){
		return array('A','B','C','D','F','G','H','I','G','K','L','M','N','O','P','Q','R','S','T','U','V','W','H','Y','Z');
	}
	
	/****************************
	*处理数据,排序及删除字段
	*/
	private function maniData($record,$data){
		if(!$data)return false;
		if(!is_array($record))return false;//表样式及其他设置
	
		$data = $this->objectToArray($data);//对象转数组
		
		$delKey = (array_key_exists('delKey',$record))?$record['delKey']:'';//是否删除关键字
		$sort = (array_key_exists('sort',$record))?$record['sort']:'';//是否排序
		$data = $this->delSort($data,$delKey,$sort);
		return $data;
	}
	
	/****************************
	*对象转数组
	*/
	private function objectToArray($data){
		if(!$data)return false;
		$data = (array)$data;
		
		foreach($data as $k=>$v){
			if(is_object($v) || is_array($v)){
				$data[$k] = (array)$this->objectToArray($v);
			}
		}
		return $data;
	}
	
	/****************************
	*删除键值,并排序
	*/
	private function delSort($data,$delKey='',$sort=''){
	
		if(!$data)return false;
		
		$array = array();
		foreach($data as $k=>$v){
		
			//删除数据中的某个键值
			$delData = $this->delData($v,$delKey);
			//按设定键值排序
			$sortData = $this->sortData($delData,$sort);
			$array[$k] = $sortData;
		}
		
		return $array;
		
	}
	
	/****************************
	*删除键值
	*/
	public function delData($data,&$delKey){
		if($delKey){
			foreach($delKey as $delVal){
				if(array_key_exists($delVal,$data))//判断键值是否存在
					unset($data[$delVal]);//清除键名。
			}
		}
		return $data;
	}
	
	/****************************
	*键值排序
	*/
	public function sortData($data,&$sort){
		$array = array();
		if($sort){
			if(array_key_exists('keyName',$sort)){
				$keyName = $sort['keyName'];
				if(array_key_exists('reorder',$sort)){
					if($sort['reorder'] == 'DESC'){
						krsort($keyName);
					} else if($sort['reorder'] == 'ASC'){
						ksort($keyName);
					}
				}
				foreach($keyName as $vn){
					$array[$vn] = (array_key_exists($vn,$data))?$data[$vn]:'';
				}
			} else {
				if(array_key_exists('reorder',$sort)){
					if($sort['reorder'] == 'DESC'){
						krsort($data);
					} else if($sort['reorder'] == 'ASC'){
						ksort($data);
					}
					$array = $data;
				}
			}
			
		}
		return $array;
	}
	
	
	
	//导出下载
	private function down(){
		
		if($this->format == '2007'):
			header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
			$excel = 'Excel2007';
		else:
			header('Content-Type: application/vnd.ms-excel'); 
			$excel = 'Excel5';
		endif;
		
	 	header("Content-Disposition: attachment; filename=\"$this->fileName\"");
	 	header('Cache-Control: max-age=0');
		
		$objWriter = PHPExcel_IOFactory::createWriter($this->objExcel, $excel);

		$objWriter->save('php://output');
		
	}
}
Copy after login

 

想用PHP导出excel表格,但是php环境是51*的,没办法用PHPExcel开源类,有啥办法?

你好,其实非常简单的问题,就是将要打印的页面的头设置为!

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=php100.xls");

这样,浏览器就直接下载该文件。将文件里面的









就直接放在Excel的表格里面了!

详情,请参考php100的教程。php100对这个问题有一个视频教程。

下载地址: www.php100.com/...0.html
 

PHPExcel生成Excel不可以读取

总结 php导出Excel php导入Excel PhpExcel使用说明 PhpExcel使用手册2009/03/06 上午 02:37方法一:特点,简单,省心,

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");

$tx='表头';
echo $tx."\n\n";
//输出内容如下:
echo "姓名"."\t";
echo "年龄"."\t";
echo "学历"."\t";
echo "\n";
echo "张三"."\t";
echo "25"."\t";
echo "本科"."\t";
?>

方法二: 引用google code中推荐的小类库(大体同方法一,比较复杂点)

code.google.com/p/php-excel/downloads/list

方法三: PHPEXCEL 类库,功能强大,支持win Excel2003 ,Win Excel2007.

www.codeplex.com/PHPExcel


//设置PHPExcel类库的include path
set_include_path('.'. PATH_SEPARATOR .
'D:\Zeal\PHP_LIBS' . PATH_SEPARATOR .
get_include_path());

/**
* 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要
* 打开对应行的注释。
* 如果使用 Excel5 ,输出的内容应该是GBK编码。
*/
require_once 'PHPExcel.php';

// uncomment
////require_once 'PHPExcel/Writer/Excel5.ph......余下全文>>
 

内容

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if there is data in the Excel table but the blanks are filtered? What should I do if there is data in the Excel table but the blanks are filtered? Mar 13, 2024 pm 06:38 PM

Excel is a frequently used office software. Many users record various data in the table, but the table clearly contains data and is blank when filtering. Regarding this problem, many users don’t know how to solve it. It doesn’t matter. , the content of this software tutorial is to provide answers to the majority of users. Users in need are welcome to check out the solutions. What should I do if there is data in the Excel table but the blanks are filtered? The first reason is that the table contains blank rows. We want to filter all people with the surname "Li", but we can see that the correct results are not filtered out because the table contains blank rows. How to deal with this situation? Solution: Step 1: Select all content and then filter. Press c

How to convert deepseek pdf How to convert deepseek pdf Feb 19, 2025 pm 05:24 PM

DeepSeek cannot convert files directly to PDF. Depending on the file type, you can use different methods: Common documents (Word, Excel, PowerPoint): Use Microsoft Office, LibreOffice and other software to export as PDF. Image: Save as PDF using image viewer or image processing software. Web pages: Use the browser's "Print into PDF" function or the dedicated web page to PDF tool. Uncommon formats: Find the right converter and convert it to PDF. It is crucial to choose the right tools and develop a plan based on the actual situation.

How to find average in Excel table How to find average in Excel table Aug 07, 2023 am 11:16 AM

How to find the average in an Excel table: 1. Use the AVERAGE function, select a blank cell and enter the function "=AVERAGE", and press the Enter key; 2. Use the SUM function and COUNT function, select a blank cell and enter the function "=SUM/COUNT", press the Enter key; 3. Use the IF function and the AVERAGE function, select a blank cell and enter the function "=AVERAGE, and press the Ctrl+Shift+Enter key combination.

The solution to the problem that Excel tables cannot be filtered The solution to the problem that Excel tables cannot be filtered Mar 14, 2024 am 10:20 AM

In daily life, I find that the Excel table cannot be filtered, what should I do? Excel is one of the most commonly used data processing tools, helping users to complete data entry and organization simply and quickly. In order to allow users to use Excel smoothly, let's take a look at the solutions to the problem that Excel tables cannot be filtered. 1. First open the Excel document and click the [View] option in the menu. 2. Click the [Freeze Window] option in the toolbar. 3. Then in the options that open, cancel the freeze setting. 4. You can re-screen after canceling.

What to do if the Excel table is not fully printed and part of it cannot be printed? What to do if the Excel table is not fully printed and part of it cannot be printed? Mar 20, 2024 pm 10:26 PM

At work, we often print the forms and documents we create into paper versions. Because the form content is too much and too long, sometimes all of it cannot be printed out. What should we do? Next, let’s learn together: excel form printing What to do if an incomplete part cannot be printed. Method/Step 1: First, we need to prepare the form to be printed and adjust it. While maintaining proportions, shorten the horizontal length as much as possible, as shown in the picture. 2. Then we click the &quot;File&quot; button in the upper left corner of the interface, as shown in the picture. 3. Then we click &quot;Print&quot; in the pop-up options, as shown in the picture. 4. After that, the print preview window will pop up, and we click &quot;Print Preview&quot; in it, as shown in the figure. 5. of

How to display two excel tables on the screen at the same time How to display two excel tables on the screen at the same time Mar 20, 2024 pm 01:40 PM

When using data, multiple tables may be involved. The data of several tables must be used back and forth. It is inconvenient to use one table and one window. This requires continuous table switching. If the required data tables can be displayed in one at the same time, On the screen, it saves a lot of time in repeatedly switching tables. Today we will take two tables as an example to see how the editor can magically display them on one screen. The course will begin immediately! In order to realize the function of displaying two tables at the same time, you can create two new Excel tables and name one be TEST1 and the other named TEST2. Open both forms and fill in some content as shown in the image below. 2. Open any table and click the &quot;View&quot; button in the upper toolbar.

How to unlock excel sheet if I forget my password? How to unlock excel sheet if I forget my password? Mar 20, 2024 am 10:19 AM

With the acceleration of the pace of modern life, when there are many passwords set, it is difficult to remember all the passwords. For example, if you forget the password for the Excel table you set, what should you do if you encounter this situation? It’s too much trouble to redo! Next, I will provide you with a solution, come and try it. First, find the password-protected worksheet Excel file, and then change the file name suffix from .xlsx to .rar. This will not have a major impact on the file. If prompted, you can ignore it. 2. Use a compressed file to open the Excel file that you have forgotten the password, and then follow the steps below: xl——worksheets——sheet1.

How to use deepseek to settle accounts How to use deepseek to settle accounts Feb 19, 2025 pm 04:36 PM

Question: Is DeepSeek available for accounting? Answer: No, it is a data mining and analysis tool that can be used to analyze financial data, but it does not have the accounting record and report generation functions of accounting software. Using DeepSeek to analyze financial data requires writing code to process data with knowledge of data structures, algorithms, and DeepSeek APIs to consider potential problems (e.g. programming knowledge, learning curves, data quality)

See all articles