2010-05-11 168 views
2

如何使用ASP.Net與C#(2008)創建兩個擴展名爲.doc和.docx的word文檔文件?創建Word文檔文件

+0

可能欺騙:http://stackoverflow.com/questions/2727633/how-to-open-ms-office-word-in-asp-net – ahsteele 2010-05-11 06:13:32

回答

2

我寧願避免COM路線,而是產生在響應流中的文檔。這對我來說真的很好。希望能幫助到你。

 Response.ContentType = "application/msword"; 
     Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8; 
     Response.Charset = "UTF-8"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + "mydoc.doc"); 

     Response.Write("<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'>"); 
     Response.Write("<head>"); 
     Response.Write("<!--[if gte mso 9]> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> <![endif]-->"); 
     Response.Write("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"\"text/html; charset=UTF-8\"\">"); 
     Response.Write("<meta name=ProgId content=Word.Document>"); 
     Response.Write("<meta name=Generator content=\"Microsoft Word 9\">"); 
     Response.Write("<meta name=Originator content=\"Microsoft Word 9\">"); 
     Response.Write("</head>"); 
     Response.Write("<body>"); 
     Response.Write("<div class=Section2>"); 

     // write some content here 

     Response.Write("</body>"); 
     Response.Write("</html>"); 
     HttpContext.Current.Response.Flush(); 
+0

謝謝。我會檢查這一點。那麼在腳本任務編碼部分,我怎麼不能維護我爲Reference添加的文件?每當我去編輯腳本時,我都必須添加參考文件來恢復編碼。是否有其他設置用於維護添加以供參考的文件? – 2010-05-11 06:41:15

+0

以及爲了獲得「迴應」而包含的命名空間是什麼?我沒有得到System.Web.HttpResponse !!!!!!! – 2010-05-11 06:45:29

+0

我不遵循您的第一條評論。也許你可以進一步解釋,並有人可以提出。 至於響應,如果你從你的web表單調用它,你應該可以直接使用。奇。您可能想嘗試Page.Response或Context.Response或System.Web.HttpContext.Current.Response。 – earthling 2010-05-11 06:51:44