如何在excel文件中选择指定列的值? [英] How to select values of a specified column in excel file?

查看:126
本文介绍了如何在excel文件中选择指定列的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些excel文件,我必须选择一个excel文件并通过一些验证解析它。

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

在所选文件中我必须在列上验证,如命名作为"费率"并获取值并将其存储在列表中以进行某些验证。可以任何人给我建议和示例代码我的问题。我将用c#编码。

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#.

推荐答案

Hi,

请参阅下面的演示,检查它是否适合您的文件。

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

这是一个winform应用程序。

This is a winform application.

请先在winform上添加一个文本框和一个列表框。

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

添加引用:Microsoft.Office.Interop.Excel

Add reference: Microsoft.Office.Interop.Excel

添加行:使用Excel = 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(); }


这篇关于如何在excel文件中选择指定列的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆