当前位置:网站首页>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
边栏推荐
- 安裝electron失敗的解决辦法
- MySQL union和union all区别
- 并发编程-单例
- Modular programming of single chip microcomputer
- previous permutation lintcode51
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- 2022年中南大学夏令营面试经验
- Interview experience in summer camp of Central South University in 2022
- pragma-pack语法与使用
- PHP导出word方法(一mht)
猜你喜欢

Raven2 of vulnhub

Qt OpenGL 旋转、平移、缩放

laravel 时区问题timezone

Vulnhub's presidential

(database authorization - redis) summary of unauthorized access vulnerabilities in redis

Ripper of vulnhub

MCDF Experiment 1

Unity3D学习笔记5——创建子Mesh

During FTP login, the error "530 login incorrect.login failed" is reported

《剑指offer 03》数组中重复的数字
随机推荐
2022年中南大学夏令营面试经验
During FTP login, the error "530 login incorrect.login failed" is reported
How to convert a numeric string to an integer
Groovy测试类 和 Junit测试
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
Php Export word method (One MHT)
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
Sheet1$.输出[Excel 源输出].列[XXX] 出错。返回的列状态是:“文本被截断,或者一个或多个字符在目标代码页中没有匹配项。”。
外插散点数据
简单工厂和工厂方法模式
Kubernetes three dozen probes and probe mode
Differences between MySQL Union and union all
网络通讯之Socket-Tcp(一)
优化接口性能
DNS multi-point deployment IP anycast+bgp actual combat analysis
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
并发编程-单例
Notes on 32-96 questions of sword finger offer
OpenGL 索引缓存对象EBO和线宽模式
安装electron失败的解决办法