[英]Get row from Excel file in C#
I am reading through an Excel file in C#. 我正在阅读C#中的Excel文件。 There are 3 sheets in the file:
文件中有3张:
I am looping through the columns of Summary sheet.(code below) 我正在浏览摘要表的列。(代码如下)
There is a column: SummaryID
in every sheet. 每列中都有一列:
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
}
}
My question is: for everyrow in Summary Sheet (SummaryID), I want to get all matching rows that match the SummaryID in 'Users' and 'Others' sheets . 我的问题是: 对于摘要表(SummaryID)中的每一行,我想获得与“用户”和“其他”工作表中的SummaryID匹配的所有匹配行 。
Note: The column SummaryID
exists in all 3 sheets and is the first column in all sheets. 注意:列
SummaryID
存在于所有3个工作表中,并且是所有工作表中的第一列。
我喜欢使用LinqToExcel他们有一个可能对你有帮助的LinqToExcel.Row类,你将使用linq而不是foreach语句。
Have you considered treating your Excel sheet like a Database and querying out the data you want using OLEDB
or a similar technology? 您是否考虑将Excel表格视为数据库并使用
OLEDB
或类似技术查询所需数据?
This would be a simple Join
query at that point - Might be faster... 这将是一个简单的
Join
查询 - 可能会更快......
You can use OleDB to do this. 您可以使用OleDB来执行此操作。 Code is similiar
代码类似
// 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.