gpt4 book ai didi

php - 在用户桌面上动态创建 doc/docx 文档

转载 作者:行者123 更新时间:2023-11-30 13:22:37 24 4
gpt4 key购买 nike

我的网站是 HTML/Javascript,AJAX 调用服务器端 PHP。我想允许用户单击一个图标并根据 MySQL 数据创建一个报告,然后将其保存在客户端的桌面上无需重新加载页面。

据我所知,创建文档的选项如下所示。 (我认为它需要在服务器端完成,而不是使用 Javascript。)我不确定每种情况下文件的最终位置。请随时纠正我的误解:)

方法 1 - 这似乎只是为了创建一个 .doc 文件。我不确定文件放在哪里。

$fp = fopen("method1.doc", 'w+');
$str = "<B>This is the text for the word file created through php programming</B>";
fwrite($fp, $str);
fclose($fp);

方法 2 - 这似乎也创建了一个 .doc 文件。

$word = new COM("word.application") or die ("couldnt create an instance of word");
echo "loaded , word version{$word->version}";
$word->visible = 1;
$word->Documents->Add();
$word->Selection->TypeText("Sample text.");
$word->Documents[1]->SaveAs("method2.doc");
$word->Quit();
$word->Release();
$word = null;

方法 3 - 我认为也是一个 .doc 文件。

header('Content-type: application/vnd.ms-word');
header("Content-Disposition: attachment;Filename=method3.doc");

echo "<html>";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";

方法 4 - PHPWord

方法 5 - PHPDocx

我已经在我的家庭开发环境中测试了 1 和 2,但是我找不到文件!请问最好的方法是什么?

谢谢:)

顺便说一句,我知道有相关帖子here , herehere ,但没有人真正回答这个问题。

最佳答案

如果您想要一个图标,并且当单击该图标时它会在不重新加载页面的情况下进行下载,那么您只需要创建一个指向该图标的链接,该链接会使用适当的标题启动一个开始下载的脚本。

示例:

header ('Pragma: no-cache');
header('Content-Disposition: attachment; filename="'.$File.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.$Len);

这样做,下载将开始,但用户点击的页面不会改变,也不会重新加载。

如果要生成动态DOCX文件下载,我推荐使用OpenTBS .该库可以使用模板生成 DOCX(以及 XLSX、PPTX、ODT、ODS 等)。它具有让您直接将结果作为下载发送的功能,没有临时文件,或者让您保存在服务器端。

关于php - 在用户桌面上动态创建 doc/docx 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9662537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com