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();
}
}
复制代码 代码如下:
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();
}
}
推荐阅读
-
ASP.NET实现word文档在线预览功能代码
-
Asp.Net平台下的图片在线裁剪功能的实现代码(源码打包)
-
ASP.NET实现word文档在线预览功能代码
-
Asp.Net平台下的图片在线裁剪功能的实现代码(源码打包)
-
asp.net实现word文档在线预览功能的方法
-
asp.net实现word文档在线预览功能的方法
-
word转pdf 转swf 实现在线预览word文件功能
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
-
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
-
asp.net 实现在线浏览word文档(word转html)