zoukankan      html  css  js  c++  java
  • c#获取txt,word,excel文档内容方法

      

         获取txt文档的内容 

     1 public string ResumeTxt(string path)
     2 {
     3    string str = string.Empty;
     4        
     5    StreamReader reader = new StreamReader(path, System.Text.Encoding.Default);
     6    str = reader.ReadToEnd();
     7  
     8    //再通过查询解析出来的的字符串有没有GB2312 的字段,来判断是否是GB2312格式的,如果是,则重新以GB2312的格式解析
     9    Regex reGB = new Regex("GB2312", RegexOptions.IgnoreCase);
    10    Match mcGB = reGB.Match(str);
    11    if (mcGB.Success)
    12    {
    13        StreamReader reader2 = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
    14        str = reader2.ReadToEnd();
    15    }
    16  
    17    return str;
    18 19 

        获取word文档的内容

     

     1 private string ResumeWord(string path)
     2 {
     3    string str = string.Empty;
     4    Document myWordDoc; 
     5    Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
     6  
     7    object filepath = path;
     8    object oMissing = Missing.Value;
     9   
    10    myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    11          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    12          ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    13    str = myWordDoc.Content.Text;
    14  
    15    return str;
    16 

        获取Excel文档的内容
     

     1 private string ResumeExcel(string path)
     2 {
     3    string str = string.Empty;
     4    //创建Application对象
     5     Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
     6    xApp.Visible = false;
     7  
     8  
     9    //得到WorkBook对象,
    10    Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks._Open(path,
    11          Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    12          Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    13          Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    14  
    15    //指定要操作的Sheet:
    16    Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
    17  
    18    //读取,通过Range对象,但使用不同的接口得到Range
    19    for (int i = 1; i <= 100; i++)
    20    {
    21        for (int j = 1; j <= 100; j++)
    22        {
    23            Microsoft.Office.Interop.Excel.Range rng = (Microsoft.Office.Interop.Excel.Range)xSheet.Cells[i, j];
    24            if (rng.Value2 != null)
    25            {
    26                str += rng.Value2.ToString();
    27            }
    28        }
    29     }
    30  
    31      return str;
    32 }

     

  • 相关阅读:
    java DMO及增删改查代码的自动生成
    如果有一天苹果免费了,支付宝怎么办
    jquery实现页面交互的几个小例子
    android中的所谓观察者模式
    随笔——runnable勘误以及其他
    android 源码角度全方位理解filter
    android 你的onfocuschangelistener和android:state_hovered为何不起作用
    android 如何阻断seekbar的触摸事件
    java 传值和传引用
    Android内存优化(使用SparseArray和ArrayMap代替HashMap)
  • 原文地址:https://www.cnblogs.com/sishierfei/p/1610612.html
Copyright © 2011-2022 走看看