在Excel文档中搜索文本 [英] Search Text in Excel Document

查看:79
本文介绍了在Excel文档中搜索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的代码项目团队[全部],

我想搜索文本Excel表格。如果找到搜索文本,那么我想返回工作表和行位置。



谢谢

Dear Code Project Team[ALL],
I want to search text Excel Sheet. If search Text found then i want to return Sheet and Row position.

Thanks

推荐答案

嗨Arun ,



首先将excel表加载到数据集中并在数据集中查找文本。



使用下面的示例代码load excel to dataset:



Hi Arun,

first load excel sheet into dataset and find text in dataset.

use below sample code to load excel to dataset:

public void LoadallAttributions(string filePath)
        {
            IExcelDataReader excelReader = null;
            DataSet result = new DataSet();
            FileStream stream = null;
            try
            {
                stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
                if (String.Compare(Path.GetExtension(filePath), ".xls", true) == 0)
                    excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                else if (String.Compare(Path.GetExtension(filePath), ".xlsx", true) == 0)
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

                if (excelReader != null)
                {
                    excelReader.IsFirstRowAsColumnNames = true;
                    result = excelReader.AsDataSet();
                    dataset = result;

                }
            }
            catch (Exception ex)
            {
               // clsErrorLog.WriteErrorLog(ex);
                MessageBox.Show(Path.GetFileName(filePath).ToString() +" file opened out side the application, please close and try again.");

            }
            finally
            {
                //if (excelReader != null && !excelReader.IsClosed)
                //{
                    excelReader.Close();
                    excelReader.Dispose();
               // }
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();

                }
            }
           // return result;

           
        }


嗨Arun,



首先将Excel工作表加载到数据集中并在数据集中查找文本。



使用下面的示例代码将excel加载到数据集:



Hi Arun,

first load excel sheet into dataset and find text in dataset.

use below sample code to load excel to dataset:

IExcelDataReader excelReader = null;
      DataSet result = new DataSet();
      FileStream stream = null;
      try
      {
          stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
          if (String.Compare(Path.GetExtension(filePath), ".xls", true) == 0)
              excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
          else if (String.Compare(Path.GetExtension(filePath), ".xlsx", true) == 0)
              excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

          if (excelReader != null)
          {
              excelReader.IsFirstRowAsColumnNames = true;
              result = excelReader.AsDataSet();
              clsUtility.dsAttributionMap = result;

          }
      }
      catch (Exception ex)
      {
         // clsErrorLog.WriteErrorLog(ex);
          MessageBox.Show(Path.GetFileName(filePath).ToString() +" file opened out side the application, please close and try again.");

      }


浏览这些链接..

http://www.authorcode.com/search-text-in-excel-file-through-c/ [ ^ ]

http ://social.msdn.microsoft.com/forums/en-US/winforms/thread/6339a7e2-9b4e-4ba7-847a-3024ac2f30e8 [ ^ ]

http://msdn.microsoft.com/en-us/library/e4x1k99a%28v=vs.80%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-8 [ ^ ]
Go through these links..
http://www.authorcode.com/search-text-in-excel-file-through-c/[^]
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/6339a7e2-9b4e-4ba7-847a-3024ac2f30e8[^]
http://msdn.microsoft.com/en-us/library/e4x1k99a%28v=vs.80%29.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-8[^]


这篇关于在Excel文档中搜索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆