tp5.1/thinkPHP5.1/TP5.1 composer安装和使用PHPExcel

一.windows本地环境composer安装PHPExcel

首先:下载安装composer

其次 cmd切换到项目根目录 运行命令:composer require phpoffice/phpexcel

注意:

        1.运行可能会报错OpenSSL之类的 去php.ini中开启extension=php_openssl.dll扩展,去掉分号

        2.还有可能出现 找不到匹配的包的错误,这是因为我本地用的phpstudy的nts版本,这个版本比较特殊同时支持apache和nginx,只是本地虚拟环境开发测试用的PHP版本,所以匹配不到,应该切换正常版本在运行命令安装

tp5.1/thinkPHP5.1/TP5.1 composer安装和使用PHPExcel_第1张图片

    3.composer只支持php5.6以上版本

出现下图说明已经安装成功了

tp5.1/thinkPHP5.1/TP5.1 composer安装和使用PHPExcel_第2张图片

二、使用phpexcel

composer安装的phpexcel

只需要在头部use PHPExcel;

然后创建一个对象$objExcel = new \PHPExcel();

这样就可以使用了



三、

我直接封装了一个方法,只需要把要导出的数据的数组传进来,然后改一些字段和文案就可以了

function daysales_excel($driver){
    //导出表格
    $objExcel = new \PHPExcel();
    $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
    // 设置水平垂直居中
    $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
    // 字体和样式
    $objExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
    $objExcel->getActiveSheet()->getStyle('A2:AB2')->getFont()->setBold(true);
    $objExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
    // 第一行、第二行的默认高度
    $objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
    $objExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
    //设置某一列的宽度
    $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
    $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
    $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(5);
    $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
    $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
    $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
//设置表头
//  合并
$objExcel->getActiveSheet()->mergeCells('A1:I1');
$objActSheet = $objExcel->getActiveSheet(0);
$objActSheet->setTitle('用户统计');//设置excel的标题
$objActSheet->setCellValue('A1','日销售额统计');
$objActSheet->setCellValue('A2','标的名称');
$objActSheet->setCellValue('B2','标的金额');
$objActSheet->setCellValue('C2','标的利率(%)');
$objActSheet->setCellValue('D2','标的上线时间');
$objActSheet->setCellValue('E2','募集天数');
$objActSheet->setCellValue('F2','实际募集金额(元)');
$objActSheet->setCellValue('G2','超过部分');
$objActSheet->setCellValue('H2','融资人');

$baseRow = 3; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
foreach ( $driver as $r => $d ) {
    $i = $baseRow + $r;
    $objExcel->getActiveSheet()->setCellValue('A'.$i,$d['title']);
    $objExcel->getActiveSheet()->setCellValue('B'.$i,$d['amount']);
    $objExcel->getActiveSheet()->setCellValue('C'.$i,$d['user_interest_rate']);
    $objExcel->getActiveSheet()->setCellValue('D'.$i,$d['start_time']);
    $objExcel->getActiveSheet()->setCellValue('E'.$i,''.$d['duration_collect']);
    $objExcel->getActiveSheet()->setCellValue('F'.$i,$d['daysales_amount']);
    $objExcel->getActiveSheet()->setCellValue('G'.$i,$d['amount']-$d['daysales_amount']);
    $objExcel->getActiveSheet()->setCellValue('H'.$i,$d['debit_name']);
}
 $objExcel->setActiveSheetIndex(0);
    //4、输出
    $objExcel->setActiveSheetIndex();
    header('Content-Type: applicationnd.ms-excel');
    $time=date('YmdHis');
    header("Content-Disposition: attachment;filename=用户统计$time.xls");
    header('Cache-Control: max-age=0');
    $objWriter->save('php://output');
}

你可能感兴趣的:(TP5.0/5.1)