当前位置:网站首页>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
边栏推荐
- Based on MCU, how to realize OTA differential upgrade with zero code and no development?
- 【mysql专项】读锁和写锁
- typeScript
- STL tutorial 10 container commonalities and usage scenarios
- "Jianzhi offer 04" two-dimensional array search
- R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
- PHP导出word方法(一phpword)
- 错排问题 (抽奖,发邮件)
- (数据库提权——Redis)Redis未授权访问漏洞总结
- DNS multi-point deployment IP anycast+bgp actual combat analysis
猜你喜欢

Extrapolated scatter data
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

STL tutorial 10 container commonalities and usage scenarios

CGroup introduction

Qt OpenGL 旋转、平移、缩放

STL教程10-容器共性和使用场景

vulnhub之Ripper

laravel 时区问题timezone

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

Unity3d learning notes 5 - create sub mesh
随机推荐
Keepalived中Master和Backup角色选举策略
Dynamically monitor disk i/o with ZABBIX
(数据库提权——Redis)Redis未授权访问漏洞总结
Ripper of vulnhub
Groovy测试类 和 Junit测试
Quantitative calculation research
Oracle advanced (I) realize DMP by expdp impdp command
Vulnhub geminiinc
OpenStack中的测试分类
Vulnhub narak
STL tutorial 8-map
Duplicate numbers in the array of sword finger offer 03
Vulnhub's cereal
Unity3d learning notes 5 - create sub mesh
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
【mysql专项】读锁和写锁
Differences between MySQL Union and union all
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
STL教程8-map
Web security summary