ali
- ali 2017-05-14 已签到连续签到1天,获得了5个金钱
我终于搞定了 谢谢你们宝贵的意见 提议
3Q 我试试
- ali 回复了 johnny1991 的回答
一个比较简单的例子,你参考一下
$objPHPExcel = new PHPExcel(); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw") ->setLastModifiedBy("Maarten Balliauw") ->setTitle("Office 2007 XLSX Test Document") ->setSubject("Office 2007 XLSX Test Document") ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") ->setKeywords("office 2007 openxml php") ->setCategory("Test result file"); // Add some data $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); // Miscellaneous glyphs, UTF-8 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A4', 'Miscellaneous glyphs') ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç'); // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="01simple.xls"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header ('Pragma: public'); // HTTP/1.0 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output');
谢谢你 我试试
- ali 发表了说说PHPExcel 导出做不了了 好烦
`
/** * * 导出Excel */
public function actionExport($month){//导出Excel
$xlsName = "Staffstatic"; $xlsCell = array( array('id','序号'), array('staffName','姓名'), array('staffCode','操作员'), array('month','考核月份'), array('total','业务量'), array('compute','业务量计算数') ); $laws=Protfolio::findOne(1); $compute=Staffstatic::find()->select([ "id", "staffCode", "staffName", "month", "createTime", "sum(total)", ])->groupBy(['staffName','staffCode'])->where(['month'=>$month])->asArray()->all(); $xlsData=null; foreach ($compute as $key=>$v){ $xlsData[]=array( "id"=>$v["id"], "staffCode"=>$v["staffCode"], "staffName"=>$v["staffName"], "month"=>$v["month"], "createTime"=>$v["createTime"], "total"=>$v["sum(total)"], "compute"=>$v["sum(total)"]*$laws['protfolio'] ); } $this->exportExcel($xlsName,$xlsCell,$xlsData); }
public function exportExcel($xlsName,$xlsCell,$xlsData){
header("Content-type:text/html;charset=utf-8"); set_time_limit(0); require dirname(dirname(__DIR__)).'/vendor/PHPExcel/PHPExcel.php'; $xlsTitle = iconv('utf-8', 'gb2312', $xlsName);//文件名称 $fileName = date('YmdHis');//or $xlsTitle 文件名称可根据自己情况设定 $cellNum = count($xlsCell); $dataNum = count($xlsData); $objPHPExcel = new \PHPExcel(); $objPHPExcel = new \PHPExcel(); $cellName = 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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
// $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $xlsName.' Export time:'.date('Y-m-d H:i:s')); for($i=0;$i<$cellNum;$i++){ $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $xlsCell[$i][1]); } // Miscellaneous glyphs, UTF-8 for($i=0;$i<$dataNum;$i++){ for($j=0;$j<$cellNum;$j++){ $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $xlsData[$i][$xlsCell[$j][0]]); } } header('pragma:public'); header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"'); header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); exit(); $objWriter->save('php://output'); }
`
1、下载PHPExcel解压到extensions,修改PHPExcel/PHPExcel_Autoloader解决自动载入问题
public static function Register() { /*if (function_exists('__autoload')) { //Register any existing autoloader function with SPL, so we don't get any clashes spl_autoload_register('__autoload'); } //Register ourselves with SPL return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));*/ $functions = spl_autoload_functions(); foreach ($functions as $function) spl_autoload_unregister($function); $functions = array_merge(array(array('PHPExcel_Autoloader', 'Load')), $functions); foreach ($functions as $function) $x = spl_autoload_register($function); return $x; } // function Register()
2、修改config/main.php
'import' => array( 'application.models.*', 'application.components.*', 'application.extensions.*', ),
3、创建components/Excel.php
class Excel { private $phpExcel; function __construct(PHPExcel $phpExcel) { $this->phpExcel = $phpExcel; } /** * 根据文件名获取PHPExcel对象 * @param $file * @return PHPExcel */ public function getByFile($file) { $reader = PHPExcel_IOFactory::createReader('Excel5'); $excel = $reader->load($file); return $excel; } }
4、使用
$excel = new Excel(new PHPExcel());
我按照你的写法来 写了一下 报错不行了 求源码 Email:1844594430@qq.com
求源码 Email:1844594430@qq.com
- ali 2017-05-11 已签到连续签到2天,获得了10个金钱