PHP 使用服务器上存储的 XML 文件创建 Excel 供下载
天呐,基本上我正在做的就是创建一个包含 Db 信息的 XML 文件。我使用 PHP 创建 XML 文件并将其存储在服务器上。 我必须存储 XML,因为它基于特定的时间段。 我无法使用任何外部库或类,无论如何我都不需要,很可能是代码错误。这是生成的 XML 标头和基本结构,总共有 6 个表。
<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Report Page 1">
<Table>
<Row><Cell><Data ss:Type="String">col 1</Data></Cell><Cell><Data ss:Type="String">Col 2</Data></Cell></Row>
</Table>
</Worksheet>
第 1 栏第 2 栏
这是从链接调用的 PHP
<?php
$dir = "reports/xls/";
$filename = 'Summary.xml';
header("Content-type: application/vnd.ms-excel");
header("Content-Length: " . filesize($dir.$filename));
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");
$Dfile = file_get_contents($dir.$filename);
print $Dfile;
?>
我得到的只是一个空白的 excel 文件,其中包含sheet1,没有数据。如果我使用相同的技术但动态生成文件,则报告看起来很棒,所有数据都存在。当然,PHP 标头略有不同。
header("Content-type: application/vnd.ms-excel");
header("Content-Length: " . strlen($excelxml));
header("Content-Disposition: attachment; filename={$excelxml}.xml");
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");
print $excelxml;
$excelxml 只是一个保存生成数据的字符串。 任何帮助将不胜感激,我不认为 CSV 是一个选项,我不认为它可以创建多个工作表。 我注释掉了 Content-type/Disposition 标头来查看源代码,除了服务器上 XML 文件中的代码在 .
Gday, basically what I'm doing is creating a XML file with Db info. I'm using PHP to create the XML file and store it on the server.
I have to store the XML because its based on a specific period in time.
I can't use any outside libraries or classes, I shouldn't need one anyway, most likely code error. So here's the generated XML header and basic structure, it has 6 sheets altogether.
<?xml version="1.0" encoding="UTF-8"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Report Page 1">
<Table>
<Row><Cell><Data ss:Type="String">col 1</Data></Cell><Cell><Data ss:Type="String">Col 2</Data></Cell></Row>
</Table>
</Worksheet>
col 1Col 2
Here's the PHP called from a link
<?php
$dir = "reports/xls/";
$filename = 'Summary.xml';
header("Content-type: application/vnd.ms-excel");
header("Content-Length: " . filesize($dir.$filename));
header("Content-Disposition: attachment; filename=$filename");
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");
$Dfile = file_get_contents($dir.$filename);
print $Dfile;
?>
All I get is a blank excel file with sheet1 and no data. If I use the same technic but generate the file on the fly the report looks great, all data present. Slightly different PHP headers of course.
header("Content-type: application/vnd.ms-excel");
header("Content-Length: " . strlen($excelxml));
header("Content-Disposition: attachment; filename={$excelxml}.xml");
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");
print $excelxml;
$excelxml is just a string holding the generated data.
Any help would be greatly appreciated, I don't think CSV is an option,I don't think it can create multiple worksheets.
I commented out the Content-type/Disposition headers to look at the source code and its the same except the code from the XML file on the server has an extra space at the top, above the .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的第一个文件中有两个 PHP 块,导致出现空格或换行问题。
I had two PHP blocks in the first file causing a white-space or line-break issue.
或者通过 FireBug 或其他应用程序观看响应标头进行查找:
如果将重新运行 0 - 文件不存在
Or watch Response header via FireBug or another app for loking:
If will retrun 0 - file doesn't exist
尝试关闭 xml 脚本末尾的
标记try to close
<Workbook>
tag at the end of xml script