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

边栏推荐
- Find the length of the common part of two line segments
- 在国内如何买港股的股?用什么平台安全一些?
- Viewing JVM parameters
- The latest wechat iPad protocol code obtains official account authorization, etc
- [PR # 5 A] two way running (state pressure DP)
- 基于Pytorch完整的训练一个神经网络并进行验证
- Prototype and prototype chain in JS
- Mnasnet learning notes
- 鼠标悬停效果四
- Restcloud ETl数据通过时间戳实现增量数据同步
猜你喜欢

Nacos configuration center tutorial

CentOS installs multiple versions of PHP and switches

Sampling Area Lights

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

How to use Jieba participle in unity

Sampling Area Lights

The operation efficiency of the park is improved, and the application platform management of applet container technology is accelerated

Optimal transport Series 1

单片机 MCU 固件打包脚本软件

Optimal Transport系列1
随机推荐
Optimal Transport系列1
Thread Detach
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
[wechat applet development] style summary
园区运营效率提升,小程序容器技术加速应用平台化管理
Mouse over effect I
Dell server restart Idrac method
Youmeng (a good helper for real-time monitoring of software exceptions: crash) access tutorial (the easiest tutorial for Xiaobai with some foundation)
How to realize the scene linkage of intelligent lock, lamp and intelligent curtain motor in zhiting?
Restcloud ETL实践之数据行列转换
LeetCode_栈_困难_227.基本计算器(不含乘除)
import tensorflow.contrib.slim as slim报错
Restcloud ETL practice to realize incremental data synchronization without identification bit
Pulsar theme compression
鼠标悬停效果一
Xception学习笔记
Pychart software deployment gray unable to point
Complete training and verification of a neural network based on pytorch
Pychar open remote directory remote host
Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?