欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-08 13:06:34
于是考虑在每个文件上传时为其生存一份html文件,这样就能实现在线预览功能。主要代码如下 复制代码 代码如下: using system; using system.col...
于是考虑在每个文件上传时为其生存一份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();
}
}