首页
/ LinqToExcel 项目技术文档

LinqToExcel 项目技术文档

2024-12-23 04:18:13作者:田桥桑Industrious

1. 安装指南

1.1 通过 NuGet 安装

你可以使用 NuGet 快速将 LinqToExcel 添加到你的项目中。只需在 NuGet 包管理器中搜索 linqtoexcel 并安装该包。

1.2 安装 Microsoft Access Database Engine

为了使用 LinqToExcel,你需要安装 Microsoft Microsoft Access Database Engine 2010 Redistributable。如果没有安装,你将会遇到以下异常:

The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

请注意,32 位和 64 位版本都有提供,选择与你项目设置匹配的版本。你一次只能安装其中一个版本。

2. 项目的使用说明

2.1 查询带有标题行的工作表

默认查询期望第一行是标题行,包含与泛型类属性名称匹配的列名。它还期望数据位于名为 "Sheet1" 的工作表中。

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.Worksheet<Company>()
                       where c.State == "IN"
                       select c;

2.2 查询特定名称的工作表

默认情况下,数据从名为 "Sheet1" 的工作表中查询。要查询不同名称的工作表,可以将工作表名称作为参数传递。

var excel = new ExcelQueryFactory("excelFileName");
var oldCompanies = from c in excel.Worksheet<Company>("US Companies") //worksheet name = 'US Companies'
                   where c.LaunchDate < new DateTime(1900, 1, 1)
                   select c;

2.3 属性到列的映射

可以使用 AddMapping() 方法将工作表中的列名映射到类中的特定属性名称。属性名称可以作为字符串或编译时安全的表达式传递。

var excel = new ExcelQueryFactory("excelFileName");
excel.AddMapping<Company>(x => x.State, "Providence"); //maps the "State" property to the "Providence" column
excel.AddMapping("Employees", "Employee Count");       //maps the "Employees" property to the "Employee Count" column

var indianaCompanies = from c in excel.Worksheet<Company>()
	               where c.State == "IN" && c.Employees > 500
	               select c;

2.4 使用 LinqToExcel.Row 类

查询结果可以作为 LinqToExcel.Row 对象返回,这允许你通过列名作为字符串索引来访问单元格的值。只需使用不带泛型参数的 Worksheet() 方法。

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.Worksheet()
                       where c["State"] == "IN" || c["Zip"] == "46550"
                       select c;

2.5 查询没有标题行的工作表

可以使用 WorksheetNoHeader() 方法查询没有标题行的工作表。单元格值通过索引引用。

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.WorksheetNoHeader()
                       where c[2] == "IN" //value in 3rd column
                       select c;

3. 项目 API 使用文档

3.1 查询命名范围

查询可以限定为仅包含命名范围内的数据。

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.NamedRange<Company>("NamedRange") //Selects data within the range named 'NamedRange'
                       where c.State == "IN"
                       select c;

3.2 查询特定范围

数据可以从工作表中的特定单元格范围查询。

var excel = new ExcelQueryFactory("excelFileName");
var indianaCompanies = from c in excel.WorksheetRange<Company>("B3", "G10") //Selects data within the B3 to G10 cell range
                       where c.State == "IN"
                       select c;

3.3 查询特定工作表索引

可以通过工作表的索引查询特定工作表。

var excel = new ExcelQueryFactory("excelFileName");
var oldCompanies = from c in excel.Worksheet<Company>(1) //Queries the second worksheet in alphabetical order
                   where c.LaunchDate < new DateTime(1900, 1, 1)
                   select c;

3.4 应用转换

可以在将单元格值设置到类属性之前应用转换。

var excel = new ExcelQueryFactory("excelFileName");
excel.AddTransformation<Company>(x => x.IsBankrupt, cellValue => cellValue == "Y");

var bankruptCompanies = from c in excel.Worksheet<Company>()
                        where c.IsBankrupt == true
                        select c;

4. 项目安装方式

4.1 通过 NuGet 安装

使用 NuGet 包管理器搜索并安装 linqtoexcel 包。

4.2 安装 Microsoft Access Database Engine

下载并安装与项目设置匹配的 Microsoft Access Database Engine 2010 Redistributable。

登录后查看全文
热门项目推荐
相关项目推荐