当前位置:网站首页>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

边栏推荐
猜你喜欢

kubernetes资源对象介绍及常用命令(二)

详解数据治理知识体系

旷世轻量化网络ShuffulNetV2学习笔记

Share Creators萌芽人才培养计划来了!

Xception learning notes

How to use Jieba participle in unity

The latest wechat iPad protocol code obtains official account authorization, etc

Detailed data governance knowledge system

Nacos configuration center tutorial
![[2022] Jiangxi postgraduate mathematical modeling scheme and code](/img/f4/86b0dc2bd49c3a54c1e0538b31d458.png)
[2022] Jiangxi postgraduate mathematical modeling scheme and code
随机推荐
Analysis and solution of anr problems
在国内如何买港股的股?用什么平台安全一些?
[PR # 5 A] two way running (state pressure DP)
Restcloud ETL practice data row column conversion
如何在智汀中實現智能鎖與燈、智能窗簾電機場景聯動?
Focusing on green and low carbon, data center cooling has entered a new era of "intelligent cooling"
旷世轻量化网络ShuffulNetV2学习笔记
js防抖和节流
Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
基于Pytorch完整的训练一个神经网络并进行验证
kubernetes资源对象介绍及常用命令(二)
Here comes the share creators budding talent training program!
I want to know how to open a stock account? Is it safe to open an account online?
Find the length of the common part of two line segments
Mouse over effect I
RestCloud ETL WebService数据同步到本地
基于OPENCV和图像减法的PCB缺陷检测
ANR问题的分析与解决思路
lavaweb【初识后续问题的解决】
Big orange crazy blog move notice