【问题标题】:Protecting word Document with Password用密码保护word文档
【发布时间】:2013-03-28 04:53:25
【问题描述】:

我创建了一个 asp.net 应用程序,它具有将 Html 表转换为 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();

如果有任何方法可以为保存文件添加密码,请帮助我。 提前谢谢..

【问题讨论】:

    标签: c# asp.net ms-word export password-protection


    【解决方案1】:

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

    【讨论】:

    • 使用此代码从该位置打开文档需要更多时间。
    【解决方案2】:

    这是 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多