PHP 使用服务器上存储的 XML 文件创建 Excel 供下载

发布于 2024-11-08 15:39:31 字数 1797 浏览 2 评论 0原文

天呐,基本上我正在做的就是创建一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我乃一代侩神 2024-11-15 15:39:31

我的第一个文件中有两个 PHP 块,导致出现空格或换行问题。

I had two PHP blocks in the first file causing a white-space or line-break issue.

尝蛊 2024-11-15 15:39:31
 $dir = "reports/xls/";
 $filename = 'Summary.xml';
 if(!file_exists($dir.$filename)) die("Upload the file");

或者通过 FireBug 或其他应用程序观看响应标头进行查找:

"Content-Length: " . filesize($dir.$filename))

如果将重新运行 0 - 文件不存在

 $dir = "reports/xls/";
 $filename = 'Summary.xml';
 if(!file_exists($dir.$filename)) die("Upload the file");

Or watch Response header via FireBug or another app for loking:

"Content-Length: " . filesize($dir.$filename))

If will retrun 0 - file doesn't exist

扛刀软妹 2024-11-15 15:39:31

尝试关闭 xml 脚本末尾的 标记

try to close <Workbook> tag at the end of xml script

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文