1

当我从给定的表单提交数据时,我对如何创建 docx 文件感到非常困惑,而且我需要将该数据保存到数据库中。

<html>
 <head>
  <title>Create Docx File</title> 
  <script>
   $('#myButton').click(function(){   var s = $('#myHTMLDiv').html($('#htmlstring').val(s);   );
  </script>
 </head>
 <body>
  <h3>User Details</h3>
  <form method="post" action="gendocx.php"> 
  Enter Name:<input type="text" name="htmlstring" id="htmlstring" value="">
     <input type="submit"id="MyButton" value="Generate Docx">
  </form>
 </body>
</html>

gendocx.php

<?php 
    require_once('vendor/autoload.php');
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection(); 
    \PhpOffice\PhpWord\Shared\Html::addHtml($section, $_POST['htmlstring']);
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment;filename="myfirstdocfile.docx"');
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('php://output'); 
?> 
4

0 回答 0