当前位置:网站首页>PHP export word method (phpword)
PHP export word method (phpword)
2022-07-03 11:59:00 【ignativs amor】
background
Last one :MHT export word file
Last one , The use of MHT To generate and Export word Method of documentation , This time, , To record the use php An extension of phpword To generate and Export word;
Use steps
gitlab On composer phpword Expand
phpword Style use document
command :composer require phpoffice/phpword
One 、 Usage method 1(html File generation word file )
But the use of examples in documents is not complete , Because I want to export word It's a resume , There are pictures , Tabulated , So I wonder if I can talk to MHT equally , Render one first html page , Then export , Is there really is , Ha ha ha ha
<?php
namespace App\Services;
use App\Models\Adresume;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;
class WordService
{
public function __construct(){
}
public static function downResumeWord($medlive_id,$title='',$updated_at=''){
info('export');
list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
$oUser = Adresume::where('medlive_id',$medlive_id)->first();
$filename = $oUser->truename?$oUser->truename." Resume of ":" resume ";
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="' . $filename . '.docx"');
header('Cache-Control: max-age=0');
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$domain = config('app.url');
$html = view('pc.usercenter.resume_doc',compact('domain','oResume','oIntension','oWorkexp','oEduexp','oAttach','title','updated_at'))->render();
Html::addHtml($section, $html,false,false);
$writer = IOFactory::createWriter($phpWord,'Word2007' );
$writer->save('php://output');
}
}
Here is a direct download , If you need to save to the server , The following example :
<?php
namespace App\Services;
use App\Models\Adresume;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;
class WordService
{
public function __construct(){
}
public static function downResumeWord($medlive_id,$title='',$updated_at=''){
info('export');
list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
$oUser = Adresume::where('medlive_id',$medlive_id)->first();
$filename = $oUser->truename?$oUser->truename." Resume of ":" resume ";
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$domain = config('app.url');
$html = view('pc.usercenter.resume_doc',compact('domain','oResume','oIntension','oWorkexp','oEduexp','oAttach','title','updated_at'))->render();
Html::addHtml($section, $html,false,false);
$writer = IOFactory::createWriter($phpWord,'Word2007' );
$writer->save($filename.'docx');
}
}
Two 、 Usage method 2( Use word The template to generate word file )
however , This method has not been tried , For reference only , I found a detailed article for you to share
The forefathers planted trees , Descendants cool 1:word The template to generate word file
Two 、 Usage method 3( Use phpword attribute , Build your own style )
<?php
namespace App\Services;
use App\Models\Adresume;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;
//require_once 'bootstrap.php';
//require './vender/autoload.php';
class WordService
{
public function __construct(){
}
public static function downResumeWord($medlive_id,$title='',$updated_at=''){
info('export');
list($oResume,$oIntension,$oWorkexp,$oEduexp,$oAttach) = Adresume::ViewResume($medlive_id);
$oUser = Adresume::where('medlive_id',$medlive_id)->first();
$filename = $oUser->truename?$oUser->truename." Resume of ":" resume ";
$phpWord = new PhpWord();
/* Method 2 : Handle the style line by line , Compatible with pictures */
$fontStyle2 = array('align'=>'center'); // Overall style
$section = $phpWord->addSection(); // Overall page
$phpWord->addTitleStyle(1, ['bold' => true, 'color' => '000', 'size' => 17, 'name' => ' Song style '],$fontStyle2); // Set up title style
$time = date('Y-m-d',time());
$section->addText(" Application :$title
Delivery time :$updated_at"); // To add text
$styleTable = [
'name' => ' Microsoft YaHei ',
'borderSize' => 6,
'cellMargin' => 20,
]; // Set table style
$phpWord->addTableStyle('myTable', $styleTable);
$table = $section->addTable('myTable');
// first line
$table->addRow(500);
$table->addCell(5000)
->addImage(public_path().$oResume->downthumb, ['gridSpan' => 4,'width'=>350, 'height'=>100, 'valign'=>'center', 'align'=>'center']);
// The second line
$table->addRow(500);
$table->addCell(5000)
->addText($oResume->truename,['size'=>24,'color'=>'1f6990'], ['gridSpan' => 4,'valign'=>'center', 'align'=>'center']);
// The third line
$table->addRow(500);
$table->addCell(5000)
->addText('> essential information ',['size'=>18,'color'=>'black','bold'=>true], ['gridSpan' => 4,'valign'=>'top', 'align'=>'left']);
// In the fourth row
$table->addRow(500);
$table->addCell(5000)
->addImage(public_path().'/assets/images/ad/review_line.png', ['gridSpan' => 4,'width'=>540, 'valign'=>'top', 'align'=>'center']);
$writer = IOFactory::createWriter($phpWord,'Word2007' );
$fileName = $filename.'.doc';
$writer->save( $fileName);
// If you just save it to the server, it's good to stop here
// $file = public_path().'/uploadfile/export/'.date('Ymd').'/' . $fileName;
// return response()->download($file); // Download the file here
}
}
The results are as follows :
Look at this. , I'm tired , This is just the head of the resume , I've written so much code , My heart is so tired , Or render directly html The page is comfortable
边栏推荐
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
- Simple factory and factory method mode
- Unity3D学习笔记5——创建子Mesh
- vulnhub之cereal
- Wrong arrangement (lottery, email)
- vulnhub之tomato(西红柿)
- vulnhub之presidential
- PHP导出word方法(一phpword)
- Qt OpenGL 纹理贴图
- The world's most popular font editor FontCreator tool
猜你喜欢
Kibana - installation and configuration of kibana
Unity3D学习笔记5——创建子Mesh
This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system
Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements
How to get started embedded future development direction of embedded
【学习笔记】dp 状态与转移
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
836. 合并集合(DAY 63)并查集
GCC compilation process and dynamic link library and static link library
Download address and installation tutorial of vs2015
随机推荐
为什么我的mysql容器启动不了呢
Master and backup role election strategy in kept
vulnhub之Ripper
Nestjs configuration service, configuring cookies and sessions
PHP導出word方法(一mht)
OpenGL 着色器使用
STL教程8-map
Solutions to the failure of installing electron
vulnhub之momentum
2022年中南大学夏令营面试经验
Hongmeng fourth training
外插散点数据
libvirt 中体验容器
《剑指offer 04》二维数组查找
【mysql专项】读锁和写锁
Qt OpenGL 旋转、平移、缩放
CGroup introduction
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
Kubernetes 三打探针及探针方式
Kubernetes three dozen probes and probe mode