简体   繁体   English

如何从C#中获取Excel中特定列中包含数据的所有行

[英]How to get all the rows which contain data in a particular column in Excel from C#

I have a data set in Excel and am using C# to open the worksheet and access some of the data. 我在Excel中有一个数据集,并且正在使用C#打开工作表并访问一些数据。

I am trying to get all the rows that contain data from a particular column. 我试图获取包含来自特定列的数据的所有行。 For example in column B starting from cell 'B3' going down I want to store all the rows that contain data in a collection like an Array. 例如在B列中,从单元格“ B3”开始向下,我想将包含数据的所有行存储在Array之类的集合中。

This is what I have so far: 这是我到目前为止的内容:

Application excelApplication;
_Workbook workbook;
_Worksheet sheet;

excelApplication = new Excel.Application
  {
    Visible = true,
    ScreenUpdating = true
  };

workbook = excelApplication.Workbooks.Open(@"C:\Documents and Settings\user\Desktop\Book1.xls");
sheet = (Worksheet)workbook.Worksheets[2];

Excel.Range range = sheet.Range["b3:b145"].

foreach (Range cell in range)
   {

      // Do something with rows which contain data          
   }

As you can see above I have specified the range from B3 to B45 which I don't want. 如您在上面看到的,我指定了我不想要的从B3到B45的范围。 I want to get all the rows in the B column which contain data starting from B3. 我想获取B列中包含从B3开始的数据的所有行。

How would I achieve this? 我将如何实现?

In general when I get stuck in these situations I record a Macro and convert the VBA code to C#. 通常,当我陷入这些情况时,我会记录一个宏并将VBA代码转换为C#。 The object model in VSTO is pretty much exactly the same (remember this its a great tip) and with .Net 4.0 onwards optional parameters save a lot of code. VSTO中的对象模型几乎完全相同(请记住,这是一个很好的技巧),并且从.Net 4.0开始,可选参数节省了大量代码。

In your particular instance I envisage the larger the spreadsheet the longer it will take to read all the Excel cells in column B using VSTO. 在您的特定情况下,我设想电子表格越大,使用VSTO读取B列中的所有Excel单元格所需的时间就越长。 My advice is to use this technique to read them all at once: 我的建议是使用此技术一次读取所有内容:

//Work out the number of rows with data in column B:
//int lastColumn = range.Columns.Count;
int lastRow = range.Rows.Count;

//Get all the column values:
object[,] objectArray = shtName.get_Range("B3:B" + lastRow.ToString()).Value2;
rngName.Value2 = objectArray;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Excel C#中的数据行中查找最小列数 - Finding minimum column count from data rows in Excel C# 如何从Excel中获取唯一的列数据,该数据在C#的数据库表列中 - how to fetch the only column data from excel which is there in database table column in c# 如何使用 C# 将选定列的所有行从一个 excel 文件复制到另一个文件 - How to copy all the rows of selected column from one excel file to another using C# 如何获取Excel工作表的第一行列数据的集合(使用C#) - How to get the collection of Excel Sheet's first rows column data (using C#) 如何将数据从Excel转换为C#? - How to get the data from Excel into C#? 如何从散列集中获取c#中特定子类的所有元素? - How to get from a hashset all the elements that are of a particular subclass in c#? 如何从Excel中删除除C#中的标头之外的所有行 - How to delete all rows from excel except header in c# 使用 C#,如果“x”在第 2 列中,如何从 smartsheet 获取所有行? - Using C#, how do I get all rows from smartsheet if “x” is in column 2? 如何使用 OleDbDataAdapter 在 c# 中获取所有 excel 工作表数据 - how to get all excel sheet data in c# using OleDbDataAdapter 使用C#在excel中将一列的所有行创建为下拉列表 - Create all the rows of a column as a dropdownlist in excel using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM