none
How to select values of a specified column in excel file? RRS feed

  • Question

  • I have some excel files , and i have to choose a excel file and parse it by some validation.

    in the selected file i have to validate on column like named as "rate" and get values and store it in list for some validation. can any one give me suggestion and sample code for my problem. i going to code in c#.

    Thursday, December 22, 2016 5:46 AM

All replies

  • Hi,

    Please see the demo below to check if it suitable for your files.

    This is a winform application.

    Please add a textbox and a listbox firstly on the winform.

    Add reference: Microsoft.Office.Interop.Excel

    Add line: using Excel = Microsoft.Office.Interop.Excel; before the namespace.

     

    OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Excel files|*.xlsx|All files|*.*"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string fileName = openFileDialog1.FileName; Excel.Application xlApp = new Excel.Application(); xlApp.Visible = false; Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(fileName); Excel.Worksheet xlWorksheet = xlWorkBook.ActiveSheet; Excel.Range currentFind = null; Excel.Range oRng = xlWorksheet.UsedRange; currentFind = oRng.Find(textBox1.Text, Type.Missing, Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing); if (currentFind != null) { Excel.Range sRng = xlWorksheet.UsedRange.Columns[currentFind.Column]; listBox1.Items.Clear(); foreach (Excel.Range cell in sRng.Cells) { listBox1.Items.Add(cell.Value); } } else { MessageBox.Show("Not Found"); } xlWorkBook.Close(false); xlApp.Quit(); }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 23, 2016 5:00 AM
    Moderator