PHP创建并格式化Microsoft Word文档

时间:2013-07-08 16:45:42

标签: php ms-word

我希望使用PHP来创建Microsoft Word文档。在线查看之后,我发现大多数提供的解决方案只是创建一个没有格式化的.doc。我想知道创建一个我可以用PHP格式化的Word文档的最佳方法是什么,即改变我公司的字体,颜色,大小等。我猜这个需要某种库。任何回复都将不胜感激。

4 个答案:

答案 0 :(得分:5)

您可以使用PHPWord。它是一个PHP库,可以创建DOCX以及一些格式。

答案 1 :(得分:3)

以HTML格式构建动态Word文档的内容,以及一些特定于Office的样式属性。

Public Sub Page_Load(sender as Object, e as EventArgs)
    Dim strBody As New System.Text.StringBuilder("")

    strBody.Append("<html " & 
        "xmlns:o='urn:schemas-microsoft-com:office:office' " & 
        "xmlns:w='urn:schemas-microsoft-com:office:word'" & 
        "xmlns='http://www.w3.org/TR/REC-html40'>" &
        "<head><title>Time</title>") 

    //The setting specifies document's view after it is downloaded as Print instead of the default Web Layout
    strBody.Append("<!--[if gte mso 9]>" & 
                         "<xml>" & 
                         "<w:WordDocument>" & 
                         "<w:View>Print</w:View>" & 
                         "<w:Zoom>90</w:Zoom>" & 
                         "<w:DoNotOptimizeForBrowser/>" & 
                         "</w:WordDocument>" & 
                         "</xml>" & 
                         "<![endif]-->")

    strBody.Append("<style>" & 
                        "<!-- /* Style Definitions */" & 
                        "@page Section1" & 
                        "   {size:8.5in 11.0in; " & 
                        "   margin:1.0in 1.25in 1.0in 1.25in ; " & 
                        "   mso-header-margin:.5in; " & 
                        "   mso-footer-margin:.5in; mso-paper-source:0;}" & _
                        " div.Section1" & 
                        "   {page:Section1;}" & 
                        "-->" & 
                       "</style></head>") 

    strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" &
                        "<div class=Section1>" & _
                        "<h1>Time and tide wait for none</h1>" & 
                        "<p style='color:red'><I>" & 
                        DateTime.Now & "</I></p>" & 
                        "</div></body></html>") 

    // Force this content to be downloaded as a Word document with the name of your choice
    Response.AppendHeader("Content-Type", "application/msword")
    Response.AppendHeader ("Content-disposition", "attachment;filename=myword.doc")
    Response.Write(strBody)
End Sub

答案 2 :(得分:0)

戴夫的代码适合我。我在我的PHP代码中使用过它。这将以打印布局打开。

<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>

<head>
    <title>Time</title>

    <!--[if gte mso 9]>
    <xml>
        <w:WordDocument>
            <w:View>Print</w:View>
            <w:Zoom>100</w:Zoom>
            <w:DoNotOptimizeForBrowser/>
        </w:WordDocument>
    </xml>
    <![endif]-->

    <style>
        @page Section1 {
            size: 8.5in 11.0in;
            margin: 1.0in 1.25in 1.0in 1.25in ;
            mso-header-margin: .5in;
            mso-footer-margin: .5in;
            mso-paper-source: 0;
        }

        div.Section1 {
            page: Section1;
        }
    </style>

答案 3 :(得分:-1)

你可以使用纯文本和变量,而不是用HTML打开它 - 请参阅http://www.jakebown.com/?p=77

相关问题