引用命名空间

 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)的更多相关文章

  1. 在线读取office 文件(Word excel 等)

    https://view.officeapps.live.com/op/view.aspx?src=http://www.xxx.com/uploadfile/app/11.xls src 后面的网址 ...

  2. 微信小程序云开发-云存储-下载并打开文件文件(word/excel/ppt/pdf)

    一.wxml文件 1.写文本框,用来获取文件链接. 2.按钮,点击下载文件 <!-- 下载文件(word/excel/ppt/pdf等) --> <view class=" ...

  3. 微信小程序云开发-云存储-上传文件(word/excel/ppt/pdf)到云存储

    说明 word/excel/ppt/pdf是从客户端会话选择文件.使用chooseMessageFile中选择文件. 一.wxml文件 上传按钮,绑定chooseFile <!--上传文件(wo ...

  4. 微信小程序云开发-云存储-上传、下载、打开文件文件(word/excel/ppt/pdf)一步到位

    一.wxml文件 <!-- 上传.下载.打开文件一步执行 --> <view class="handle"> <button bindtap=&quo ...

  5. 在线文档转换API word,excel,ppt等在线文件转pdf、png

    在线文档转换API提供word,excel,ppt等在线文件转pdf.png等,文档:https://www.juhe.cn/docs/api/id/259 接口地址:http://v.juhe.cn ...

  6. Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件

    Aspose是一个很强大的控件,可以用来操作word,excel,ppt等文件,用这个控件来导入.导出数据非常方便.其中Aspose.Cells就是用来操作Excel的,功能有很多.我所用的是最基本的 ...

  7. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  8. java 如何将 word,excel,ppt如何转pdf--jacob

    问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice.   PS:1.本文 ...

  9. PDF/WORD/EXCEL/PPT 文档在线阅读

    查资料看了2种解决方法: 1.通过办公软件dll转换,用flans去看 2.通过Aspose转换成pdf格式,在用js前台读pdf(我用的pdf.js) 今天我解决的就是WORD/EXCEL/PPT ...

随机推荐

  1. Java NIO教程 Buffer

    缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存,这块内存中有很多可以存储byte(或int.char等)的小单元.这块内存被包装成NIO Buffer对象,并提供了一组方法,用来方便的访问 ...

  2. gdb调式

    1.PCB版的相应目录下执行命令: gdbserver 10.18.13.84:5555 DvdPlayer 2.linux操作系统执行:(如果是android找到android项目路径下的gdb)m ...

  3. QuickSort快速排序的多种实现和优化

    并不是很懂wikipedia上面说快排的空间复杂度最坏情况是O(NlogN)啊,难道不是空间复杂度平均O(logN),最坏O(N)么--原地快排难道不是只要算递归栈深度就好了么--有谁给我解释一下啊( ...

  4. 第三十三章 metrics(1) - graphite搭建 + whisper存储模式 + 高精度向低精度聚合方式 + 集成StatsD + 集成grafana

    组件介绍: carbon:Carbon实际上是一系列守护进程,组成一个Graphite安装的存储后端.这些守护进程用一个名为Twisted的事件驱动网络引擎监听时间序列数据.Twisted框架让Car ...

  5. sourceTree忽略跟踪文件

    1.未跟踪文件,直接在.gitignore文件打上文件名字,如果要忽略整个文件夹,要在文字后面加上一个/ .如:gradle/ . 2.已跟踪文件,先删除那些文件,再提交到git上,即删除远程的文件, ...

  6. Python学习笔记- Python threading模块

    Python threading模块 直接调用 # !/usr/bin/env python # -*- coding:utf-8 -*- import threading import time d ...

  7. memcache与memcached介绍及安装配置

    也许大家一看到Memcache和Memcached会有点晕,这两者有什么关系又有什么区别呢,下面先给大家说下Memcached,Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应 ...

  8. OA项目之导出

    要导出页的前台: <asp:Button runat="server" ID="btnExport" Text="导出" CssCla ...

  9. iOS 1 到 iOS 10 ,我都快老了

    iOS 1:iPhone诞生 虽然很难想像,但初代iPhone在问世时在功能方面其实远远落后于那时的竞争对手,比如Windows Mobile.Palm OS.塞班.甚至是黑莓.它不支持3G.多任务. ...

  10. C++进阶 面向对象基础(三)

    类的的定义: 初始化一般建议使用构造函数初始化列表形式: Person(const string nm, const string addr):name(nm), address(addr){} th ...