我是Laravel的新手,我正在使用Laravel 4.2
我喜欢在PDF和excel中导出一些数据。
在Laravel有什么办法吗?
发布于 2015-09-18 21:03:52
使用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');发布于 2017-11-25 00:56:11
使用maatwebsite创建和导入Excel、CSV和PDF文件
将以下代码行添加到您的composer.json
"require": {
"maatwebsite/excel": "~2.1.0"
}更新composer后,将ServiceProvider添加到config/app.php中的提供程序数组中
Maatwebsite\Excel\ExcelServiceProvider::class,您可以使用facade来编写更短的代码。将此添加到您的别名:
'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')https://stackoverflow.com/questions/32651909
复制相似问题