.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 = 1; i <= sheet.UsedRange.Cells.Rows.Count; i++)
                    {
                        for (int j = 1; 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;
        }


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值