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 ...
随机推荐
- 网易云课堂_程序设计入门-C语言_第三周:循环_1奇偶个数
1 奇偶个数(5分) 题目内容: 你的程序要读入一系列正整数数据,输入-1表示输入结束,-1本身不是输入的数据.程序输出读到的数据中的奇数和偶数的个数. 输入格式: 一系列正整数,整数的范围是(0,1 ...
- 在magento中发邮件
1. 在system->Configuration->Store Email Addresses中设置General Contact的Sender Name.Sender Email. S ...
- 一个利用扩展方法的实例:AttachDataExtensions
扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...
- kindeditor更改图片上传时网络图片的路径
当我们想要使用kindeditor的图片上传功能时,有两种选择图片方式,一种是本地选择,一种是在图片空间中选择,图片空间的默认地址是server上的/kindeditor/attached/image ...
- 20151225--easyUI
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- js基础——cssText
以前,只知道设置css里的属性都是通过 元素.style.属性名 = 属性值 的方式,此时经常遇到的情况就是类似这样: var head= document.getElementById(&quo ...
- 记录下url拼接的多条件筛选js
本着为提高工作效率百度或者google这些代码发现拿过来的都不好用,然后自己写了个,写的一般但记录下以后再优化 <html> <head> <script> $(f ...
- 用C++编写程序,输出两个字符串的最大公共子字符串
#include<iostream> #include<string> using namespace std; int main() { string s_l,s_sh; ...
- 【phpcms-v9】如何实现在含有子栏目的栏目下添加内容?
对于题目的解释: 假设现在有一个一级栏目 为:栏目1 其下有二级栏目 :栏目1=>栏目11,栏目1=>栏目12,栏目1=>栏目13 同时栏目1下有文章列表 : 栏目1-----文章 ...
- java中JSON串转换并获取数据
String strAllImp= " [{"id":2,"name":"还不错","count":3}]&q ...