Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 5-Jun-13 21:49pm
v2
Comments
ridoy 6-Jun-13 3:29am    
what have you tried?

Hi Arun,

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

use below sample code to load excel to dataset:

C#
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;

           
        }
 
Share this answer
 
v2
Comments
Arun Vasu 6-Jun-13 3:34am    
Thanks for your help.
My problem is, i have huge number of Excel file. in that i want to search that text in which excel file, in which sheet, and which row that text search is found.
Hi Arun,

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

use below sample code to load excel to dataset:

C#
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.");

      }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900