2013-07-08 52 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> 
相關問題