使用LINQ语法查询Excel的.NET库:LinqToExcel
Linq to Excel 是一个 .Net 库能够让你使用LINQ语法来查询Excel电子表格。
Adding LinqToExcel to your project
NuGet
You can use NuGet to quickly add LinqToExcel to your project. Just search for linqtoexcel and install the package.
Manually Add References
If you don't want to use the NuGet package you can Download the latest files and add the following references to your project
* LinqToExcel.dll
* Remotion.Data.Linq.dll
x64 Support
If you want LinqToExcel to run in a 64 bit application, make sure to use the 64 bit version of the library.
You will also need to make sure to have the 64 bit version of the Access Database Engine installed on the computer.
Query a worksheet with a header row
The default query expects the first row to be the header row containing column names that match the property names on the generic class being used. It also expects the data to be in the worksheet named "Sheet1".
var excel = new ExcelQueryFactory("excelFileName"); var indianaCompanies = from c in excel.Worksheet<Company>() where c.State == "IN" select c;
Query a specific worksheet by name
Data from the worksheet named "Sheet1" is queried by default. To query a worksheet with a different name, pass the worksheet name in as an argument.
var excel = new ExcelQueryFactory("excelFileName"); var oldCompanies = from c in repo.Worksheet<Company>("US Companies") //worksheet name = 'US Companies' where c.LaunchDate < new DateTime(1900, 1, 1) select c;