How to export tables to Excel with PHP

Guanhui
Release: 2023-03-01 08:14:02
Original
3459 people have browsed it

How to export tables to Excel with PHP

How does PHP realize the export of table Excel

First download the PHPExcel package and introduce the package into the project; then instantiate the PHPExcel object; then Set the columns of the table and the queue attributes; then add the data to the table; and finally output the table file data.

PHPExcel

is a PHP class library used to operate Office Excel documents. It is based on Microsoft's OpenXML standard and PHP language . You can use it to read and write spreadsheets in different formats

ThinkPHP Example

public function exportExcel()
{
    //先获取数据
    $where['comid'] = session('uid');
    $res = M('cheliang')->where($where)->select();
   // var_dump($res);die;

    //下面就是导出的步骤了
    vendor('PHPExcel0.Classes.PHPExcel');

    $objPHPExcel = new \PHPExcel();
    $objPHPExcel->createSheet(0);
    $objPHPExcel->setActiveSheetIndex(0);
   //只需要把你想要的字段改成你自己需要的就可以了!!!
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '考试人')
            ->setCellValue('B1', '车牌')
            ->setCellValue('C1', '考试时间')
            ->setCellValue('D1', '考试分数')
            ->setCellValue('E1', '签名');

        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);//设置单元格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);//设置单元格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);//设置单元格宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);//设置单元格宽度
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);      //第一行是否加粗
        $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);      //第一行是否加粗
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setBold(true);      //第一行是否加粗
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setBold(true);      //第一行是否加粗
        $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setBold(true);      //第一行是否加粗
        //$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);         //第一行字体大小
        // 设置垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        // 设置行高度
        $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //设置默认行高
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);    //第一行行高
        //循环添加数据,注意的是下面的$kk+2,是因为$kk是下标,从0开始的,而第一行是你的标题,所以要从第二行开始才是你的数据
        foreach ($data as $kk => $vv) {
            $no = $kk + 2;
                $currentSheet = $objPHPExcel->getActiveSheet();
                $currentSheet->setCellValue('A' . $no, $vv['xueyuan']['name']);
                $currentSheet->setCellValue('B' . $no, $vv['xueyuan']['chepaihao']);
                $currentSheet->setCellValue('C' . $no, date('Y-m-d H:i:s',$vv['addtime']).'至'.date('Y-m-d H:i:s',$vv['sbttime']));
                $currentSheet->setCellValue('D' . $no, $vv['score']);
                //设置单元格高度,这个是重点哦
                $currentSheet->getRowDimension($no) -> setRowHeight(40);
                // 图片生成
                $objDrawing[$kk] = new \PHPExcel_Worksheet_Drawing();//这个就是生成图片的类(重点)
                $objDrawing[$kk]->setPath('./'.$vv['qmimg']);/图片的路径
                // 设置宽度高度
                $objDrawing[$kk]->setHeight(85);//照片高度
                $objDrawing[$kk]->setWidth(100); //照片宽度
                /*设置图片要插入的单元格*/
                $objDrawing[$kk]->setCoordinates('E'.$no);
                // 图片偏移距离
                 $objDrawing[$kk]->setOffsetX(12);
                 $objDrawing[$kk]->setOffsetY(12);
                $objDrawing[$kk]->setWorksheet($objPHPExcel->getActiveSheet());
        }

    $objPHPExcel->setActiveSheetIndex(0);
    header('Content-Type: application/vnd.ms-excel');

    header('Content-Disposition: attachment;filename="' . '文件名称'. '.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}
Copy after login

Recommended tutorial: "PHP Tutorial"

The above is the detailed content of How to export tables to Excel with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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