国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

.NET讀取Office文件內(nèi)容(word、excel、ppt)

2019-11-17 03:11:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

.NET讀取Office文件內(nèi)容(WordExcelPPT

引用命名空間

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             try10             {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             catch16             {17                 18             }19             finally20             {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             try10             {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             catch25             {26 27             }28             finally29             {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             try11             {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             catch24             {25 26             }27             finally28             {29                 pp.Close();30                 pp = null;31                 app.Quit();32                 app = null;33             }34 35             return text;36         }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝丰县| 桃江县| 丁青县| 桐柏县| 永顺县| 台南县| 大港区| 紫阳县| 光泽县| 富宁县| 银川市| 突泉县| 双鸭山市| 布拖县| 察隅县| 吕梁市| 繁峙县| 珲春市| 武穴市| 无为县| 石景山区| 原平市| 桑植县| 宜良县| 庆元县| 千阳县| 建瓯市| 黔西县| 安平县| 长岛县| 丰宁| 襄汾县| 阿克陶县| 南涧| 和政县| 江永县| 天柱县| 六枝特区| 博白县| 行唐县| 调兵山市|