asp.net实现word文档在线预览功能的方法
程序员文章站
2024-02-23 19:11:10
本文实例讲述了asp.net实现word文档在线预览功能的方法。分享给大家供大家参考。具体实现方法如下:
实现方式:office文档转html,再在浏览器里面在线浏览...
本文实例讲述了asp.net实现word文档在线预览功能的方法。分享给大家供大家参考。具体实现方法如下:
实现方式:office文档转html,再在浏览器里面在线浏览
1、首先引入com组件中office库,然后在程序集扩展中引入word的dll
2、将microsoft.office.interop.word的嵌入互操作类型设置为 false,如图
3、主要代码:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using microsoft.office.core;
using word = microsoft.office.interop.word;
namespace wolfy.officepreview
{
public class office2htmlhelper
{
/// <summary>
/// word转成html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savepath">转换成html的保存路径</param>
/// <param name="wordfilename">转换成html的文件名字</param>
public static void word2html(string path, string savepath, string 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[] { (object)path, true, true });
type doctype = doc.gettype();
string strsavefilename = savepath + wordfilename + ".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);
wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod, null, word, null);
}
}
}
using system.collections.generic;
using system.linq;
using system.web;
using microsoft.office.core;
using word = microsoft.office.interop.word;
namespace wolfy.officepreview
{
public class office2htmlhelper
{
/// <summary>
/// word转成html
/// </summary>
/// <param name="path">要转换的文档的路径</param>
/// <param name="savepath">转换成html的保存路径</param>
/// <param name="wordfilename">转换成html的文件名字</param>
public static void word2html(string path, string savepath, string 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[] { (object)path, true, true });
type doctype = doc.gettype();
string strsavefilename = savepath + wordfilename + ".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);
wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod, null, word, null);
}
}
}
调用:
复制代码 代码如下:
office2htmlhelper.word2html(mappath("/doc/分析某网站的seo策略(外链篇).doc"), mappath("/html/"), "分析某网站的seo策略(外链篇)");
希望本文所述对大家的asp.net程序设计有所帮助。