当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
MCDF实验1
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
Php Export word method (One MHT)
Based on MCU, how to realize OTA differential upgrade with zero code and no development?
836. 合并集合(DAY 63)并查集
Xml的(DTD,xml解析,xml建模)
Cacti监控Redis实现过程
The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
Deploying WordPress instance tutorial under coreos
Web security summary
Quantitative calculation research
Excel quick cross table copy and paste
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
Keepalived中Master和Backup角色选举策略
Vulnhub narak
Hongmeng third training (project training)
Qt OpenGL 旋转、平移、缩放
Solution à la défaillance de l'installation d'Electron
OPenGL 基本知识(根据自己理解整理)
STL tutorial 8-map









