ASP.NET实现word文档在线预览功能代码
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");
}
///
/// word转成html
///
///
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();
}
}