【问题标题】:PHP create and format a Microsoft Word documentPHP 创建和格式化 Microsoft Word 文档
【发布时间】:2013-07-08 16:45:42
【问题描述】:

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

【问题讨论】:

标签: php ms-word


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    以 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
    

    【讨论】:

    • 你的代码对我有用。这就是我在我的 PHP 代码中使用的方式。顺便说一句,第 1 节是什么?是类名吗?
    【解决方案3】:

    dave 的代码对我有用。我已经在我的 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>
    

    【讨论】:

      【解决方案4】:

      您可以使用纯文本和变量来执行此操作,而无需在 HTML 中打开它 - 请参阅此处http://www.jakebown.com/?p=77

      【讨论】:

      • 请注意,link-only answers are discouraged,SO 答案应该是搜索解决方案的终点(相对于另一个参考中途停留,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,并保留链接作为参考。
      • 链接已损坏。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2014-04-29
      相关资源
      最近更新 更多