.NET读取Office文件内容(word、excel、ppt)_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > .NET读取Office文件内容(word、excel、ppt)

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

 2014/4/29 10:27:10  M守护神  博客园  我要评论(0)
  • 摘要:引用命名空间1usingMicrosoft.Office.Core;2usingWord=Microsoft.Office.Interop.Word;3usingExcel=Microsoft.Office.Interop.Excel;4usingPowerPoint=Microsoft.Office.Interop.PowerPoint;Word文件的读取1publicstringReadFile()2{3stringtext=string.Empty;4Word
  • 标签:ppt .net excel 文件 net 内容 Office

引用命名空间

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 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.Rows.Count; i++)
16                     {
17                         for (int j = 1; j <= sheet.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         }
发表评论
用户名: 匿名