注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯倦客的博客

祝福你朋友永远快乐!

 
 
 

日志

 
 

.NET读取Office文件内容(word、excel、ppt)  

2014-07-04 13:10:42|  分类: C# |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

引用命名空间

1 using Microsoft.Office.Core;
2 using Word = Microsoft.Office.Interop.Word;
3 using Excel = Microsoft.Office.Interop.Excel;
4 using PowerPoint = Microsoft.Office.Interop.PowerPoint;

Word文件的读取

复制代码
 1  public string ReadFile()
 2         {
 3             string text = string.Empty;
 4             Word.ApplicationClass app = null;
 5             Word.Document doc = null;
 6             object readOnly = true;
 7             object missing = System.Reflection.Missing.Value;
 8             object fileName = this.FileInstance.FullName;
 9             try
10             {
11                 app = new Microsoft.Office.Interop.Word.ApplicationClass();
12                 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);
13                 text = doc.Content.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty);
14             }
15             catch
16             {
17                 
18             }
19             finally
20             {
21                 doc.Close(ref missing, ref missing, ref missing);
22                 doc = null;
23                 app.Quit(ref missing, ref missing, ref missing);
24                 app = null;
25             }
26             return text;
27         }
复制代码

Excel文件的读取

复制代码
 1 public string ReadFile()
 2         {
 3             string text = string.Empty;
 4             Excel.ApplicationClass app = null;
 5             Excel.Workbook book = null;
 6             object readOnly = true;
 7             object missing = System.Reflection.Missing.Value;
 8             object fileName = this.FileInstance.FullName;
 9             try
10             {
11                 app = new Microsoft.Office.Interop.Excel.ApplicationClass();
12                 book = app.Workbooks.Open(fileName.ToString(), missing, readOnly, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
13                 foreach (Excel.Worksheet sheet in book.Sheets)
14                 {
15                     for (int i = 1; i <= sheet.UsedRange.Cells.Rows.Count; i++)
16                     {
17                         for (int j = 1; j <= sheet.UsedRange.Cells.Columns.Count; j++)
18                         {
19                             text += ((Excel.Range)sheet.Cells[i, j]).Text.ToString().Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty) + " ";
20                         }
21                     }
22                 }
23             }
24             catch
25             {
26 
27             }
28             finally
29             {
30                 book.Close(missing, fileName, missing);
31                 book = null;
32                 app.Quit();
33                 app = null;
34             }
35             return text;
36         }
复制代码

PPT文件的读取

复制代码
 1  public override string ReadFile()
 2         {
 3             string text = string.Empty;
 4             PowerPoint.ApplicationClass app = null;
 5             PowerPoint.Presentation pp = null;
 6             object readOnly = true;
 7             object missing = System.Reflection.Missing.Value;
 8             object fileName = this.FileInstance.FullName;
 9 
10             try
11             {
12                 app = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
13                 pp = app.Presentations.Open(fileName.ToString(), Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
14 
15                 foreach (PowerPoint.Slide slide in pp.Slides)
16                 {
17                     foreach (PowerPoint.Shape shape in slide.Shapes)
18                     {
19                         text += shape.TextFrame.TextRange.Text.Replace("\r", string.Empty).Replace("\n", string.Empty).Replace("\t", string.Empty) + " ";
20                     }
21                 }               
22             }
23             catch
24             {
25 
26             }
27             finally
28             {
29                 pp.Close();
30                 pp = null;
31                 app.Quit();
32                 app = null;
33             }
34 
35             return text;
36         }
复制代码
  评论这张
 
阅读(431)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017