create xls using PhpSpreadsheet
This commit is contained in:
parent
5822224c67
commit
bed5a26b5d
3 changed files with 397 additions and 15 deletions
|
@ -2,9 +2,12 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use DateTime;
|
||||
use OwenVoke\Gitea\Client;
|
||||
use Illuminate\Http\Request;
|
||||
use DateTime;
|
||||
use PhpOffice\PhpSpreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xls;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
|
||||
class GiteaApiController extends Controller
|
||||
{
|
||||
|
@ -65,18 +68,41 @@ class GiteaApiController extends Controller
|
|||
|
||||
private function create_csv(string $file_name, array $data)
|
||||
{
|
||||
$f = fopen('php://output', 'w'); // Configure fopen to create, open, and write data.
|
||||
$reader = new Spreadsheet();
|
||||
$spreadsheet = $reader->getActiveSheet();
|
||||
$headers = array_keys($data[0]);
|
||||
$data_new = array_unshift($data, $headers);
|
||||
$spreadsheet->fromArray(
|
||||
$data,
|
||||
null,
|
||||
'A1'
|
||||
);
|
||||
|
||||
fputcsv($f, array_keys($data[0])); // Add the keys as the column headers
|
||||
// Loop over the array and passing in the values only.
|
||||
foreach ($data as $row) {
|
||||
fputcsv($f, $row);
|
||||
}
|
||||
fclose($f);
|
||||
// tell the browser it's going to be a csv file
|
||||
header('Content-Type: text/csv');
|
||||
// tell the browser we want to save it instead of displaying it
|
||||
header('Content-Disposition: attachment; filename="' . $file_name . '.csv";');
|
||||
//dd($spreadsheet);
|
||||
$filename = $file_name . '.xls';
|
||||
|
||||
$writer = new Xls($reader);
|
||||
$writer->save($filename);
|
||||
|
||||
//Define header information
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/octet-stream');
|
||||
header("Cache-Control: no-cache, must-revalidate");
|
||||
header("Expires: 0");
|
||||
header('Content-Disposition: attachment; filename="' . basename($filename) . '"');
|
||||
header('Content-Length: ' . filesize($filename));
|
||||
header('Pragma: public');
|
||||
|
||||
//Clear system output buffer
|
||||
flush();
|
||||
|
||||
//Read the size of the file
|
||||
readfile($filename);
|
||||
|
||||
unlink($filename);
|
||||
|
||||
//Terminate from the script
|
||||
die();
|
||||
}
|
||||
|
||||
private function date_to_datetime(string $date)
|
||||
|
@ -103,7 +129,7 @@ class GiteaApiController extends Controller
|
|||
}
|
||||
|
||||
if (count($data) > 0) {
|
||||
$file_name = $issues_params['state']."_issues_from_".$from_date."_to_".$to_date;
|
||||
$file_name = $issues_params['state'] . "_issues_from_" . $from_date . "_to_" . $to_date;
|
||||
$this->create_csv($file_name, $data);
|
||||
return true;
|
||||
}
|
||||
|
@ -118,6 +144,7 @@ class GiteaApiController extends Controller
|
|||
$from = $req->input('from_year') . '-' . $req->input('from_month') . '-01';
|
||||
$to = $req->input('to_year') . '-' . $req->input('to_month') . '-31';
|
||||
$res = $this->export_issues($from, $to, ['state' => $req->input('issues_type')]);
|
||||
|
||||
return view('backend', [
|
||||
'token' => getenv('GITEA_TOKEN'),
|
||||
'download' => $res
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue