简体   繁体   English

Linq to Excel-仅从sheet1的最后一行接收数据

[英]Linq to Excel - only recieving data from last row in sheet1

After the headache of attempting to query an Excel spreadsheet with ADO.NET, I've decided to try Linq to Excel in my project. 在尝试使用ADO.NET查询Excel电子表格后,我决定在我的项目中尝试使用Linq to Excel

I have created a method that is supposed to return the value of column B when I provided it with the corresponding value of column A (Both in the same row). 我创建了一个方法,当我向它提供列A的对应值(均在同一行)时,该方法应返回列B的值。 Sheet1 has a header row or at least a row indicating what Columns A and B are. Sheet1具有标题行或至少一行指示A和B列的行。

When I combine my code with a basic linq query to retrieve data, I only get data from the last row, no matter what value in column AI ask for. 当我将代码与基本的linq查询结合起来以检索数据时,无论AI列中要求什么值,我都只能从最后一行获取数据。 It's alway the last row. 总是最后一行。 There are something like 1159 rows in my sheet. 我的工作表中有大约1159行。 The spreadsheet is Excel 2010, so the data engine should be ACE I presume. 电子表格为Excel 2010,因此数据引擎应为ACEI。

Here is my code... 这是我的代码...

    public static string ReturnDefinition(string remCode)
    {
        string strReturnMessage = "";
        string pathToExcelFile = @"MEDICARE.xlsx";
        string sheetName = "sheet1";

        var excelFile = new ExcelQueryFactory(pathToExcelFile);
        var codeDescriptions = from a in excelFile.Worksheet(sheetName) select a;

        try
        {
            foreach (var a in codeDescriptions)
            {
                if (a["Code"].ToString().Contains(remCode))
                {
                    strReturnMessage = a["Description"].ToString();
                }
                else
                {
                    strReturnMessage = "No Data";
                }
            }
            return strReturnMessage;

        }
        catch (Exception ex)
        {
            return "Error: " + ex.Message;
        }
        finally
        {

        }
    }

Any Ideas? 有任何想法吗?

Update 更新

It seems that I am not returning the result when it is found. 似乎找到结果后我没有返回结果。 This has something to do with not breaking out of the loop when the result is found. 这与发现结果时不中断循环有关。 The loop continues to the last result each time. 每次循环都会循环到最后一个结果。

I would like to recommand another usefull package: 我想推荐另一个有用的软件包:

http://nugetmusthaves.com/Package/EPPlus http://nugetmusthaves.com/Package/EPPlus

As for me the best one. 至于我最好的。 Inthe case ou yours i have no expirience. 如果您的情况我没有经验。

When you find what you are looking for you should return the value straight-away, otherwise it will just keep looping, and the value will either be the last one it finds or 'No Data': 找到要查找的内容后,应立即返回该值,否则它将不断循环,该值将是找到的最后一个值或为“无数据”:

strReturnMessage = a["Description"].ToString();
return strReturnMessage;

You don't need the else; 您不需要其他的东西。 only if the loop completes without finding the value would you return "No Data": 只有在循环完成而找不到值的情况下,您才返回“无数据”:

        strReturnMessage = a["Description"].ToString();
        return strReturnMessage;
    } // end of if
} // end of loop

return "No Data";

If you want the LAST Description, where Code contains the search-text, then: 如果需要LAST Description(其中Code包含搜索文本),则:

        strReturnMessage = a["Description"].ToString();
    } // end of if
} // end of loop

if (strReturnMessage == "") {
    return "No Data";
} else {
    return strReturnMessage;
}

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

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