1、添加Microsoft.Vbe.Interop.dll引用。

2、以下方法可以简单的读取到word文档文字内容,不包括图片、格式等。

private string ReadWordFile(string file)
{
string filePath = Server.MapPath(file);
if (System.IO.File.Exists(filePath))
{
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
object fileobj = filePath;
object nullobj = System.Reflection.Missing.Value;
//打开指定文件(不同版本的COM参数个数有差异,一般而言除第一个外都用nullobj就行了)
Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
//取得doc文件中的文本
string outText = doc.Content.Text; //关闭文件
doc.Close(ref nullobj, ref nullobj, ref nullobj); //关闭COM
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj); //返回
return outText;
} return null;
}

3、把word文档正确展示出来,把word转换成html文档,然后读取出来。

private void ChangeWordToHtml(string docFilePath, string htmlFilePath)
{
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents; Type docsType = docs.GetType();
object file = docFilePath;
Document doc = (Document)docsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, (object)docs, new Object[] { file, true, true });
object nullobject = System.Reflection.Missing.Value;
//判断与文件转换相关的文件是否存在,存在则删除。(这里,最好还判断一下存放文件的目录是否存在,不存在则创建)
if (File.Exists(htmlFilePath))
{
File.Delete(htmlFilePath);
} //每一个html文件,有一个对应的存放html相关元素的文件夹(html文件名.files)
if (Directory.Exists(htmlFilePath.Replace(".html", ".files")))
{
Directory.Delete(htmlFilePath.Replace(".html", ".files"), true);
} Type docType = doc.GetType();
object saveFileName = htmlFilePath;
docType.InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatHTML });
doc.Close(ref nullobj, ref nullobj,ref nullobj); // 退出 Word
wordType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, word, null);
}
private string LoadFileContent(string fileName)
{
if (fileName != "")
{
string path ="~/path/";
string docFilePath = Server.MapPath(path + fileName);
string htmlFilePath = Server.MapPath(path + fileName.Split('.')[] + ".html");
ChangeWordToHtml(docFilePath, htmlFilePath);
if (File.Exists(htmlFilePath))
{
              
string content = File.ReadAllText(htmlFilePath, Encoding.Default);
return content;
}
}
return null;
}

asp.net word内容读取到页面的更多相关文章

  1. Java word 内容读取

    1.添加依赖关系(网上好多帖子没有写依赖,害我找半天) <dependency>            <groupId>org.apache.poi</groupId& ...

  2. java读取word内容

    暂时只写读取word内容的方法. 依赖的jar: poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarxmlbeans-2.3.0.jar package co ...

  3. OpenXml读取word内容(二)

    注意事项 上一篇已经说明,这次就不一一说了,直接来正文: word内容 相关代码 方法1 static void Main(string[] args) { string wordPathStr = ...

  4. OpenXml读取word内容(一)

    OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...

  5. python如何转换word格式、读取word内容、转成html

    # python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...

  6. OpenXml读取word内容注意事项

    OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...

  7. c#读取word内容,c#提取word内容

    Post by 54admin, 2009-5-8, Views:575 1: 对项目添加引用,Microsoft Word 11.0 Object Library 2: 在程序中添加 using W ...

  8. OpenXml读取word内容(三)

    内容和表格内容一起读: word内容: 代码: public static void ReadWordByOpenXml(string path) { using (WordprocessingDoc ...

  9. 如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites]

    如何在ASP.NET Web站点中统一页面布局[Creating a Consistent Layout in ASP.NET Web Pages(Razor) Sites] 一.布局页面介绍[Abo ...

随机推荐

  1. 优雅智慧女性课程班 - 公开课程 - 课程介绍 - 中国人民大学商学院EDP中心

    优雅智慧女性课程班 - 公开课程 - 课程介绍 - 中国人民大学商学院EDP中心 优雅智慧女性课程班 课程总览 思想睿智成熟,外表美丽自信,气质优雅端庄,是魅力女性所应具备的特性.在当今不确定环境下, ...

  2. Acess错误:"文件共享锁定数溢出"

    对于ACCESS数据库,如果通过大量的SQL来操作数据库或者直接操作大量的数据时,经常会出现这种错误: "文件共享锁定数溢出" 原因如下:     Access数据库,同时操作大量 ...

  3. linux下的进程、网络、性能监控命令

    进程监控 端口   1)netstat -anp | grep xxx   2)lsof  -i:<port> 句柄    lsof  -n | grep xxxx 线程栈   1)gst ...

  4. 在.NET下学习Extjs(第三个案例 Array的过滤方法(filter))

    Ext.Array.filter(Array array,Function fn,Object scope):Array array是一个数组,fn是过滤函数,scope是作用域,filter返回的是 ...

  5. Android Studio的配置

    第一次使用Android Studio时你应该知道的一切配置 - 生命壹号 - 博客园http://www.cnblogs.com/smyhvae/p/4390905.html

  6. PHP正则提取HTML中img的url值

    <html> <head> <title>获取HTML中的img的src值</title> </head> <body> < ...

  7. mysql学习(四)-字段类型

    mysql数据类型: 数值型: 整形:int 浮点型:float double decimal:定点型 日期: date  '2012-01-02' time '10:01:01' datetime ...

  8. Mysql中时间的操作笔记

    1.创建修改表时,为datetime字段设置当前时间为默认值 CREATE TABLE `NewTable` ( `id` int(11) NOT NULL AUTO_INCREMENT , `des ...

  9. avalon.js 多级下拉框实现

    学习avalon.js的时候,有一个多级下拉框的例子,地址 戳这里 代码实现了联动, 但是逻辑上面理解有点难度,获取选择的值 和 页面初始化 功能存在问题. 在写地图编辑的时候,也用到了多级下拉框,特 ...

  10. 向mysql添加新用户并分配权限

    首先要声明一点,大部分情况下,修改MySQL是需要有mysql里的root权限的,所以一般用户无法更改密码,除非请求管理员. 方法一使用phpmyadmin,这是最简单的了,修改mysql库的user ...