当前位置:网站首页>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文件已经生成了

打开看一下相应的信息也填进去了

边栏推荐
- Mouse over effect 9
- Proxy support and SNI routing of pulsar
- Optimal transport Series 1
- Big orange crazy blog move notice
- go: finding module for package
- C # generates PPK files in putty format (supports passphrase)
- Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
- Pulsar的Proxy支持和SNI路由
- Mouse over effect IV
- [PR # 5 A] two way running (state pressure DP)
猜你喜欢

Applet custom top navigation bar, uni app wechat applet custom top navigation bar

视觉特效,图片转成漫画功能

js中的图片预加载

(summary I) Halcon Foundation's target finding features + becoming a regular

Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021

The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer

Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)

Pychar open remote directory remote host

SWT / anr problem - binder stuck

Sampling Area Lights
随机推荐
UE4渲染管线学习笔记
Thread Detach
详解数据治理知识体系
Restcloud ETl数据通过时间戳实现增量数据同步
Ernie gram, an explicit and complete n-gram mask language model, implements explicit n-gram semantic unit knowledge modeling.
DenseNet网络论文学习笔记
Big orange crazy blog move notice
Optimal Transport系列1
nacos配置中心使用教程
5款主流智能音箱入门款测评:苹果小米华为天猫小度,谁的表现更胜一筹?
How to buy Hong Kong shares in China? What platform is safer?
How do I open an account on my mobile phone? Also, is it safe to open an account online?
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
kubernetes资源对象介绍及常用命令(二)
The latest wechat iPad protocol code obtains official account authorization, etc
CentOS installs multiple versions of PHP and switches
7_ Openresty installation
Pychart software deployment gray unable to point
Share Creators萌芽人才培养计划来了!
Pulsar theme compression