简体   繁体   中英

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. 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 .

我在第一个文件中有两个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:

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

If will retrun 0 - file doesn't exist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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