1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- //Excel操作类
- class Excel{
- public $excel;
- public $documentRoot;
- function __construct(){
- Yii::$enableIncludePath = false;
- $this->documentRoot = Yii::app()->basePath."/../";
- require_once($this->documentRoot."lib/plugins/phpexcel/PHPExcel.php");
- require_once($this->documentRoot."lib/plugins/phpexcel/PHPExcel/Writer/Excel2007.php");
- $this->excel = new PHPExcel();
- }
-
- public function export($rs, $fileName){
- $colNumber = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
- "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
-
- $excel = $this->excel;
- $writer = new PHPExcel_Writer_Excel2007($excel);
- $writer->setOffice2003Compatibility(true);
-
- $excel->getProperties()->setCreator("互教教育");
- $excel->getProperties()->setLastModifiedBy("互教教育");
- $excel->getProperties()->setTitle("Document");
- $excel->getProperties()->setSubject("Document");
- $excel->getProperties()->setDescription("Generated with PHP");
- $excel->getProperties()->setKeywords("office2007 openxml php");
- $excel->getProperties()->setCategory("result");
-
- $excel->setActiveSheetIndex(0);
- $sheet = $excel->getActiveSheet();
-
- $rowsCount = count($rs);
- $colsCount = count(current($rs));
- for($i = 0; $i < $rowsCount; $i++){
- for($j = 0; $j < $colsCount; $j++){
- if($j === 0)
- $val = current($rs[$i]);
- else
- $val = next($rs[$i]);
-
- $sheet->setCellValue($colNumber[$j].($i + 1), $val);
- }
- }
- ob_end_clean();
- ob_start();
- header("Content-Type: application/force-download");
- header("Content-Type: application/octet-stream");
- header("Content-Type: application/download");
- header("Content-Disposition:inline;filename={$fileName}.xlsx");
- header("Content-Transfer-Encoding: binary");
- header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
- header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
- header("Pragma: no-cache");
- $writer->save("php://output");
- }
-
- public function read($file){
- $sheetData = array();
-
- if($file){
- $mValues = "";
- $sValues = "";
- // 导入PHPExcel类
- require_once($this->documentRoot."lib/plugins/phpexcel/PHPExcel/IOFactory.php");
-
- // 读取Excel文档
- $objPHPExcel = PHPExcel_IOFactory::load($file);
- $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
-
- // 去掉导航
- unset($sheetData[1]);
- $sheetData = Filter::act($sheetData);
- //krsort($sheetData);
- }
-
- return $sheetData;
- }
- }
|