分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

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

发布时间:2023-09-06 01:58责任编辑:苏小强关键词:word

首先引用 Microsoft.Office.Interop.Word.dll

转换方法:

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;
        }
    }
}

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

原文地址:https://www.cnblogs.com/xiaowei3632/p/9144001.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved