PHP生成.xlsx [英] PHP generate .xlsx
问题描述
我正在生成扩展名为.xlsx的Excel文件
I am generating Excel file with extension of .xlsx
这是我的简单代码:
$file = "test.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.$file);
$content = "Col1\tCol2\tCol3\t\n";
$content .= "test1\ttest1\ttest3\t\n";
$content .= "testtest1\ttesttest2\ttesttest3\t\n";
echo $content;
但是当我打开生成的文件时出现此错误:
But I get this error when I open the generated file:
Excel无法打开文件"test.xlsx",因为文件格式或文件扩展名无效.
Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid.
曾经寻找正确的代码1个小时,但是我没有找到解决方案.
Been searching for the right code for 1 hour, but I didn't find the solution.
推荐答案
没有库就无法创建一个.通读此 PHPExcel Github ,它应该为您指明正确的方向.
You won't be able to just create one without a library. Have a read through this PHPExcel Github which should point you in the right direction.
编辑:PHPExcel已弃用,您也可以在GitHub上使用其后继PHPSpreadsheet. PhpSpreadsheet
PHPExcel is DEPRECATED, you may use its successor PHPSpreadsheet, also on GitHub. PhpSpreadsheet
这篇关于PHP生成.xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!