我有一个非常奇怪的问题……为什么我用php创建的xls文件与从linux服务器下载时和从windows服务器下载时不一样?
使用jsi解析html表,过滤出一些列并将表推送到表单中,然后将其提交到php脚本。在脚本中,我回显了一个带有特定头的xml文件。当我用xampp在windows上执行此操作并下载并打开文件时,我从excel收到一条消息,内容和文件类型不匹配,但我可以打开它。当我从linux服务器下载相同的文件(新生成的具有相同内容的文件)时,我无法打开该文件,甚至消息也没有出现。
编辑
:
当我用记事本++打开文件(我从linux下载的),更改一行,撤消并保存此操作时,excel可以打开文件(显示内容和文件类型不匹配的消息)。
以下是一些代码供您理解:
// var filename = string
// var this table = previously parsed html table
$('<form target="_blank" action="download.php" method="post"><input type="hidden" name="filename" value="' + encodeURIComponent(filename) + '" /><input type="hidden" name="data" value="' + encodeURIComponent(thisTable) + '" /></form>').appendTo($('body')).submit().remove();
PHP部分:
<?php
header('P3P: CP="NOI NID ADMa CAO PSA OUR IND UNI COM NAV"');
header("Content-Type: application/ms-excel");
header("Content-Disposition: attachment; filename=\"".$_REQUEST['filename'].".xls\";");
echo "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">";
echo "<head>";
echo "<!--[if gte mso 9]>";
echo "<xml>";
echo "<x:ExcelWorkbook>";
echo "<x:ExcelWorksheets>";
echo "<x:ExcelWorksheet>";
echo "<x:Name>".$_REQUEST['filename']."</x:Name>";
echo "<x:WorksheetOptions>";
echo "<x:Panes>";
echo "</x:Panes>";
echo "<x:PageSetup>";
echo "<x:Layout x:Orientation=\"Landscape\"/>";
echo "</x:PageSetup>";
echo "</x:WorksheetOptions>";
echo "</x:ExcelWorksheet>";
echo "</x:ExcelWorksheets>";
echo "</x:ExcelWorkbook>";
echo "</xml>";
echo "<![endif]-->";
echo '<style type="text/css">td{mso-number-format:"#\,##0\.00";} @page {mso-page-orientation:landscape;}</style>';
echo "</head>";
echo "<body>";
echo utf8_decode(urldecode($_REQUEST['data']));
echo "</body>";
echo "</html>";
?>
谢谢你的帮助!