Home > Backend Development > PHP Tutorial > Detailed introduction to the method of exporting data to Excel using phpExcel

Detailed introduction to the method of exporting data to Excel using phpExcel

不言
Release: 2023-04-04 21:30:02
forward
7761 people have browsed it

This article brings you a detailed introduction to the method of exporting data to Excel with phpExcel. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Install the class library

Download the PHPExcel class library from GitHub

Address: https://github.com/PHPOffice/PHPExcel

Detailed introduction to the method of exporting data to Excel using phpExcel

After unzipping, move the Classes folder to the extend directory of ThinkPHP and rename it to phpexcel

Detailed introduction to the method of exporting data to Excel using phpExcel

Detailed introduction to the method of exporting data to Excel using phpExcel

Add references where needed in the project

import('phpexcel.PHPExcel', EXTEND_PATH);
Copy after login

Code implementation

getProperties()->setCreator(''); //设置创建者
        $obj->getProperties()->setLastModifiedBy(''); //设置修改者
        $obj->getProperties()->setTitle(''); //设置标题
        $obj->getProperties()->setSubject(''); //设置主题
        $obj->getProperties()->setDescription(''); //设置描述
        $obj->getProperties()->setKeywords('');//设置关键词
        $obj->getProperties()->setCategory('');//设置类型

        // 设置当前sheet
        $obj->setActiveSheetIndex(0);

        // 设置当前sheet的名称
        $obj->getActiveSheet()->setTitle('student');

        // 列标
        $list = ['A', 'B', 'C'];

        // 填充第一行数据
        $obj->getActiveSheet()
            ->setCellValue($list[0] . '1', '学号')
            ->setCellValue($list[1] . '1', '姓名')
            ->setCellValue($list[2] . '1', '班级');

        // 填充第n(n>=2, n∈N*)行数据
        $length = count($data);
        for ($i = 0; $i < $length; $i++) {
            $obj->getActiveSheet()->setCellValue($list[0] . ($i + 2), '20190101', \PHPExcel_Cell_DataType::TYPE_STRING);//将其设置为文本格式
            $obj->getActiveSheet()->setCellValue($list[1] . ($i + 2), 'student01');
            $obj->getActiveSheet()->setCellValue($list[2] . ($i + 2), '1班');
        }

        // 设置加粗和左对齐
        foreach ($list as $col) {
            // 设置第一行加粗
            $obj->getActiveSheet()->getStyle($col . '1')->getFont()->setBold(true);
            // 设置第1-n行,左对齐
            for ($i = 1; $i <= $length + 1; $i++) {
                $obj->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
            }
        }

        // 设置列宽
        $obj->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $obj->getActiveSheet()->getColumnDimension('C')->setWidth(15);

        // 导出
        ob_clean();
        if ($fileType == 'xls') {
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $fileName . '.xls');
            header('Cache-Control: max-age=1');
            $objWriter = new \PHPExcel_Writer_Excel5($obj);
            $objWriter->save('php://output');
            exit;
        } elseif ($fileType == 'xlsx') {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx');
            header('Cache-Control: max-age=1');
            $objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
            $objWriter->save('php://output');
            exit;
        }
    }


    // 准备数据
    protected function getData()
    {
        $studentList = [
            [
                'stuNo' => '20190101',
                'name' => 'student01',
                'class' => '1班'
            ], [
                'stuNo' => '20190102',
                'name' => 'student02',
                'class' => '1班'
            ], [
                'stuNo' => '20190103',
                'name' => 'student03',
                'class' => '1班'
            ]
        ];

        return $studentList;
    }
}
Copy after login

Run

Browser access http:// 127.0.0.1:8083/index/excel/exportExcelDownload the Excel file

Detailed introduction to the method of exporting data to Excel using phpExcel

Open the file as follows:

Detailed introduction to the method of exporting data to Excel using phpExcel

You can view it The data in Excel is consistent with the data in the PHP array!

The above is the detailed content of Detailed introduction to the method of exporting data to Excel using phpExcel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template