嗨,我创建了一个 asp.net 应用程序,在其中我使用下面的代码片段将 asp:panel 转换为 word 文档。
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
tblMain.RenderControl(htw);
if (strType.Equals("Save"))
{
string filename = DateTime.Now.ToString().Replace("/", "").Replace("-", "").Replace(":", "").Replace(" ", "") + ".doc";
string strPath = Server.MapPath("Attachments") + "\\" + filename;
StreamWriter sWriter = new StreamWriter(strPath);
sWriter.Write(sw.ToString());
sWriter.Close();
现在文件正在保存到指定的文件夹。之后,我正在获取相同的文件以附加密码以进行保护。我也使用了下面的代码。
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object readOnly = false;
object visible = true;
object password = "123";
object fileToOpen = strPath;
try
{
doc = wordApp.Documents.Open(ref fileToOpen, ref missing, ref readOnly, ref missing, ref missing,
ref missing, ref missing, ref password, ref missing, ref missing, ref missing,
ref visible, ref visible, ref missing, ref missing, ref missing);
doc.Activate();
doc.Protect(WdProtectionType.wdAllowOnlyComments,ref missing, ref password, ref missing, ref missing);
doc.SaveAs(ref fileToOpen, ref missing, ref missing, ref password, ref missing, ref password, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
// exception catching
}
finally
{
doc.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing);
}
但它会将文件与一个名为 filename_files 的文件夹一起保存。
如果我删除该行doc.Protect(WdProtectionType.wdAllowOnlyComments,ref missing, ref password, ref missing, ref missing);
它将仅保存 doc 文件,但不附加密码。
任何想法?请帮我解决这个问题。邀请所有的建议。
提前致谢..