php 基于phpexcel制作的导出excel类,phpexcelexcel
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'); } }
你好,其实非常简单的问题,就是将要打印的页面的头设置为!
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=php100.xls");
这样,浏览器就直接下载该文件。将文件里面的
内容 |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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 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.

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.

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 "File" button in the upper left corner of the interface, as shown in the picture. 3. Then we click "Print" in the pop-up options, as shown in the picture. 4. After that, the print preview window will pop up, and we click "Print Preview" in it, as shown in the figure. 5. of

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 "View" button in the upper toolbar.

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.

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)
