2013-07-08 53 views
3

我正在寻找使用PHP创建Microsoft Word文档。在网上查找之后,我发现大部分提供的解决方案都是创建一个没有任何格式化的.doc文件。我想知道创建Word文档的最佳方式是什么,我可以使用PHP进行格式设置,即更改我公司的字体,颜色,大小等。我猜测为此需要某种图书馆。任何答复将不胜感激。PHP创建并格式化Microsoft Word文档

+0

有很多这方面的资源。例如。 [用PHP读取/写入MS Word文件](http://stackoverflow.com/q/188452) –

+0

您最好的办法是创建一个HTML文件,然后将其转换为docx文件。见例如[使用PHP将html转换为word/excel/powerpoint](http://stackoverflow.com/q/3590646) –

+0

这些类可以免费用于商业公司吗? –

回答

0

可以使用纯文本和变量,而没有在HTML中开做到这一点 - 在这里看到http://www.jakebown.com/?p=77

+3

请注意,[只提供链接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info),所以答案应该是搜索解决方案的终点(vs.而另一个引用的中途停留时间往往会随着时间推移而过时)。请考虑在此添加独立的摘要,并将链接保留为参考。 – kleopatra

3

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

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 
+0

你的代码适合我。这是我在我的PHP代码中使用的方式。顺便说一句,Section1是什么?这是一个类名吗? –

0

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> 
相关问题