使用SpreadSheet将html表格转换为excel表格,但由于默认单元格太小,在excel文件中显示和隐藏数据。

问题描述 投票:2回答:1

一切都很好,我的html表可以成功转换为excel。但这里的问题是。代码。

$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']);

正如你可以看到,从上面的例子中,我说的地方是 'Projects First Year' 在A1单元格中,将 Grades 到单元格B1,将 'PHP Project 2020' 到B2等单元格。这里是 问题. 当我下载了我的html表格,已经转换为excel,我打开excel文件,我通过的数据和单元格 隐藏 'Projects First Year' 因为excel中的单元格它默认是小的,而我的数据 'Projects First Year' 更大的结果是这样的


enter image description here.


有谁知道如何在代码里面解决这个问题?谢谢你。剩下的代码

$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');
php jquery phpexcel spreadsheet phpspreadsheet
1个回答
1
投票

你可以设置一个单元格的 "与 "字段,见下图。文件在此.

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

有了autosize,你就开始了一个或多或少的好的自动调用。

$sheet->getColumnDimension('A')->setAutoSize(true);
© www.soinside.com 2019 - 2024. All rights reserved.