Click here to Skip to main content
15,888,100 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
See more:
Hi friends,
I want check all data present in excel sheet ,if any of cell contain any data (except 0 amount) I want display message "this cell contain except 0 amount please verify" like that please help me on that
Posted
Comments
Richard MacCutchan 10-Mar-15 4:32am    
Then you need to read the worksheet and iterate through the cells to check them. Where are you stuck?
Member 10562086 10-Mar-15 4:47am    
I am going to write code but I don't know how to wright code for this condition ..which logic and how to implement ??
If you get the cells, then get the value and compare that with 0. That is the logic.

I have written a sample example. Hope this helps. In this example it is checking for data only in column B. You can do the same for whatever columns you require.
C#
void CheckExcel() 
        {

            string filePath = @"F:\Book2.xlsx";

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;

            try
            {
                xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 0);

                Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet1"];

                int sheet1LastRowCount = worksheet1.UsedRange.Rows.Count;

                for (int i = 2; i <=sheet1LastRowCount; i++)
                {
                        if (Convert.ToInt32(worksheet1.Range["B" + i, "B" + i].Value2) == 0)
                        {
                            Console.WriteLine("Value is equal to 0 in row "+i);
                        }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                xlApp.Quit();
            }


        }
 
Share this answer
 
v2
Comments
Member 10562086 10-Mar-15 5:17am    
thanks subramanym .suppose If I want search all data of ( all Column and all rows of excel sheet) .what type of condition I have put .please tell me
For all columns and all rows. Please accept the answer if it's correct

C#
try
           {
               xlWorkBook = xlApp.Workbooks.Open(filePath, 0, true, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, false, 0, true, 0);

               Microsoft.Office.Interop.Excel.Worksheet worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlApp.Worksheets["Sheet1"];

               int sheet1LastRowCount = worksheet1.UsedRange.Rows.Count;
               int _val=0;

               for (int i = 2; i <=sheet1LastRowCount; i++)
               {
                   for(int j=1;j<=worksheet1.UsedRange.Columns.Count;j++)
                   {
                       if( Int32.TryParse(Convert.ToString(worksheet1.Cells[i,j].Value2),out _val))
                       {
                           if (Convert.ToInt32(_val) == 0)
                           {
                               Console.WriteLine("Value is equal to 0 in row " + i + " and column " + j);
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }
           finally
           {
               xlApp.Quit();
           }
 
Share this answer
 
v2
Comments
Member 10562086 10-Mar-15 9:46am    
can I use same code for Coded UI?? Is it work? I have to implement by using coded ui test case .
Subramanyam Shankar 10-Mar-15 11:09am    
The logic will work the same. it will work for UI also. If you face some issues,We are here to help you.
Member 10562086 11-Mar-15 6:09am    
thank you Shankar.i have one question ,why you initialize i=2 and j=1 ,which is row and which is column .suppose I want check data from particular row range(10th row to 25 th row )and particular column range (column "E" to "G" column )on this condition how to check
Subramanyam Shankar 11-Mar-15 7:05am    
i represents row and j represents the columns in your excel.i=1 because your row will start from 2 as the first row will have header information. j=1 because you need to start with first column "A". if you want to check data from 10th to 25th row then you need to replace the condition with for(i=10;i<=25;i++) and for(j=5;j<=8;j++).
Member 10562086 12-Mar-15 10:30am    
hello sir I need one help. I want display meaasge inside message box ,message is like "this sheet have non zero amount in the pariculor(row no) and and pariculor ( column no) please check it"
I am comparing pariculor cell value with "0" by the method Assert.AreEqual("0", values); and once it catch it will go to catch and display message like what I mention in above and my test case should be fail .
but what ever I declare row and column variable ,how to use inside the catch and how to display that row and column in the meaasge box .bellow code is there

try
{
int row;
int column;
for (Row = 22; Row <=23; Row++)
{
for (Colm = 5; Colm <= 18; Colm++)
{
excelProperties.excelCell.Set_CellIndices(Row, Colm);
string values = excelProperties.excelCell.Get_CellValue();

Assert.AreEqual("0", values);
Playback.Wait(500);
}

}

MessageBox.Show("There is no zero data in revenue input sheet");

}
catch (Exception ex)
{

MessageBox.Show("This sheet contains non zero amount, please check it in row " + Row + " and column " + Colm);
Assert.IsNull(ex, ex.Message);
}
}

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