在 PHP 中生成 Microsoft Word 文件

发布于 2024-10-13 15:11:47 字数 1794 浏览 1 评论 0原文

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

这是我的代码

    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>"; 

I am able to generate a Microsoft word file, but I want the data in table structure. I have used an HTML table tag in PHP, but the output does not look satisfactory. In the table are a lot of cellspacing and cellpadding even if I have initialize them to zero space. Any idea how can I solve this?

here goes my code

    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>"; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-10-20 15:11:47

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

Why not consider creating a "real" Word document rather than a "make-believe that HTML is actually Word" file. Take a look at PHPWord.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文