从 linux 下载的 xls 与从 windows 下载的不同

xls downloaded from linux not the same as downloaded from windows

我有一个非常奇怪的问题...为什么我用 php 创建的 xls 文件与从 linux 服务器下载的 xls 文件不一样当我从 windows 服务器下载它时?

我用 js 解析 html table,过滤掉一些列并将 table 推入表单并将其提交给 php 脚本。在脚本中,我用特定的 headers 回显了一个 xml 文件。当我在 windows 上使用 xampp 执行此操作并下载并打开文件时,我从 excel 收到一条消息,内容和文件类型不匹配,但我可以打开它。当我从 linux 服务器下载相同的文件(新生成的具有相同内容的文件)时,我无法打开文件 - 甚至没有出现消息。

文件大小、内容、编码 - 都一样...

为什么会这样?

编辑: 当我用记事本++打开文件(我从linux下载的)时,更改一行,撤消并保存,excel可以打开文件(提示内容和文件类型不匹配)。

这里有一些代码供您理解:

JS-Part:

// 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-Part:

<?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>";
?>

感谢您的帮助!

可能 Linux 中的文件是 "blocked",因为该文件来自未知计算机。如果您查看 Windows 中的文件属性,底部可能会显示一条关于安全性和 "unblock" 能力的消息。因为您编辑并保存了 Linux 文件,Excel 现在认为它是来自您 Windows 计算机的安全文件,因此没有被阻止。

文章 https://superuser.com/questions/1103241/excel-no-longer-receive-the-option-to-open-files-in-a-different-format 解释了 Microsoft 最近 "patch" 引入了此限制(Windows 更新 KB3115130 用于 Excel 2010 和 Windows 更新 KB3115262 用于Excel 2013),以及以下帮助选项:

Option 1

  • Control Panel > Internet Options
  • Security Tab > Trusted Sites
  • Add the site you are downloading from to the trusted site list

** You will need to re-download excel files once this change is made. Files previously downloaded when untrusted may still open blank **

Option 2

  • Go into the properties of the file you have saved to your computer (right click it, then left click properties)
  • Click Unblock
  • Click Apply
  • Open the file

Option 3

  • Open Excel Go to File Options
  • Click Trust Center -> Trust Center Settings
  • Go to Protected View. there are 3 options that show that were all clicked. Uncheck the first option that reads -- "Enable Protected View for files originating from the Internet"

Option 4

  • Open Excel Go to File Options
  • Click Trust Center -> Trust Center Settings
  • Go to File Block Settings
  • Uncheck Web Pages and Excel 2003 XML Spreadsheets

Option 5

  • Uninstall the previously mentioned update that applies to your Office version