PHP code example of svrnm / exceldatatables
1. Go to this page and download the library: Download svrnm/exceldatatables library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
svrnm / exceldatatables example snippets
...
'providers' => array(
...
'Svrnm\ExcelDataTables\ExcelDataTablesServiceProvider'
)
...
eate a new instance
$dataTable = new Svrnm\ExcelDataTables\ExcelDataTable();
// Specify the source file
$in = 'spec.xlsx';
// Specify the output file
$out = 'test.xlsx';
// Specify the data for the worksheet.
$data = array(
array("Date" => new \DateTime('2014-01-01 13:00:00'), "Value 1" => 0, "Value 2" => 1),
array("Date" => new \DateTime('2014-01-02 14:00:00'), "Value 1" => 1, "Value 2" => 0),
array("Date" => new \DateTime('2014-01-03 15:00:00'), "Value 1" => 2, "Value 2" => -1),
array("Date" => new \DateTime('2014-01-04 16:00:00'), "Value 1" => 3, "Value 2" => -2),
array("Date" => new \DateTime('2014-01-05 17:00:00'), "Value 1" => 4, "Value 2" => -3),
);
// Attach the data table and copy the new xlsx file to the output file.
$dataTable->showHeaders()->addRows($data)->attachToFile($in, $out);
class ReportController extends Controller {
protected $dataTable;
public function __construct(\Svrnm\ExcelDataTables\ExcelDataTable $dataTable) {
$this->dataTable = $dataTable;
}
public function show($month) {
$data = DB::select('select date,value1,value2 from reporting where MONTH(date) = ?', array($month));
$path = storage_path() . '/reports/example.xlsx';
$xlsx = $this->dataTable->showHeaders()->addRows($data)->fillXLSX($path);
return Response::make($xlsx, 200, array(
'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'Content-Disposition' => 'attachment; filename="report.xlsx"',
'Content-Length' => strlen($xlsx)
));
// ...
}
}