2010-05-11 149 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