php excel 模板 导出word,使用phpexcel导出excel和phpword导出word--简单使用

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‘);

}

}

原文:https://www.cnblogs.com/echobao/p/13085106.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值