简体   繁体   English

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

[英]PHP Using XML file stored on server to create excel for download

Gday, basically what I'm doing is creating a XML file with Db info. Gday,基本上我正在做的是使用Db信息创建XML文件。 I'm using PHP to create the XML file and store it on the server. 我正在使用PHP创建XML文件并将其存储在服务器上。 I have to store the XML because its based on a specific period in time. 我必须存储XML,因为它基于特定的时间段。 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标头和基本结构,共有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>

col 1Col 2 Col 1颜色2

Here's the PHP called from a link 这是从链接调用的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;
?>

All I get is a blank excel file with sheet1 and no data. 我得到的只是一个空白的Excel文件,带有sheet1,没有数据。 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. 当然,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 is just a string holding the generated data. $ excelxml只是保存生成的数据的字符串。 Any help would be greatly appreciated, I don't think CSV is an option,I don't think it can create multiple worksheets. 任何帮助将不胜感激,我不认为CSV是一种选择,我认为它不能创建多个工作表。 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 . 我注释掉了Content-type / Disposition标头,以查看源代码及其相同之处,只是服务器上XML文件中的代码在上方的顶部有一个额外的空间。

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

尝试在xml脚本的末尾关闭<Workbook>标记

 $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: 或通过FireBug或其他应用查看Response标头以查找:

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

If will retrun 0 - file doesn't exist 如果将重新运行0-文件不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM