使用phpexcel导出excel和phpword导出word--简单使用

<?php
namespace app\index\controller;

//离线环境不能使用composer安装,只能下载包文件,然后放在vendor下,代码中require使用
require_once VENDOR_PATH.‘/PHPExcel/PHPExcel.php‘;

use app\index\controller\Base;

class Phpexcel extends Base{

    public function __construct(){
        parent::__construct();
    }

    public function getExcel(){
        $pexcel = new \PHPExcel();
        $pexcel -> setActiveSheetIndex(0);//设置sheet序号
        $pexcel -> getActiveSheet() -> setTitle(‘电信网络诈骗信息表‘);//设置sheet的名称
        $pexcel -> getActiveSheet() -> setCellValue(‘A1‘,‘案件编号‘);//设置A1内容
     $pexcel -> getActiveSheet() -> mergeCells(‘A1:A3‘);//合并A1到A3列,合并行一样的写法
     $pexcel -> getDefaultStyle() -> getAlignment() -> setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
     $pexcel -> getActiveSheet() -> getStyle(‘A1‘) -> getAlignment() -> setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//A1水平居中
     $pexcel -> getActiveSheet() -> getStyle(‘A1‘) -> getFill() 
        -> applyFromArray(array(‘type‘=> \PHPExcel_Style_Fill::FILL_SOLID,‘color‘ => array(‘rgb‘ => ‘ADD8E6‘)));//设置A1的填充颜色

        $filename = date(‘YmdHis‘).‘.xls‘;//文档名称
        //设置输出格式,写入到输出流
        $xlsWrite = new \PHPExcel_Writer_Excel5($pexcel);
        header("Content-Type:application/force-download");
        header("Content-Type:application/octet-stream");
        header("Content-Disposition:attachment;filename=‘".$filename."‘");
        header("pragma:no-cache");
        $xlsWrite->save("php://output");
    }
  
   public function getWord(){
      $phpword = new \PhpWord();
      $phpword -> setDefaultFontName(‘仿宋‘);//设置字体
       $phpword -> setDefaultFontSize(16);//设置字号
      $fontcolor = array(
        "color" => ‘#FF0000‘
      );//颜色
      $section = $phpword -> createSection();
      //section的addText方法生成的是段落,下面两句自带换行
       $section -> addText("问:",$fontcolor);//设置内容及内容的颜色
      $section-> addText("答:");
      //section的TextRun的addText方法生成的是一个字符串,没有换行,下面两句连成一个字符串
      $textrun = $section -> createTextRun();
      $textrun -> addText("第1个是电话");
      $textrun -> addText("第2个是微信");
 
      $filename = date(‘YmdHis‘).‘.doc‘;
      header("Content-Description:File Transfer");
       header(‘Content-Disposition:attachment;filename=‘.$filename);
       header("Expires:0");
       $xmlWriter = \PHPWord_IOFactory::createWriter($phpword,‘Word2007‘);
       $xmlWriter -> save(‘php://output‘);
    }
}

  

使用phpexcel导出excel和phpword导出word--简单使用

上一篇:[vue报错vue-router.esm.js?8c4f:2007 Uncaught (in promise) NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}]


下一篇:thinphp5,模型调用模型,和控制器调用模型的区别