使用SpreadSheet将html表转换为excel,但将显示数据隐藏在excel文件中的原因是默认单元格较小 [英] Converting an html table to excel with SpreadSheet but displaying-hiding the data inside the excel file cause of the small default cells

查看:215
本文介绍了使用SpreadSheet将html表转换为excel,但将显示数据隐藏在excel文件中的原因是默认单元格较小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切正常,我的html表可以成功转换为excel.但这是问题所在.代码:

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

从上面的示例中可以看到,我说的是将'Projects First Year'放置在单元格A1中,将Grades放置在单元格B1中,将'PHP Project 2020'放置在单元格B2中,等等.这是问题.当我下载已转换为excel的html表时,我使用传递的数据和单元格打开excel文件隐藏'Projects First Year',因为excel的单元格默认情况下很小,而我的数据'Projects First Year'为结果更大

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

推荐答案

您可以使用使用自动调整大小,您可以开始或多或少的自动调用.

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

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

这篇关于使用SpreadSheet将html表转换为excel,但将显示数据隐藏在excel文件中的原因是默认单元格较小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆