4 Star 14 Fork 2

李春林 / phpoffice-phpword-phpexcel 独立版 PHP56 兼容WIN LINUX

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

适用范围

  • 需要使用phpword 但又没有安装compower或者不喜欢composer的童鞋
  • phpword 源码地址 github
  • phexcel 源码地址 github
  • common 源码地址 gighub

为什么不是 PhpSpreadsheet 为了向下兼容^_^

方法

  • 获取common公共文件 src/Common
  • 获取phpword类文件src/PhpWord
  • 获取phpexcel类文件Classes
  • 组装成如下目录
phpoffice(任意目录)
│	├─common (公共类)
│	├─phpexcel 
│	├─phpword 
│	├─phpexcel.php 
│	└─phpword.php
└─readme.md

构造加载文件

<?php
spl_autoload_register(function ($class) {
    $class = ltrim($class, '\\');
    $prefix = 'PhpOffice\PhpWord';
    if (strpos($class, $prefix) === 0) {
        $class = str_replace('\\', DIRECTORY_SEPARATOR, $class);
        $class = implode(DIRECTORY_SEPARATOR, array('phpword','PhpWord')) . substr($class, strlen($prefix));
        $file = __DIR__ . DIRECTORY_SEPARATOR . $class . '.php';
        var_dump($file);
        if (file_exists($file)) {
            require_once $file;
        }
    }
});

require_once __DIR__ . "/common/Autoloader.php";
\PhpOffice\Common\Autoloader::register();

使用

include('phpoffice/phpword.php');
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
# code...干你该干的事

技巧

  • phpword替换图片
include('phpoffice/phpword.php');

$PHPWord = new \PhpOffice\PhpWord\PhpWord();
//加载
$tempPlete = $PHPWord->loadTemplate($doc_file);
//替换 word里的字符为 ${abc} 
//照片
$tempPlete->setImageValue('photo_url', ['path' => $v,'width'=>120,'height'=>170]); 
//普通
$tempPlete->setValue('field','abc');
//输出
$filename = "[{$data['cat_text']}]-{$data['name']}.docx";
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename='.$filename);
header('Cache-Control: max-age=0');
$tempPlete->saveAs('php://output');//直接下载
//保存
//$tempPlete->saveAs($save_path.(iconv('utf-8','GB2312//IGNORE',$filename)));
  • phpexcel使用
require_once('phpoffice/phpexcel.php');
$objPHPExcel = new \PHPExcel();
# code...干你该干的事
$filename = "xxx.xls";
$objWrite = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWrite->save($save_path.(iconv('utf-8','GB2312//IGNORE',$filename)));
return false;

空文件

简介

适用范围 需要使用`phpword` 但又没有安装`compower`或者不喜欢`composer`的 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/nbnat/phpoffice.git
git@gitee.com:nbnat/phpoffice.git
nbnat
phpoffice
phpoffice-phpword-phpexcel 独立版 PHP56 兼容WIN LINUX
master

搜索帮助