当前位置:Gxlcms > html代码 > InvokeMember打开Word文档另存为Html页如何先关闭Html再退出Word?_html/css_WEB-ITnose

InvokeMember打开Word文档另存为Html页如何先关闭Html再退出Word?_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:62人阅读

public partial class CountyTown : System.Web.UI.Page
{
public string countyName = string.Empty; //乡镇名称
public string documentFullName = string.Empty; //预案全称(包括后缀名)
public string documentName = string.Empty; //预案名称

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
//根据传递过来的乡镇名称获取到文件名称
countyName = Request["space"];
documentFullName = "江谭乡防台风和山洪灾害防治预案.docx";
documentName = documentFullName.Substring(0,documentFullName.LastIndexOf('.'));
}
catch (Exception)
{
documentFullName = "";
}
}
// 在此处放置用户代码以初始化页面

Application word = new Application();
Type wordType = word.GetType();
Documents docs = word.Documents; // 打开文件
Type docsType = docs.GetType();
object fileName = Server.MapPath(@"~\UpLoad\") + documentFullName;
Document doc = (Document)docsType.InvokeMember("Open",BindingFlags.InvokeMethod,null, docs,new Object[]{fileName, true, true }); // 转换格式,另存为


Type docType = doc.GetType();
object saveFileName = Server.MapPath(@"\UpLoad\") +documentName + ".html";

//string j="text/javascript";

ClientScript.RegisterClientScriptBlock(GetType(), "", "");

//保存HTML(报错:Word 无法保存此文件,因为它已在别处打开。)
docType.InvokeMember("SaveAs",BindingFlags.InvokeMethod, null, doc, new object[] {
saveFileName, WdSaveFormat.wdFormatHTML });
// 退出 Word
wordType.InvokeMember("Quit",BindingFlags.InvokeMethod, null, word, null);

Response.Redirect("~/Upload/" + documentName + ".html");
}


回复讨论(解决方案)

你本地测试不要打开word文档,将打开的word文档关闭掉。

没有打开word文档?

人气教程排行