.NET读取Office文件内容(word、excel、ppt)
引用命名空间
- using Microsoft.Office.Core;
- using Word = Microsoft.Office.Interop.Word;
- using Excel = Microsoft.Office.Interop.Excel;
- using PowerPoint = Microsoft.Office.Interop.PowerPoint;
Word文件的读取
- public string ReadFile()
- {
- string text = string.Empty;
- Word.ApplicationClass app = null;
- Word.Document doc = null;
- object readOnly = true;
- object missing = System.Reflection.Missing.Value;
- object fileName = this.FileInstance.FullName;
- try
- {
- app = new Microsoft.Office.Interop.Word.ApplicationClass();
- doc = app.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
- text = doc.Content.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);
- }
- catch
- {
- }
- finally
- {
- doc.Close(ref missing, ref missing, ref missing);
- doc = null;
- app.Quit(ref missing, ref missing, ref missing);
- app = null;
- }
- return text;
- }
Excel文件的读取
- public string ReadFile()
- {
- string text = string.Empty;
- Excel.ApplicationClass app = null;
- Excel.Workbook book = null;
- object readOnly = true;
- object missing = System.Reflection.Missing.Value;
- object fileName = this.FileInstance.FullName;
- try
- {
- app = new Microsoft.Office.Interop.Excel.ApplicationClass();
- book = app.Workbooks.Open(fileName.ToString(), missing, readOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
- foreach (Excel.Worksheet sheet in book.Sheets)
- {
- for (int i = ; i <= sheet.UsedRange.Cells.Rows.Count; i++)
- {
- for (int j = ; j <= sheet.UsedRange.Cells.Columns.Count; j++)
- {
- text += ((Excel.Range)sheet.Cells[i, j]).Text.ToString().Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty) + " ";
- }
- }
- }
- }
- catch
- {
- }
- finally
- {
- book.Close(missing, fileName, missing);
- book = null;
- app.Quit();
- app = null;
- }
- return text;
- }
PPT文件的读取
- public override string ReadFile()
- {
- string text = string.Empty;
- PowerPoint.ApplicationClass app = null;
- PowerPoint.Presentation pp = null;
- object readOnly = true;
- object missing = System.Reflection.Missing.Value;
- object fileName = this.FileInstance.FullName;
- try
- {
- app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
- pp = app.Presentations.Open(fileName.ToString(), Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
- foreach (PowerPoint.Slide slide in pp.Slides)
- {
- foreach (PowerPoint.Shape shape in slide.Shapes)
- {
- text += shape.TextFrame.TextRange.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty) + " ";
- }
- }
- }
- catch
- {
- }
- finally
- {
- pp.Close();
- pp = null;
- app.Quit();
- app = null;
- }
- return text;
- }
.NET读取Office文件内容(word、excel、ppt)的更多相关文章
- 在线读取office 文件(Word excel 等)
https://view.officeapps.live.com/op/view.aspx?src=http://www.xxx.com/uploadfile/app/11.xls src 后面的网址 ...
- 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)
一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...
- 微信小程序云开发-云存储-上传文件(word/excel/ppt/pdf)到云存储
说明 word/excel/ppt/pdf是从客户端会话选择文件.使用chooseMessageFile中选择文件. 一.wxml文件 上传按钮,绑定chooseFile <!--上传文件(wo ...
- 微信小程序云开发-云存储-上传、下载、打开文件文件(word/excel/ppt/pdf)一步到位
一.wxml文件 <!-- 上传.下载.打开文件一步执行 --> <view class="handle"> <button bindtap=&quo ...
- 在线文档转换API word,excel,ppt等在线文件转pdf、png
在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...
- Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件
Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
- java 如何将 word,excel,ppt如何转pdf--jacob
问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice. PS:1.本文 ...
- PDF/WORD/EXCEL/PPT 文档在线阅读
查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...
随机推荐
- enum 枚举的使用
在程序当中,我们经常定义一些常量来标识一些状态,类型等. 比如 定义订单的状态,可以定义为ORDER_STATUS_CANCEL = 1 表示订单状态为"订单已取消". 但是感觉定 ...
- Oracle 11g AWR和ADDM性能报告
一.自动工作负载库(Automatic Workload Repository,AWR) 自动工作负载库(Automatic Workload Repository,AWR)是在Oracle公司提供的 ...
- 存在网路的情况下重命名SDE中数据图层错误(The orphan junction feature class cannot be renamed)
运行环境为ArcGIS9.3,VS2008. 问题描述:数据通过SDE存储在Oracle10g数据库中,数据集中存在几何网络,在存在网络的情况下通过程序对其中的数据图层进行重命名,弹出"Th ...
- easyui中tree使用simpleData的形式加载数据
了解了zTree的使用, 发现它的simpleData是非常好用的, 由后台返回一个扁平数据, 直接在前台解析成树形菜单, 网上查了一下, easyui也可以简单实现, 不过....没看懂, 先记录一 ...
- javascript 封装分页
最近自己做了一个后台,想把分页通过js给封装起来 于是乎就有了下面的代码 此代码,算是一个半成品,还需完善,思路还是可以借鉴的 page方法传入3个参数 1.total总条数 2.page当前页码 3 ...
- 2016 小马哥 IOS
2016 小马哥 IOS 最新视频完整版 链接:http://pan.baidu.com/s/1c1EQlBM 密码:mxkt
- Python-Windows下安装BeautifulSoup和requests第三方模块
http://blog.csdn.net/yannanxiu/article/details/50432498 首先给出官网地址: 1.Request官网 2.BeautifulSoup官网 我下载的 ...
- 小希的数表2(用的for循环)
#include<iostream> 02.#include<algorithm> 03.using namespace std; 04. 05.bool cmp(int a, ...
- 【html】:禁止鼠标事件
<body oncontextmenu="return false" onselectstart="return false" ondragstart=& ...
- 0909 45作业one
1.编译原理学什么? 答: 初遇编译原理,我知道编译原理是计算机专业设置的一门重要的专业课程,主要是介绍编译程序构造的一般原理和基本方法.其内容大概包括语言和文法.词法分析.语法分析.语法制导翻译.中 ...