当前位置:网站首页>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
边栏推荐
- 外插散点数据
- previous permutation lintcode51
- 优化接口性能
- Visual studio 2022 downloading and configuring opencv4.5.5
- Vulnhub's presidential
- Qt+vtk+occt reading iges/step model
- How to convert a numeric string to an integer
- Talk about the state management mechanism in Flink framework
- Introduction to the implementation principle of rxjs observable filter operator
- 网络通讯之Socket-Tcp(一)
猜你喜欢
解决msvcp120d.dll和msvcr120d.dll缺失
CGroup introduction
ArcGIS application (XXI) ArcMap method of deleting layer specified features
同事写了一个责任链模式,bug无数...
836. 合并集合(DAY 63)并查集
Xml的(DTD,xml解析,xml建模)
The tutor put forward 20 pieces of advice to help graduate students successfully complete their studies: first, don't plan to take a vacation
OpenGL 绘制彩色的三角形
shardingSphere分库分表<3>
STL tutorial 10 container commonalities and usage scenarios
随机推荐
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
Optimize interface performance
Groovy test class and JUnit test
MySQL union和union all区别
vulnhub之presidential
previous permutation lintcode51
previous permutation lintcode51
OpenGL 索引缓存对象EBO和线宽模式
Vulnhub's Tomato (tomato)
libvirt 中体验容器
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
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Qt OpenGL 纹理贴图
Experience container in libvirt
Test classification in openstack
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
Kibana - installation and configuration of kibana
2022年湖南工学院ACM集训第二次周测题解
vulnhub之pyexp
抓包整理外篇fiddler———— 会话栏与过滤器[二]