.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 ...
随机推荐
- switch(面试)
public static void Output() { ; ) { i--; switch (i) { : Console.WriteLine(i.ToString()); break; : Co ...
- Ubuntu下shell脚本运行异常:bash和dash的区别
Ubuntu下我用bash到语法写了一个shell脚本(准确的说是把书上的脚本敲进电脑),在ubuntu下,用sh test.sh来运行,但是出现了意料之外到结果,比如echo -e "\n ...
- 双机相关知识(原理、LVM、Raid技术)
1 双机知识 1.1 预备知识 1.1.1 基本概念 双机热备:双机热备双机管理软件可以根据心跳自动检测环境运行情况,如果发现一个节点挂掉了,会自动切换到另外一个 ...
- 【BootStrap】 基础
[BootStrap] 基础 一. 自适应(针对不同设备如手机平板笔电,使页面的宽度适应设备宽度) <meta name="viewport" content="w ...
- android APK应用安装过程以及默认安装路径[转]
一:安装过程 APK是类似Symbian Sis或Sisx的文件格式.通过将APK文件直接传到Android模拟器或Android手机中执行即可安装. Android应用安装有如下四种方式 1. ...
- Spring MVC常见的三种URL请求参数的处理
//方法1:@RequestParam //url:/test1?id=123 @RequestMapping(value = "/test1",method = RequestM ...
- eclipse Juno Indigo Helios Galileo 版本
Galileo Ganymede Europa 这些名字代表eclipse不同的版本 2001年11月7日 ,Eclipse 1.0发布 半年之后,2002年6月27日Eclipse进入了2.0时代 ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...
- CodeForces 742B Batch Sort
B. Batch Sort time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- JavaScript-setInterval-周期性行定时器-倒计时
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...