新项目客户有需求,用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. Java五种单例区别

    详细请参考如下链接: http://www.voidcn.com/article/p-shzgsluz-bqa.html https://blog.csdn.net/android_freshman/ ...

  2. python:面向对象初级

    面向对象编程类的概念 : 具有相同属性和技能的一类事物 人类 抽象对象 : 就是对一个类的具体的描述 具体的人 具体 使用面向对象的好处: 使得代码之间的角色关系更加明确 增强了代码的可扩展性 规范了 ...

  3. [UE4]让箭头保持水平

    如图所示,当手柄前后左右转动的时候,箭头也会跟着转动,我们的目标是要求箭头紧贴着地面,不会跟着手柄前后左右转动. 分析上图坐标系可以知道,只要让箭头绕着Z轴转动就可以了,不需要绕着X轴和Y轴旋转. 

  4. vue 路由别名 路由跳转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 转HDMI

    HDMI协议解析 2017年06月18日 14:19:27 阅读数:2987 转载请标明出处floater的csdn blog,http://blog.csdn.net/flaoter 本文从软件工程 ...

  6. [java,2017-06-12] myEclipse双击无法打开文件

    点击 Window---->Preferences---> General---> Editors ---> File Associations 选择Associated ed ...

  7. H5转图片支持保存

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 02-03 CSS快速入门

    css四种引入方式:test.html: p{ color: gold; font-size: 20px; } title.html: <!DOCTYPE html> <html l ...

  9. mysql 函数获取子节点

    DELIMITER $$ USE `topsale`$$ DROP FUNCTION IF EXISTS `getShopIdByUserId`$$ CREATE DEFINER=`root`@`%` ...

  10. JAVA设计方法思考之如何实现一个方法执行完毕后自动执行下一个方法

    今天编程时,突然想起来在一些异步操作或Android原生库的时候,需要我们实现一些方法, 这些方法只需要我们具体实现,然后他们会在适当的时候,自动被调用! 例如AsyncTask,执行玩doInBac ...