Laravel EXCEL 和 PDF 导出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32651909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel EXCEL and PDF export
提问by Abrar Jahin
I am new in Laravel and I am using Laravel 4.2
我是 Laravel 的新手,我使用的是 Laravel 4.2
I like to export some data in PDF and excel.
我喜欢以 PDF 和 excel 格式导出一些数据。
Is there any way in Laravel?
Laravel 有什么办法吗?
回答by Peyman.H
Use FPDFto do what u need. You must create a pdf file from scratch and fill it in the way u want.
使用FPDF来做你需要的。您必须从头开始创建一个 pdf 文件并以您想要的方式填写它。
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage(); // add page to PDF
$pdf->SetFont('Arial','B',16); // Choose a font and size
$pdf->Cell(40,10,'Hello World!'); // write anything to any line you want
$pdf->Output("your_name.pdf"); // Export the file and send in to browser
?>
And for Excel a simple way is to add PHPExcelto laravel. Add this line to your composer.json
:
对于 Excel,一个简单的方法是将PHPExcel添加到 Laravel。将此行添加到您的composer.json
:
"require": {
"phpexcel/phpexcel": "dev-master"
}
then run a composer update
. So use it like this:
然后运行一个composer update
. 所以像这样使用它:
$ea = new PHPExcel();
$ea->getProperties()
->setCreator('somebody')
->setTitle('PHPExcel Demo')
->setLastModifiedBy('soembody')
->setDescription('A demo to show how to use PHPExcel to manipulate an Excel file')
->setSubject('PHP Excel manipulation')
->setKeywords('excel php office phpexcel')
->setCategory('programming')
;
$ews = $ea->getSheet(0);
$ews->setTitle('Data');
$ews->setCellValue('a1', 'ID'); // Sets cell 'a1' to value 'ID
$ews->setCellValue('b1', 'Season');
回答by Milad Teimouri
Use maatwebsiteto Create and import Excel, CSV and PDF files
使用maatwebsite创建和导入 Excel、CSV 和 PDF 文件
Add this lines to your composer.json
:
将此行添加到您的composer.json
:
"require": {
"maatwebsite/excel": "~2.1.0"
}
After updating composer, add the ServiceProvider to the providers array in config/app.php
更新 Composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组
Maatwebsite\Excel\ExcelServiceProvider::class,
You can use the facade for shorter code. Add this to your aliasses:
您可以将外观用于更短的代码。将此添加到您的别名中:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
To publish the config settings in Laravel 5 use:
要在 Laravel 5 中发布配置设置,请使用:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
simple use of this package:
这个包的简单使用:
$users = User::select('id','name' ,'username')->get();
$Info = array();
array_push($Info, ['id','name' ,'username']);
foreach ($users as $user) {
array_push($Info, $user->toArray());
}
Excel::create('Users', function($excel) use ($Info) {
$excel->setTitle('Users');
$excel->setCreator('milad')->setCompany('Test');
$excel->setDescription('users file');
$excel->sheet('sheet1', function($sheet) use ($Info) {
$sheet->setRightToLeft(true);
$sheet->fromArray($Info, null, 'A1', false, false);
});
})->download('xls'); // or download('PDF')