Jump to content

Fun with Excel

- - - - -

  • Please log in to reply
No replies to this topic

#1
Xystus777

Xystus777

    Learning Programmer

  • Members
  • PipPipPip
  • 59 posts
Okay everyone, this might be a tricky task, I'm not sure. If someone could help, that'd be wonderful! Here is my current code:

Excel.Application xlApp = new Excel.Application(); 

Excel.Workbook xlWB; 

Excel.Worksheet xlWS; 

 

object obj = new object(); 

 

private void viewBtn_Click(object sender, EventArgs e) 

        { 

            xlApp.Quit(); 

            xlWB = (Excel.Workbook)(xlApp.Workbooks.Add(workbookPath)); 

            xlWS = (Excel.Worksheet)xlWB.ActiveSheet; 

            obj = xlWS.get_Range("A2", "A120").Value2; 

                //get 120 items of A  

                int j = 0; 

                for (int i = 1; i < 121; i++) 

                { 

                    string str = ((object[,])obj)[i, 1].ToString(); 

                    //string str = obj[i-1,1] as string  

                    if (str.Equals(viewTNumberTextBox.Text)) 

                    { 

                        j = i + 1; 

                        //hold the index of the item in j  

                        break; 

                    } 

                } 

                if (j != 0) 

                { 

                    object po = xlWS.get_Range(xlApp.Cells[j, 2], xlApp.Cells[j, 2]).Value2; 

                    object io1 = xlWS.get_Range(xlApp.Cells[j, 3], xlApp.Cells[j,3]).Value2; 

                    object io2 = xlWS.get_Range(xlApp.Cells[j, 4], xlApp.Cells[j,4]).Value2; 

                    object no = xlWS.get_Range(xlApp.Cells[j, 5], xlApp.Cells[j, 5]).Value2; 

 

                    polarityLabel.Text = po.ToString(); 

                    interface1Label.Text = io1.ToString(); 

                    interface2Label.Text = io2.ToString(); 

                } 

Okay, what this program does is it will go through an "existing" excel document located at "workbookPath", and it will go through the first column looking for some text. The text that it is looking for is whatever the user types into a textbox, "viewTNumberTextBox". When it finds the text in Excel, it will then get the values of the cells to the right of the first column (up to 5 columns as you can see). Then it will take those values and put them into strings. Then finally, Those strings will end up as the text values for some different labels on my windows application.

Well, what I want to do...is something similar...

However, this excel document that i'm using is much larger than this one, has 15 columns, and about 120 rows. Except...the text that the user will type in, will be in the document more than once! Do you understand that part? For example...this excel document looks like this:
Posted Image

If you look in the third column, under "TUCKER T-Numbers" you'll see a list of "T-Numbers". These are what the user will be typing into the textbox control, which will be called "dceMatrixTextBox". Okay, so, if you look closely, you can see that all of the "T-Numbers" are in that column, more than once, and the values next to each are different. For example, let me narrow it down like this:

Posted Image

Okay, so as you can see, there are cases where there are more than one "T-Number". I want the user to be able to type in a T-Number, and then I want the program to read this excel document, find "ALL" instances of this T-Number, and then display it on the screen. I hope all of this explaining helps you help me. If you have any questions, just ask. Hopefully the pictures helped some too!

Thank you all!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users