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. android studio 环境配置

    遇到哪些坑: Gradle:configure project 卡死在此处 Haxm is not installed hax is not working and emulator runs in ...

  2. Aix 文件名补齐及aix6.1 bash安装

    Aix历史查询快捷键=>按ESC+k 设置KSH的自动补全(仅仅是文件名补全,没有命令补全)和历史命令功能 方法一: set -o vi 历史命令功能(esc -,esc +)自动补全文件名(e ...

  3. aix puppet agent

    demo控制脚本tel,150 5519 8367 Running Puppet on AIX Puppet on AIX is… not officially supported, yet stil ...

  4. Fibonacci Tree(最小生成树,最大生成树)

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. 当PullToRefreshScrollView里面嵌套ListView

    当PullToRefreshScrollView里面嵌套ListView,ListView上面还是有内容的,当下拉刷新的 时候,数据填充完成之后ListView就会往上面滑动,导致ListView上面 ...

  6. Hadoop学习之编译eclipse插件

    近期准备開始学习Hadoop1.2.1的源码,感觉最好的方法还是能够在运行Hadoop及hadoop作业时跟踪调试代码的实际运行情况.因为选择的IDE为eclipse,所以准备编译一下hadoop的e ...

  7. bootstarp基本模板

    <!DOCTYPE html><!--html5文档格式--> <html lang="zh-CN"><!--申明语言是中文简体--> ...

  8. js清空页面控件值

    function funClear() {var txts = document.getElementsByTagName("input");for (var i = 0; i & ...

  9. 常用的ASCII码对照表

  10. Python进阶之面向对象编程概述

    Python面向对象编程(一) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB& ...