当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET实现word文档在线预览功能代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

11朵红玫瑰代表什么,张治太,日耳曼英雄

于是考虑在每个文件上传时为其生存一份html文件,这样就能实现在线预览功能。主要代码如下
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using word = microsoft.office.interop.word;
public partial class test : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
wordtohtml("d:\\yijian.doc");
}
/// <summary>
/// word转成html
/// </summary>
/// <param name="wordfilename"></param>
private string wordtohtml(object wordfilename)
{
//在此处放置用户代码以初始化页面
word.applicationclass word = new word.applicationclass();
type wordtype = word.gettype();
word.documents docs = word.documents;
//打开文件
type docstype = docs.gettype();
word.document doc = (word.document)docstype.invokemember("open", system.reflection.bindingflags.invokemethod, null, docs, new object[] { wordfilename, true, true });
//转换格式,另存为
type doctype = doc.gettype();
string wordsavefilename = wordfilename.tostring();
string strsavefilename = wordsavefilename.substring(0, wordsavefilename.length - 3) + "html";
object savefilename = (object)strsavefilename;
doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod, null, doc, new object[] { 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);
return savefilename.tostring();
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网