使用”ms-excel”扩展头部来打开由PHP生成的HTML表格以在Excel中打开。

huangapple go评论80阅读模式
英文:

Right headers to use ms-excel http extention to open php generated html table to open in excel

问题

以下是您要翻译的内容:

"Good day everyone.

I use PHP to generate tables in HTML, which also shall be able to be exported (for excel). For a while i used datatables to do this. but it is very js heavy and runs endless on bigger tables.
I am aware that excel can read html. But just forcing the HTML table to download would mean my users need to navigate through 'open with' in windows to open it in excel. And as some of you might guess, that's to much.

I am also aware that office installs http extension like 'ms-excel', so i tried to force the system directly to open the html with excel using 'ms-excel:ofv|u|http://intranet/report.php?export=excel';

Unfortunately Excel says it's an unknown command. But if i save it the usual way, put the file up on the Server and try it again with 'ms-excel:ofv|u|http://intranet/exported_file.htm' it works.

So i assume the problem lays in the headers, but i can't figure which would be correct. Is one wrong or missing?

header('Content-Description: File Transfer');
header("Content-Type: text/html");
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="exported_'.time().'.htm"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: 11386');

Alternatively i could save the generated html on the server and provide a link but that far from an elegant solution."

英文:

Good day everyone.

I use PHP to generate tables in HTML, which also shall be able to be exported (for excel). For a while i used datatables to do this. but it is very js heavy and runs endless on bigger tables.
I am aware that excel can read html. But just forcing the HTML table to download would mean my users need to navigate through "open with" in windows to open it in excel. And as some of you might guess, that's to much.

I am also aware that office installs http extension like "ms-excel", so i tried to force the system directly to open the html with excel using "ms-excel:ofv|u|http://intranet/report.php?export=excel"

Unfortunately Excel says it's an unknown command. But if i save it the usual way, put the file up on the Server and try it again with "ms-excel:ofv|u|http://intranet/exported_file.htm" it works.

So i assume the problem lays in the headers, but i can't figure which would be correct. Is one wrong or missing?

header('Content-Description: File Transfer');
header("Content-Type: text/html");
//header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="exported_'.time().'.htm"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: 11386');

Alternatively i could save the generated html on the server and provide a link but that far from an elegant solution.

答案1

得分: 1

我已经发现了问题,与我之前的想法相反,问题不在于标头。而是在于URL。看起来Office忽略了标头中提供的文件名,只查看URL。如果URL中的文件是.html或.htm文件,它就能工作。

对于我的情况,解决方案是告诉Apache2不仅在php文件中执行php,还在htm和html文件中执行。然后重新命名我的脚本。
我知道这可能会引发一些安全担忧,但在我的情况下,这是在客户内部网络中,我是唯一上传文件的人。

英文:

I've found the Problem contrary to my believe it wasn't the headers.
It is the URL. It seems Office ignores the filename that is given in the headers and only looks in the URL. If the file in the URL is a .html or .htm file, it works.

For my case the solution is to tell Apache2 to execute php not only in php files but also in htm and html files. And rename my script.
I am aware that this may raise some security concerns, but in my case its in a customers intranet and i am the only one who put files on it.

huangapple
  • 本文由 发表于 2023年6月29日 22:31:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定