PHPExcel的使用
1.当然是下载PHPexcel的插件了 http://phpexcel.codeplex.com/
2.应用插件 我把插件和需要用到的excel模板放的是不同文件夹的,excel我放在public下的upload文件夹里,excel插件我放在vendor文件里
3.控制器的引用方法,这些应该是必须的
注:因为我用的是thinkcmf5,里面有自带的phpexcel的方法,所以你们用其他框架的我不知道可不可以用,如果不可以用,请把Loader改为
1 vendor('PHPExcel.Classes.PHPExcel'); 2 vendor('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
上面的如果读不出来,可能就是里面的路径没写对
1 $dir = 'upload/'; //模板存放目录 2 $templateName = 'account.xlsx'; //模板表 3 $path = dirname(__FILE__); //找到当前脚本所在路径 4 Loader::import('PHPExcel.Classes.PHPExcel'); 5 Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory'); 6 7 $PHPExcel = new \PHPExcel(); 8 $PHPReader = new \PHPExcel_Reader_Excel2007(); 9 //读取Excel 10 $PHPExcel = $PHPReader->load($dir.$templateName); 11 $currentSheet = $PHPExcel->getSheet(0);
4.样式设置,你也可以不设置
2 //设置水平居中 3 $PHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal( \PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 4 $PHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 5 $PHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 6 $PHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 7 $PHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 8 $PHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 9 $PHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 10 $PHPExcel->getActiveSheet()->getStyle('H')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 11 $PHPExcel->getActiveSheet()->getStyle('I')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 12 $PHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 13 $PHPExcel->getActiveSheet()->getStyle('K')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
5.把数据导入到excel模板里面,我的是个二维数组
1 for($i=0;$i<count($deal);$i++){ 2 $currentSheet->setCellValue('A'.($i+3), date('Y-m-d H:i:s', $deal[$i]['create_time'])); //日期 3 $currentSheet->setCellValue('B'.($i+3),$deal[$i]['deal_type']); //交易类型 4 $currentSheet->setCellValue('C'.($i+3),$deal[$i]['roll_name']); //支出账户名称 5 $currentSheet->setCellValue('D'.($i+3),$deal[$i]['roll_user']); //支出账号 6 $currentSheet->setCellValue('E'.($i+3),$deal[$i]['receive_name']); //收入账号名称 7 $currentSheet->setCellValue('F'.($i+3),$deal[$i]['receive_user']); //收入账户 8 $currentSheet->setCellValue('G'.($i+3),$deal[$i]['account']); //交易金额 9 $currentSheet->setCellValue('H'.($i+3),$deal[$i]['roll_account']); //支出账号交易后余额 10 $currentSheet->setCellValue('I'.($i+3),$deal[$i]['receive_account']); //收入账号交易后余额 11 $currentSheet->setCellValue('J'.($i+3),$deal[$i]['user']); //操作员 12 $currentSheet->setCellValue('K'.($i+3),$deal[$i]['remark']); //订单备注 13 }
6.这样就可以下载下来了,有两种输出格式,excel输出是有表格线条的,网页输出没有表格线条
1 ob_clean(); 2 ob_start(); 3 $objWriteHTML = new \PHPExcel_Writer_Excel2007($PHPExcel); //输出EXCLl格式的对象 4 header("Content-type: application/octet-stream;charset=utf-8"); 5 header('Content-Type: application/vnd.ms-excel'); 6 header('Content-Disposition: attachment;filename="funds.xlsx"'); //日期为文件名后缀 7 header('Cache-Control: max-age=0'); 8 $objWriteHTML->save("php://output"); 9 10 exit;//退出
7.完成
人生得意须尽欢,莫使金樽空对月.