Converting an html table to excel with SpreadSheet but displaying-hiding the data inside the excel file cause of the small default cells

微笑、不失礼 提交于 2020-06-26 08:44:10

问题


Everything works great my html table can succesfully converte to excel. But here is the issue. Code:

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Projects First Year');
$sheet->setCellValue('B1', 'Grades');

$sheet->setCellValue('A2', 'PHP Project 2020');
$sheet->setCellValue('B2', $_SESSION['phpScore']);

As you can see from the example above i am saying that place the 'Projects First Year' in the cell A1, place the Grades to the cell B1, place the 'PHP Project 2020' to the cell B2 etc. Here is the PROBLEM. When i download my html table that has been converted to excel, i open the excel file with the data that i passed and the cells Hide the 'Projects First Year' because the cell of the excel it is by default small and my data 'Projects First Year' is bigger the result is this


.


Anyone knows how to fix this inside the code? Thank you . The rest of the code

$filename = 'sample-'.time().'.xlsx';
// Redirect output to a client's web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

回答1:


You can set the with of a cell with see documentation here.

$sheet->getColumnDimension('A')->setWidth(12);

with autosize you start a more or less good automatic callculation.

$sheet->getColumnDimension('A')->setAutoSize(true);


来源:https://stackoverflow.com/questions/62202064/converting-an-html-table-to-excel-with-spreadsheet-but-displaying-hiding-the-dat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!