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. .CN根域名被攻击至瘫痪,谁之过?【转】

    2013年8月25日凌晨,.CN域名凌晨出现大范围解析故障,经分析.CN的根域授权DNS全线故障,导致大面积.CN域名无法解析.事故造成大量以.cn和.com.cn结尾的域名无法访问.直到当日凌晨4点 ...

  2. XML转化DS等

    public class XmlData    {        /// <summary>        /// 将DataTable对象转换成XML字符串        /// < ...

  3. vs2010打开设计器出现错误

    vs2010打开设计器出现此界面,  错误多种,还有“未将对象引用设置到对象的实例”  ,我项目用到了第三方控件(没有安装,bin文件夹导入DLL文件,项目直接引用的DLL文件),看下面的堆栈信息,显 ...

  4. hadoop 各种组件配置参数

    ********************************************hive*********************************************** hive ...

  5. php的cURL库介绍

    cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.很多小偷程序都是使用这个函数.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的 ...

  6. 学习笔记(一) HTML+CSS基础课程

    这个周把慕课网的<HTML+CSS基础课程>课程学完,内容都是非常非常基础的,不过还是学到了几个小知识点,记下来先. <a>超链接发送邮件 直接上把他的图片给挪过来了,我就不打 ...

  7. Git存储用户名和密码(明文需谨慎)

    当你配置好git后,在C:\Documents and Settings\Administrator\ 目录下有一个 .gitconfig 的文件,里面会有你先前配好的name 和email,只需在下 ...

  8. ubuntu远程windows服务器

    ubuntu端: sudo apt-get install rdesktop windows端: 需要允许此windows远程访问.我的windows是windows server2012,基本操作: ...

  9. PICT安装及使用

    一:PICT安装 1.下载pict33.msi:http://vdisk.weibo.com/s/d6k2tcgXDa7Eq 2.安装: 二:PICT的使用 1.在F:\PICT 目录下,新建一个tx ...

  10. python初学笔记(三)

    Unicode字符串 字符串还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特(bit)作为一个字节 (byte),所以,一 ...