我想在word文档中输出form
..最好的解决方案,我得到了一些研究是使用php header(),即
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=output.doc");
我已经很容易地用这个方法创建了一个简单的word文件。但在我的项目中,我从angularjs获得数据…这是我在Angular Controller中的代码。
$scope.createdoc = function () {
$scope.textContent = "hello";
var request = $http({
method: "post",
url: "doc.php",
data: {
items: $scope.items,
length: $scope.items.length
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
request.success(function (data) {
$scope.textContent = "Document created successfully "+data;
});
}
这是我在doc.php
$json = file_get_contents("php://input");
$array = json_decode($json, true);
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=output.doc");
echo "<html>";
echo "<meta http-equiv='"Content-Type'" content='"text/html; charset=Windows-1252'">";
echo "<body>";
echo "<p>This is Document body</p>";
echo "</body>";
我以前在简单的php文件中这样做过,该文件提示下载doc
文件,内容为This is Document body
,但这次,它在我打印$作用域的html页面中显示所有这些echo'ed行。textContent i.e. {{textContent}}
,
Document created successfully <html><meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"><body><p>This is Document body</p></body></html>
我做错了什么吗?有解决方案吗?
我不认为你可以这么容易地生成一个Office Word文档,只有几个标题和其他的。
这个PHP库可以创建一个Word文件
下面是一个如何使用phoffice/PHPWord创建Office Word 2007文件的简单示例记住使用正确的写法$objWriter = 'PhpOffice'PhpWord'IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
好运我最终并行做AngularJS和PHP…我的代码是这样的
<?php
if(isset($_POST["submit"])) {
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=output.doc");
echo "<html>";
echo "<meta http-equiv='"Content-Type'" content='"text/html; charset=Windows-1252'">";
echo "<body>";
echo "<p style = '"color: $_POST["color"]; font-size: $_POST["font"].px; font-family: $_POST["family"]'">{$_POST['text']}</p>";
echo "</body>";
echo "</html>";
}
?>
这不是分配值给$scope
变量,然后在PHP中使用它们,我使用ng-model
和name
字段在输入标签…