【发布时间】:2015-09-18 12:20:03
【问题描述】:
我是 Laravel 新手,我正在使用 Laravel 4.2
我喜欢以 PDF 和 excel 格式导出一些数据。
Laravel 有什么办法吗?
【问题讨论】:
标签: laravel laravel-4 export export-to-excel export-to-pdf
我是 Laravel 新手,我正在使用 Laravel 4.2
我喜欢以 PDF 和 excel 格式导出一些数据。
Laravel 有什么办法吗?
【问题讨论】:
标签: laravel laravel-4 export export-to-excel export-to-pdf
使用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
?>
对于 Excel,一个简单的方法是将 PHPExcel 添加到 laravel。将此行添加到您的 composer.json:
"require": {
"phpexcel/phpexcel": "dev-master"
}
然后运行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');
【讨论】:
使用maatwebsite 创建和导入 Excel、CSV 和 PDF 文件
将此行添加到您的composer.json:
"require": {
"maatwebsite/excel": "~2.1.0"
}
更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组中
Maatwebsite\Excel\ExcelServiceProvider::class,
您可以使用外观来编写更短的代码。将此添加到您的别名中:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
要在 Laravel 5 中发布配置设置,请使用:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
这个包的简单使用:
$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')
【讨论】: