3 Replies - 3123 Views - Last Post: 20 November 2013 - 12:54 PM Rate Topic: -----

#1 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

searching for particular string in excel file

Posted 20 November 2013 - 08:29 AM

Hi,

I am searching for particular string in c#.
My code:

 private void button1_Click(object sender, EventArgs e)
        {

            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\MsgRed.xls");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;


            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;
            string Value1 = "Coordinator_K";
            //string Value1 = "abc";

            for (int i = 1; i < rowCount; i++)
            {
                for (int j = 1; j < colCount; j++)
                {
                    //MessageBox.Show(xlRange.Cells[i, j].Value2.ToString());

                    if (String.Compare(xlRange.Cells[i, j].Value2.ToString(), Value1))
                    MessageBox.Show("ok");
                    //else
                    //   MessageBox.Show("not ok");

                }
            }
        }


But I am getting some error that
cannot implicitly convert type int to bool

Thanks
John

Is This A Good Question/Topic? 0
  • +

Replies To: searching for particular string in excel file

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: searching for particular string in excel file

Posted 20 November 2013 - 08:40 AM

if statements require a boolean expression/value. Compare() returns an int. You need to do a comparison with the return value of Compare() to get a boolean expression.
Was This Post Helpful? 0
  • +
  • -

#3 vivek20055   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 82
  • Joined: 02-November 13

Re: searching for particular string in excel file

Posted 20 November 2013 - 12:38 PM

View PostSkydiver, on 20 November 2013 - 08:40 AM, said:

if statements require a boolean expression/value. Compare() returns an int. You need to do a comparison with the return value of Compare() to get a boolean expression.



Hi,

I tried like this in console application.

Still I am getting some error

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication27
{
    class Program
    {
        static void Main(string[] args)
        {
            Excel.Application xlApp = new Excel.Application();
            //Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"\\global.scd.scania.com\home\se\121\valhbc\Desktop\log files\Output\MsgRed.xls");
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\Vivek\Desktop\scanla\1511_Red_N1.xlsx");
            Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            string Value1 = "Coordinator_K";
            

            for (int i = 1; i < rowCount; i++)
            {
                for (int j = 1; j < colCount; j++)
                {
                    

                    if (Value1.Equals(xlRange.Cells[i, j].Value2.ToString()))
                    
                    Console.WriteLine("true");
                    

                }
            }
        }
    }
}



It is showing true only once
But I need to get multiples
I am attaching the error with this message


Thanks
John

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: searching for particular string in excel file

Posted 20 November 2013 - 12:54 PM

That means that some cells are null. You should check for nulls before you try to dereference them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1