如何将word文档保存到文件系统而不是将其打印出来?

时间:2015-01-21 18:32:51

标签: c# asp.net webforms

在我的应用程序中,我将div打印到这样的word文档:

Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=testme.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/doc";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
divExport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

而不是在浏览器上打开它如何将其保存到路径?通常在使用ItextSharp时,我会声明路径和现有模板文件。

private static string AppRelativePath_1page = "~/DesktopModules/MyFolder";
private static string AppAbsolutePath_1page = HttpContext.Current.Server.MapPath(AppRelativePath_1page);

在这种情况下,没有现有的模板文件。我如何在服务器文件夹中创建此文件?

2 个答案:

答案 0 :(得分:3)

打开StreamWriter,而不是写入Response对象,写入您的StreamWriter。

答案 1 :(得分:0)

您可以使用Streamwriter

来实现
 using (StreamWriter sw = new StreamWriter(Path))
 {                
    sw.Write(stringWrite.ToString());               
 }