php原生PHPExcel插件导表(附表格合并,加粗居中及加边框换行操作)
阅读原文时间:2023年07月11日阅读:1

PHPExcel是用来操作Office Excel文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel(BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV,Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。

1首先下载PHPExcel插件 地址:https://github.com/PHPOffice/PHPExcel

2 导入项目中 我的目录结构如下 :

3,此代码可以兼容多种类型的表格:

<?php
namespace action\phpexcel;
/**
* Excel表   导出
*/
class excelAction extends \action
{

    /*
     *title   excel標題
     *column  表格栏目
     *OrdersData    表格数据
     *footer  表格结束行显示文字
     */
    public function  exportexcel($title,$column,$OrdersData,$footer){

        /*导入核心类*/
        include 'PHPExcel.php';
        require_once 'PHPExcel/Writer/Excel5.php';         // 用于其他低版本xls
        require_once 'PHPExcel/Writer/Excel2007.php';     // 用于 excel-2007 格式
         // Create new PHPExcel object
        $objPHPExcel = new \PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setTitle($title);  

        //set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(45);
        $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('S')->setWidth(20);

          $arr=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
          //合并单元格
          $objPHPExcel->getActiveSheet()->mergeCells('A1:Z1');
        //设置表格头部栏目 set table header content
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $title.'  时间:'.date('Y-m-d H:i:s'));
        foreach ($column as $k => $v) {
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($arr[$k].'2',$v);
        }

        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<count($OrdersData);$i++){  

            foreach ($OrdersData[$i] as $k => $va) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($arr[$k].($i+3), $va);
            }
        }
        //表格底部栏目
        $objPHPExcel->getActiveSheet()->mergeCells('A'.($i+3).':Z'.($i+3));
          $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $footer);

        //  sheet命名
        $objPHPExcel->getActiveSheet()->setTitle($title);  

        /* 生成到浏览器,提供下载 */
        ob_end_clean();  //清空缓存
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$title.'('.date('Ymd-His').').xls"');  //日期为文件名后缀
        header('Cache-Control: max-age=0');  

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls格式,excel2007为xlsx格式
        $objWriter->save('php://output');
    }
}

(注意:

title可以设置文件名称,标题,底部sheet;
cloumu表格标题 为以为数组[‘标题1’,’标题2’,’标题3’…,’标题n’];

data数据为二维数组array(0=>array(′′,′′,′′,′′…,′′),2=>array(′′,′′,′′,′′…,′′)……);
footer 设置表格底部的内容)

效果如图

合并单元格:
$objPHPExcel->getActiveSheet()->mergeCells(‘A1:F1’); //合并

$objPHPExcel->getActiveSheet()->unmergeCells(‘A1:F1’); // 拆分

加粗和居中
$objPHPExcel->getActiveSheet()->getStyle(‘A1’)->applyFromArray(
array(
‘font’ => array (
‘bold’ => true
),
‘alignment’ => array(
‘horizontal’ => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
)
)
);

加边框
$objPHPExcel->getActiveSheet()->getStyle(‘A2:F2’)->applyFromArray(
array(
‘borders’ => array(
‘top’ => array(
‘style’ => PHPExcel_Style_Border::BORDER_THIN
)
)
)
);

表格导出时单元格换行效果:数据中需带有换行符( \n )
$objPHPExcel->setActiveSheetIndex(0)->getStyle(‘A1’)->getAlignment()->setWrapText(true);