asp.net word内容读取到页面
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内容读取到页面的更多相关文章
- Java word 内容读取
1.添加依赖关系(网上好多帖子没有写依赖,害我找半天) <dependency> <groupId>org.apache.poi</groupId& ...
- java读取word内容
暂时只写读取word内容的方法. 依赖的jar: poi-3.9-20121203.jarpoi-ooxml-3.9-20121203.jarxmlbeans-2.3.0.jar package co ...
- OpenXml读取word内容(二)
注意事项 上一篇已经说明,这次就不一一说了,直接来正文: word内容 相关代码 方法1 static void Main(string[] args) { string wordPathStr = ...
- OpenXml读取word内容(一)
OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...
- python如何转换word格式、读取word内容、转成html
# python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...
- OpenXml读取word内容注意事项
OpenXml读取word内容注意事项 1.使用OpenXml读取word内容,word后缀必须是".docx":如果word后缀是".doc"需要转成&quo ...
- c#读取word内容,c#提取word内容
Post by 54admin, 2009-5-8, Views:575 1: 对项目添加引用,Microsoft Word 11.0 Object Library 2: 在程序中添加 using W ...
- OpenXml读取word内容(三)
内容和表格内容一起读: word内容: 代码: public static void ReadWordByOpenXml(string path) { using (WordprocessingDoc ...
- 如何在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 ...
随机推荐
- 杭电oj1326 Box of Bricks
Tips:先求出平均数再分别计算各数与平均数的差相加,注意两个测试结果之间要空一行 #include<iostream> using namespace std; int main() { ...
- SDUT OJ 2783 小P寻宝记
#include<iostream> #include<memory.h> #define N 10020 using namespace std; int dp[N],pi[ ...
- vs2010打开设计器出现错误
vs2010打开设计器出现此界面, 错误多种,还有“未将对象引用设置到对象的实例” ,我项目用到了第三方控件(没有安装,bin文件夹导入DLL文件,项目直接引用的DLL文件),看下面的堆栈信息,显 ...
- OCP prepare 20140703
1. trim trim('aaa' from 'aaabbbccc') 这个是错误的.ora-30001: trim set should have only one character 2. in ...
- VC/MFC使用OLE操作 EXCEL
1.VC插入sheet页到指定位置 插入sheet的函数用 sheets.Add(Before, After,Count,Type) 四个参数含义如下: 四个const VARIANT: ...
- shell基础认识
Shell 我们在终端下写命令Linux内核是看不懂的必须通过shell解释成内核可执行的代码 这就是shell(其实解释命令这只是它的一个功能模块,shell还可以用来进行程序设计) 有点类似win ...
- nodeJs入门笔记(二)
js中window通常是全局变量 global 是node.js里的全局变量 node中能访问的对象一般都是 global的 属性 global 对象属性 process 用于描述当前Node 进程状 ...
- Web 前端知识点
- php将xml文件转化为数组:simplexml_load_string
<?php $str = <<<XML <?xml version="1.0" encoding="ISO-8859-1"?> ...
- jQuery报错:
jQuery报错:Uncaught ReferenceError: $ is not defined 在使用jQuery的时候,发现有如下报错: Uncaught ReferenceError: $ ...