码迷,mamicode.com
首页 > Web开发 > 详细

asp.net如何实现word文档在线预览

时间:2014-11-05 16:25:32      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   os   for   sp   strong   文件   div   

实现方式:office文档转html,再在浏览器里面在线浏览

 

1、首先引入com组件中office库,然后在程序集扩展中引入word的dll

 

bubuko.com,布布扣

 

bubuko.com,布布扣

 

2、将Microsoft.Office.Interop.Word的嵌入互操作类型设置为 false,如图

bubuko.com,布布扣

 

3、主要代码

 
C# 代码   复制
bubuko.com,布布扣
bubuko.com,布布扣using System;
bubuko.com,布布扣using System.Collections.Generic;
bubuko.com,布布扣using System.Linq;
bubuko.com,布布扣using System.Web;
bubuko.com,布布扣using Microsoft.Office.Core;
bubuko.com,布布扣using Word = Microsoft.Office.Interop.Word;
bubuko.com,布布扣namespace Wolfy.OfficePreview
bubuko.com,布布扣{
bubuko.com,布布扣    public class Office2HtmlHelper
bubuko.com,布布扣    {
bubuko.com,布布扣        /// <summary>
bubuko.com,布布扣        /// Word转成Html
bubuko.com,布布扣        /// </summary>
bubuko.com,布布扣        /// <param name="path">要转换的文档的路径</param>
bubuko.com,布布扣        /// <param name="savePath">转换成html的保存路径</param>
bubuko.com,布布扣        /// <param name="wordFileName">转换成html的文件名字</param>
bubuko.com,布布扣        public static void Word2Html(string path, string savePath, string wordFileName)
bubuko.com,布布扣        {
bubuko.com,布布扣
bubuko.com,布布扣            Word.ApplicationClass word = new Word.ApplicationClass();
bubuko.com,布布扣            Type wordType = word.GetType();
bubuko.com,布布扣            Word.Documents docs = word.Documents;
bubuko.com,布布扣            Type docsType = docs.GetType();
bubuko.com,布布扣            Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { (object)path, true, true });
bubuko.com,布布扣            Type docType = doc.GetType();
bubuko.com,布布扣            string strSaveFileName = savePath + wordFileName + ".html";
bubuko.com,布布扣            object saveFileName = (object)strSaveFileName;
bubuko.com,布布扣            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
bubuko.com,布布扣            docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
bubuko.com,布布扣            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
bubuko.com,布布扣
bubuko.com,布布扣        }
bubuko.com,布布扣    }
bubuko.com,布布扣}
bubuko.com,布布扣

 

调用

Office2HtmlHelper.Word2Html(MapPath("/Doc/分析某网站的SEO策略(外链篇).doc"), MapPath("/Html/"), "分析某网站的SEO策略(外链篇)");

asp.net如何实现word文档在线预览

标签:http   io   ar   os   for   sp   strong   文件   div   

原文地址:http://www.cnblogs.com/ranran/p/4076381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!