新项目客户有需求,用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;
        }
    }
}

word文件转html字符串(包涵格式和图片)的更多相关文章

  1. 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

    原文:将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小) WPF的XAML文档(Main.xaml): <Window x:Class="SVG2Image.Ma ...

  2. 帝国CMS 7.5编辑器从WORD中粘贴过来无法保留格式和图片的解决办法

      配置过滤js文件 首先打开  \editor\plugins\pastefromword\filter\default.js  在文件的最后部分又如下代码(修改前的代码),也可以搜索CKEDITO ...

  3. JSP生成word文件

    1.jsp生成word文件,直接改动jsp格式: <%@ page contentType="application/vnd.ms-word;charset=GB2312"% ...

  4. 基于java 合并.doc和docx格式的Word文件

    注:摘录自 https://www.cnblogs.com/shenzhouyh/articles/7243805.html 之前用过jacob 合并.doc,但是是有jacob有弊端: 服务器必须是 ...

  5. java把Word文件转成html的字符串返回出去

    1.需求是把前端上传的word文件解析出来,生成html的字符串返回给前端去展示,Word里面的图片可以忽略不显示,所以这段代码去掉了解析图片的代码 package com.lieni.core.ut ...

  6. php base64格式的图片字符串和图片文件相互转换的代码

    在移动端上传图片的时候通常会将图片转换成base64格式的字符串提交,所以此时需要使用服务器端的程序进行转换成二进制的数据.如下PHP代码实现了图片文件和base64格式的图片字符串相互转换的方法,同 ...

  7. C#对word、excel、pdf等格式文件的操作总结

    一.word 这是我以前工作时写过的一个业务逻辑处理类,里面有不少文件操作的方法,这里主要关注一下C#对word的操作.里面的方法可以直接拿出来用,主要是通过word的dot模版来进行创建word.替 ...

  8. java 导出数据为word文档(保持模板格式)

    导出数据到具体的word文档里面,word有一定的格式,需要保持不变 这里使用freemarker来实现: ①:设计好word文档格式,需要用数据填充的地方用便于识别的长字符串替换  如  aaaaa ...

  9. JAVA:借用OpenOffice将上传的Word文档转换成Html格式

    为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...

随机推荐

  1. [转]SQL中的case when then else end用法

      Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' EN ...

  2. javaAgent介绍

    JavaAgent(转载) http://www.cnblogs.com/diyunpeng/archive/2011/05/26/2057932.html 一文带你了解Java Agent http ...

  3. dp练习--

    动态规划(DP)算法     动态规划是运筹学的一个分支,是求解决策过程最优化的数学方法.利用各个阶段之间的关系,逐个求解,最终求得全局最优解,需要确认原问题与子问题.动态规划状态.边界状态.边界状态 ...

  4. linux日志logger命令详解

    通过logger命令记录日志 logger是一个shell命令接口,可以通过该接口使用Syslog的系统日志模块,还可以从命令行直接向系统日志文件写入一行信息. ------------------- ...

  5. k2datas 基础编程题,判断字符串是否有重复串

    package String; public class DuplicateString { public static boolean isDup(String s) throws Exceptio ...

  6. C语言排序算法学习笔记——插入类排序

    排序就是讲原本无序的序列重新排序成有序的序列.序列里可以是一个单独数据,也可以是多个数据组合的记录,按照记录里的主关键字或者次关键字进行排序. 排序的稳定性:如果排序表中有两个元素R1,R2,其对应的 ...

  7. Linux背背背(5)

    目录: 1.rpm 2.运行模式 3.网卡 rpm软件管理 在Linux中的rpm其实有点类似于windows下的“xxx电脑管家”.“xxx软件管家”,其作用就是管理软件(查询软件的安装情况,安装软 ...

  8. kolla queens on centos7.5 -all in one

    目录 环境准备 开始配置 快照,快照,快照 pull镜像并部署 登录配置OpenStack 环境准备 我这里用workstation创建了一个虚拟机,安装centos7.5 mini系统,这台虚拟机上 ...

  9. 【BZOJ2054】疯狂的馒头(并查集)

    /* 经典思路, 倒序并查集处理即可 */ #include<cstdio> #include<algorithm> #include<cstring> #incl ...

  10. [UnityShader基础]07.MaterialPropertyDrawer

    参考链接: https://blog.csdn.net/e295166319/article/details/60141677 https://docs.unity3d.com/ScriptRefer ...