当前位置:网站首页>php批量excel转word
php批量excel转word
2022-07-01 02:45:00 【sdxjwkq01】
假如有如下excel表格
需求是把每一行的个人信息填写到word文件里,并且每个人生成一个word文档,就像下面这样
使用php,这个需求很好实现,大约20几行代码,之前我写过一篇博客关于php解析word文档的,是用的正则表达式解析的,这次直接用个成品,phpoffice,只需要简单的引入就可以使用了,这次主要用了两个包,phpword和phpexcel,下面还是上代码,很简单都有注释,在thinkphp框架里写的,不用框架的话直接include就行。
<?php
namespace Home\Controller;
use Think\Controller;
set_time_limit(1800);
class IndexController extends Controller {
public function docx(){
vendor("PhpOffice.PHPExcel.IOFactory");//导入phpexcel输入输出类
vendor("PhpOffice.PhpWord.TemplateProcessor");//导入phpword模板类
$sheet=\PHPExcel_IOFactory::createReader("Excel2007")->load("data.xlsx")->getActiveSheet();
$row=$sheet->getHighestRow();//取得总行数
$col=$sheet->getHighestColumn();//取得总列数
$data=[];
for($i=0;$i<$row;$i++){
for($j=0;$j<=4;$j++){
$data[$i][$j]=$sheet->getCellByColumnAndRow($j, $i+1)->getValue();
}
}
foreach ($data as $key => &$value) {
$tmp=new \PhpOffice\PhpWord\TemplateProcessor('tmp.docx');//word模板句柄
$tmp->setValue('name',$value[0]);//设置需要替换的值
$tmp->setValue('sex',$value[1]);
$tmp->setValue('nation',$value[2]);
$tmp->setValue('birthday',$value[3]);
$tmp->setValue('school',$value[4]);
if(!is_dir(iconv("utf-8","gbk","生成/"))){
mkdir(iconv("utf-8","gbk","生成/"),0777,true);//生成文件夹,iconv为转换编码防止文件夹乱码
}
$tmp->saveAs(iconv("utf-8","gbk","生成/").$value[0].".docx");//生成文件
unset($tmp);
}
die("success!");
}
}
首先还得准备相应的excel表格和word模板,word模板如下
运行了下,效果如下
然后可以看到相应的word文件已经生成了
打开看一下相应的信息也填进去了
边栏推荐
- Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
- Voici le programme de formation des talents de SHARE Creators!
- C # generates PPK files in putty format (supports passphrase)
- Dell服务器重启iDRAC方法
- Rocketqa: cross batch negatives, de noised hard negative sampling and data augmentation
- import tensorflow.contrib.slim as slim报错
- What are the top ten securities companies? In addition, is it safe to open an account online now?
- Mouse over effect 10
- Mouse over effect V
- Restcloud ETL实践之数据行列转换
猜你喜欢
Machine learning 9-universal approximator radial basis function neural network, examining PDA and SVM from a new perspective
The mobile edge browser cannot open the third-party application
Restcloud ETl数据通过时间戳实现增量数据同步
Zero foundation self-study SQL course | window function
(summary I) Halcon Foundation's target finding features + becoming a regular
记一次服务部署失败问题排查
最新微信ipad协议 CODE获取 公众号授权等
旷世轻量化网络ShuffulNetV2学习笔记
Restcloud ETL practice data row column conversion
How to use Jieba participle in unity
随机推荐
Mouse over effect III
Dell server restart Idrac method
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
在国内如何买港股的股?用什么平台安全一些?
Résumé des styles de développement d'applets Wechat
Viewing JVM parameters
pycharm 软件deployment 灰色 无法点
鼠标悬停效果七
A small document of JS method Encyclopedia
The latest wechat iPad protocol code obtains official account authorization, etc
Ipmitool download address and possible problems during compilation and installation
鼠标悬停效果三
Some uses of Halcon array
nacos配置中心使用教程
Xception learning notes
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
Analysis and solution of anr problems
Use ipmitool to configure BMC network and user information of X86 server
JS anti shake and throttling
手机上怎么开户?还有,在线开户安全么?