使用PHP生成Microsoft Word文件

时间:2011-01-22 09:58:43

标签: php ms-word

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

这里是我的代码

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

1 个答案:

答案 0 :(得分:4)

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