zoukankan      html  css  js  c++  java
  • php原生PHPExcel插件导表(附表格合并,加粗居中及加边框换行操作)

    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 actionphpexcel;
    /**
    * 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');  
        }
    }

    (注意: titlesheet; cloumu表格标题 为以为数组[‘标题1’,’标题2’,’标题3’…,’标题n’]; dataarray0=>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
    )
    )
    )
    );

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

  • 相关阅读:
    Jetty 入门
    Spring MVC 学习 之
    Spring MVC 学习 之
    Spring MVC 学习 之
    call apply 使用
    maven学习系列 之 常见问题
    SQL Server数据库partition by 与ROW_NUMBER()函数使用详解[转]
    .NET 同步 异步 委托
    常用JS方法
    通过 NPOI 生成 Excel
  • 原文地址:https://www.cnblogs.com/xiaofei723/p/14924045.html
Copyright © 2011-2022 走看看