繁体   English   中英

从C#中的Excel文件中获取行

[英]Get row from Excel file in C#

我正在阅读C#中的Excel文件。 文件中有3张:

  1. 摘要
  2. 用户
  3. 其他

我正在浏览摘要表的列。(代码如下)

每列中都有一列: SummaryID

foreach (DataColumn dc in Summary.Columns)
 {
   foreach (DataRow dr in Summary.AsEnumerable())
   {
     //get column SummaryID for everyrow
     //And then get all rows in Users sheet that match  SummaryID
     //And then get all rows in Others sheet that match SummaryID
   }
  }

我的问题是: 对于摘要表(SummaryID)中的每一行,我想获得与“用户”和“其他”工作表中的SummaryID匹配的所有匹配行

注意:列SummaryID存在于所有3个工作表中,并且是所有工作表中的第一列。

我喜欢使用LinqToExcel他们有一个可能对你有帮助的LinqToExcel.Row类,你将使用linq而不是foreach语句。

您是否考虑将Excel表格视为数据库并使用OLEDB或类似技术查询所需数据?

这将是一个简单的Join查询 - 可能会更快......

您可以使用OleDB来执行此操作。 代码类似

 // create a connection to your excel file
    // the 8.0 denotes excel 2003
    // you will need to use the right number for your version of excel
    OleDbConnection con = new OleDbConnection( @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=InsertYourFile.xls"; Extended Properties=Excel 8.0" );

    // create a new blank datatableDataTable 
   dtSheets = new DataTable();

    // create a data adapter to select everything from the worksheet you want
    OleDbDataAdapter da = new OleDbDataAdapter( "select * from [YourWorksheetName$] WHERE BLAH, etc", con );


   // use the data adapter to fill the datatable
   da.Fill( dtSheets );

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM