• word文件转html字符串(包涵格式和图片)


    新项目客户有需求,用word编辑新闻,上传到服务器并显示到富文本编辑器,编辑后保存为html格式的文本。实现如下:

    首先引用 Microsoft.Office.Interop.Word.dll(需要安装office软件并设置组件服务,否则会报拒绝访问错误)

    转换方法:

    using System;
    using System.Text;
    using MSWord = Microsoft.Office.Interop.Word;
    using System.IO;
    using System.Reflection;


    namespace ReadWord
    {
        public class GetHtmlString
        {
            /// <summary>
            /// word转html字符串   --Will.Wang
            /// </summary>
            /// <param name="wordPath">word文件绝对路径</param>
            /// <returns>html字符串</returns>
            public static string GetProceHtmlString(String wordPath)
            {
                string htmlPath = GetHtml(wordPath);
                string htmlString = ProceHtmlString(htmlPath);
                return htmlString;
            }
            /// <summary>
            /// word转html并返回html文件地址
            /// </summary>
            /// <returns></returns>
            private static string GetHtml(Object path)
            {

                MSWord.Application wordApp;
                MSWord.Document wordDoc;
                Object Nothing = Missing.Value;

                wordApp = new MSWord.Application();
                wordDoc = wordApp.Documents.Add(ref path, ref Nothing, ref Nothing, ref Nothing);

                object format = MSWord.WdSaveFormat.wdFormatFilteredHTML;
                Object newPath = path.ToString().Substring(0, path.ToString().LastIndexOf('.'))+".html";//html文件路径

                wordDoc.SaveAs(ref newPath, ref format, ref Nothing, ref Nothing, ref Nothing,
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

                return newPath.ToString();
            }


            /// <summary>
            /// 读取html字符串
            /// </summary>
            /// <param name="htmlPath"></param>
            /// <returns></returns>
            private static string ProceHtmlString(String htmlPath)
            {
                FileStream fs = new FileStream(htmlPath, FileMode.OpenOrCreate, FileAccess.Read);
                StreamReader sr = new StreamReader(fs, Encoding.Default);
                string htmlString = sr.ReadToEnd();

                sr.Close();
                fs.Close();
                return htmlString;
            }
        }
    }

  • 相关阅读:
    SpringMVC学习指南【笔记6】JSTL标签、函数
    SpringMVC学习指南【笔记5】EL表达式、实现免脚本JSP页面、禁用EL计算的设置
    SpringMVC学习指南【笔记4】数据绑定、表单标签库、转换器、格式化、验证器
    序列封包和序列解包
    python 字符串分割,连接方法
    Jmeter常用插件(转)
    不同的content-type,Jmeter入参不同
    性能监测(CPU)
    正则表达式
    乱码问题
  • 原文地址:https://www.cnblogs.com/xiaowei3632/p/9144001.html
Copyright © 2020-2023  润新知