zl程序教程

您现在的位置是:首页 >  后端

当前栏目

ASP.NET实现word文档在线预览功能代码

Net文档ASP代码 实现 功能 在线 word
2023-06-13 09:14:23 时间
于是考虑在每个文件上传时为其生存一份HTMl文件,这样就能实现在线预览功能。主要代码如下
复制代码代码如下:

usingSystem;
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingWord=Microsoft.Office.Interop.Word;
publicpartialclasstest:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
WordToHtml("d:\\yijian.doc");
}
///<summary>
///word转成html
///</summary>
///<paramname="wordFileName"></param>
privatestringWordToHtml(objectwordFileName)
{
//在此处放置用户代码以初始化页面
Word.ApplicationClassword=newWord.ApplicationClass();
TypewordType=word.GetType();
Word.Documentsdocs=word.Documents;
//打开文件
TypedocsType=docs.GetType();
Word.Documentdoc=(Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null,docs,newObject[]{wordFileName,true,true});
//转换格式,另存为
TypedocType=doc.GetType();
stringwordSaveFileName=wordFileName.ToString();
stringstrSaveFileName=wordSaveFileName.Substring(0,wordSaveFileName.Length-3)+"html";
objectsaveFileName=(object)strSaveFileName;
docType.InvokeMember("SaveAs",System.Reflection.BindingFlags.InvokeMethod,null,doc,newobject[]{saveFileName,Word.WdSaveFormat.wdFormatFilteredHTML});
docType.InvokeMember("Close",System.Reflection.BindingFlags.InvokeMethod,null,doc,null);
//退出Word
wordType.InvokeMember("Quit",System.Reflection.BindingFlags.InvokeMethod,null,word,null);
returnsaveFileName.ToString();
}
}