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

边栏推荐
- AI edge computing platform - beaglebone AI 64 introduction
- 股票开户安全吗?上海股票开户步骤。
- Desai wisdom number - other charts (parallel coordinate chart): employment of fresh majors in 2021
- 视觉特效,图片转成漫画功能
- ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
- 鼠标悬停效果十
- Pulsar theme compression
- Pychar open remote directory remote host
- PCB defect detection based on OpenCV and image subtraction
- Saving images of different depths in opencv
猜你喜欢

基于Pytorch完整的训练一个神经网络并进行验证

小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏

Introduction to kubernetes resource objects and common commands (II)

Image preloading in JS

Comment réaliser la liaison entre la serrure intelligente et la lampe, la scène du moteur de rideau intelligent dans le timing intelligent?

Complete training and verification of a neural network based on pytorch

RestCloud ETL实践之无标识位实现增量数据同步

servlet【初识】

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

Pulsar Geo Replication/灾备/地域复制
随机推荐
Share Creators萌芽人才培养计划来了!
CentOS installs multiple versions of PHP and switches
How do I open an account on my mobile phone? Also, is it safe to open an account online?
鼠标悬停效果七
RestCloud ETL WebService数据同步到本地
Pulsar theme compression
Viewing JVM parameters
PTA 1016
Go import self built package
鼠标悬停效果十
Mouse over effect II
Detailed data governance knowledge system
Mouse over effect 8
鼠标悬停效果二
If I am in Beijing, where is a better place to open an account? In addition, is it safe to open a mobile account?
import tensorflow. contrib. Slim as slim error
Borrowing constructor inheritance and composite inheritance
UE4 rendering pipeline learning notes
Pulsar Geo Replication/灾备/地域复制
Image preloading in JS