当前位置:网站首页>PHP batch Excel to word
PHP batch Excel to word
2022-07-01 02:48:00 【sdxjwkq01】
If there is excel form

The requirement is to fill in the personal information of each line into word In the document , And every life becomes a word file , It looks like this

Use php, This requirement is well implemented , about 20 A few lines of code , I wrote a blog about php analysis word Document , It is parsed with regular expressions , This time use a finished product directly ,phpoffice, You can use it with a simple introduction , This time I mainly used two bags ,phpword and phpexcel, Here's the code , It's very simple. There are notes , stay thinkphp Written in the frame , If you don't need a framework, you can include Just go .
<?php
namespace Home\Controller;
use Think\Controller;
set_time_limit(1800);
class IndexController extends Controller {
public function docx(){
vendor("PhpOffice.PHPExcel.IOFactory");// Import phpexcel Input and output classes
vendor("PhpOffice.PhpWord.TemplateProcessor");// Import phpword Template class
$sheet=\PHPExcel_IOFactory::createReader("Excel2007")->load("data.xlsx")->getActiveSheet();
$row=$sheet->getHighestRow();// Get the total number
$col=$sheet->getHighestColumn();// Get the total number of columns
$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 Template handle
$tmp->setValue('name',$value[0]);// Set the value to be replaced
$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"," Generate /"))){
mkdir(iconv("utf-8","gbk"," Generate /"),0777,true);// Build folder ,iconv Prevent folder from being garbled for conversion encoding
}
$tmp->saveAs(iconv("utf-8","gbk"," Generate /").$value[0].".docx");// Generate the file
unset($tmp);
}
die("success!");
}
}First of all, we have to prepare corresponding excel Tables and word Templates ,word The template is as follows

Run it , The effect is as follows

Then you can see the corresponding word The file has been generated

Open it and have a look. The corresponding information is also filled in

边栏推荐
- 如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
- Use ipmitool to configure BMC network and user information of X86 server
- Mouse over effect II
- Record a service deployment failure troubleshooting
- Restcloud ETl数据通过时间戳实现增量数据同步
- Image preloading in JS
- Zero foundation self-study SQL course | window function
- Visual effects, picture to cartoon function
- Mouse over effect I
- Résumé des styles de développement d'applets Wechat
猜你喜欢

Optimal transport Series 1
![Od modify DLL and exe pop-up contents [OllyDbg]](/img/ff/7249e6e6644376ae048b23bf63b046.jpg)
Od modify DLL and exe pop-up contents [OllyDbg]

Xception learning notes

How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?

Pychar open remote directory remote host

Go import self built package

Here comes the share creators budding talent training program!

在unity中使用jieba分词的方法

php批量excel转word

Restcloud ETL data realizes incremental data synchronization through timestamp
随机推荐
The mobile edge browser cannot open the third-party application
Visual effects, picture to cartoon function
DenseNet网络论文学习笔记
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
[2022] Jiangxi postgraduate mathematical modeling scheme and code
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
Dell服务器重启iDRAC方法
js中的图片预加载
C language a little bit (may increase in the future)
pycharm 软件deployment 灰色 无法点
Thread Detach
鼠标悬停效果二
Xception学习笔记
go: finding module for package
UE4 rendering pipeline learning notes
CentOS installs multiple versions of PHP and switches
视觉特效,图片转成漫画功能
Restcloud ETL实践之数据行列转换
Pulsar的Proxy支持和SNI路由