簡體   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