thinkphp5.1.39导出excel功能

进入项目目录,使用composer安装PHPExcel  

composer require phpoffice/phpexcel

在common.php中封装导出execel功能

最重要的步骤 引入 think\Loader 然后用

//加载第三方类库
Loader::autoload('PHPExcel.Classes.PHPExcel');
Loader::autoload('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');

具体代码如下


// +----------------------------------------------------------------------
use think\Loader;

// 应用公共文件
function excelFileExport($data = [],$title='')
{
    //文件名
    $fileName = $title.date("Y-m-d",time()) . ".xls";
    //加载第三方类库
    Loader::autoload('PHPExcel.Classes.PHPExcel');
    Loader::autoload('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
    //实例化excel类
    $excelObj = new \PHPExcel();
    //构建列数--根据实际需要构建即可
    $letter = array('A', 'B', 'C', 'D');
    //表头数组--需和列数一致
    $tableheader = array('用户名','标题', 'URL', '浏览器');
    //填充表头信息
    for ($i = 0; $i < count($tableheader); $i++) {
        $excelObj->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]");
    }
    //循环填充数据
    foreach ($data as $k => $v) {
        $num = $k + 1 + 1;
        //设置每一列的内容
        $excelObj->setActiveSheetIndex(0)
            ->setCellValue('A' . $num, $v['username'])
            ->setCellValue('B' . $num, $v['title'])
            ->setCellValue('C' . $num, $v['url'])
            ->setCellValue('D' . $num, $v['useragent']);

            //设置行高
            $excelObj->getActiveSheet()->getRowDimension($k+4)->setRowHeight(30);
    }
    //以下是设置宽度
    $excelObj->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    $excelObj->getActiveSheet()->getColumnDimension('B')->setWidth(30);
    $excelObj->getActiveSheet()->getColumnDimension('C')->setWidth(40);
    $excelObj->getActiveSheet()->getColumnDimension('D')->setWidth(100);

    //设置表头行高
    $excelObj->getActiveSheet()->getRowDimension(1)->setRowHeight(28);
    $excelObj->getActiveSheet()->getRowDimension(2)->setRowHeight(28);
    $excelObj->getActiveSheet()->getRowDimension(3)->setRowHeight(28);

    //设置居中
    $excelObj->getActiveSheet()->getStyle('A1:D1'.($k+2))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    //所有垂直居中
    $excelObj->getActiveSheet()->getStyle('A1:D1'.($k+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

    //设置字体样式
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setName('黑体');
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setSize(20);
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setName('宋体');
    $excelObj->getActiveSheet()->getStyle('A1:D1')->getFont()->setSize(16);
    $excelObj->getActiveSheet()->getStyle('A1:D1'.($k+2))->getFont()->setSize(10);

    //设置自动换行
    $excelObj->getActiveSheet()->getStyle('A1:D1'.($k+2))->getAlignment()->setWrapText(true);

    // 重命名表
    $fileName = iconv("utf-8", "gb2312", $fileName);

    // 设置下载打开为第一个表
    $excelObj->setActiveSheetIndex(0);

    //设置header头信息
    header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
    header("Content-Disposition: attachment;filename={$fileName}");
    header('Cache-Control: max-age=0');
    $writer = \PHPExcel_IOFactory::createWriter($excelObj, 'Excel5');
    $writer->save('php://output');
    exit();

}

 

 

点击导出按钮,

然后到控制器处理 ,调用上面封装的方法
thinkphp5.1.39导出excel功能_第1张图片

 

读出数据 组装成数组

thinkphp5.1.39导出excel功能_第2张图片

导出如下图

thinkphp5.1.39导出excel功能_第3张图片

thinkphp5.1.39导出excel功能_第4张图片

具体可以参考 https://jingyan.baidu.com/article/ac6a9a5ed74f922b653eacea.html

 

 

你可能感兴趣的:(thinkphp5.1.39导出excel功能)