用 PHP 生成 Microsoft Word 文件

sid*_*ant -2 php ms-word

我能够生成 Microsoft Word 文件,但我想要表结构中的数据。我在 PHP 中使用了 HTML table 标记,但输出看起来并不令人满意。表格中有很多单元格间距和单元格填充,即使我已将它们初始化为零空间。知道我该如何解决这个问题吗?

这是我的代码

    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=document_name.doc");
    $now_date = date('d-m-Y H:i');
    echo "<html>";
    echo "<body>";
    echo "</body>";
    echo "<head>";
    echo "<div>Seminarthema:</div>\n";
    echo "<div>Termin:$now_date</div>";
    echo "<br></br>";
    echo "<div style='size:20px;'><h2>Teilnehmerliste</h2></div>";
    echo "<table border='1' cellpadding='0' cellspacing='0'>
            <tr><th>Name</th><th>Vorname</th><th>Buchungskreis</th><th>Kurs-ID</th><th>Teilgenommen</th></tr>";     
    $students = $GLOBALS["TYPO3_DB"]->exec_SELECTquery('*','tx_training_Registration','hidden=0 AND deleted=0');
    $countstud = $GLOBALS['TYPO3_DB']->sql_num_rows($students); 
    if($countstud){ 
        while($row2 = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($students)){
            echo "<tr>";
                echo "<td>".utf8_decode($row2['firstname'])."</td>";
                echo "<td>".utf8_decode($row2['lastname'])."</td>";
                echo "<td>".utf8_decode($row2['companycode'])."</td>";
                echo "<td>".utf8_decode($row2['trainingid'])."</td>";
                echo "<td>".utf8_decode($row2[' status'])."</td>";
            echo "</tr>";
        }   
    }                                 
    echo  "</table>";   
    echo "</head>";
    echo "</html>"; 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ker 5

为什么不考虑创建一个“真实”的 Word 文档,而不是一个“假装 HTML 实际上是 Word”的文件。看看PHPWord