2013-03-28 84 views
0

我已经创建了一个asp.net应用程序,它具有将Html表转换为word文档的一种功能。我已经成功完成了这项工作但现在我需要用密码保护我的文件。是否可以通过添加密码来使文件安全。我的转换代码如下所示。用密码保护word文档

Response.ClearContent(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", DateTime.Today.ToShortDateString().Replace("/", "").Replace("-", "") + "_" + DateTime.Now.ToShortDateString() + ".doc")); 
     Response.ContentType = "application/ms-word"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 

     tblMain.RenderControl(htw); 

     string strPath = Request.PhysicalApplicationPath + "Test.doc"; 
     StreamWriter sWriter = new StreamWriter(strPath); 
     sWriter.Write(sw.ToString()); 
     sWriter.Close(); 

请帮助我,如果有任何方法可以将密码添加到保存文件。 在此先感谢...

回答

1

Word.Document允许您通过传递密码作为参数来保存文档。请参阅this

+0

其花费更多的时间通过使用此代码,打开该定位的文件。 – MAC 2013-04-01 04:59:55

1

这是C#,但看起来是您正在寻找的属性。

ThisDocument.SaveAs("c:\test\MyNewDocument.doc") 

// C# 
Object fileName = @"C:\Test\MyNewDocument.doc"; 
Object password = Type.Missing; 
Object writePassword = Type.Missing; 

ThisDocument.SaveAs(ref fileName, ref password, ref writePassword); 

http://msdn.microsoft.com/en-us/library/office/aa192495(v=office.11).aspx