LinqToexcel仅在标题位于第一行时才有效。 如何通过标题名称而不是第一行读取。 任何人都可以帮忙吗?
答案 0 :(得分:0)
查询列名
GetColumnNames()方法可用于检索工作表中的列名列表。
var excel = new ExcelQueryFactory("excelFileName");
var columnNames = excel.GetColumnNames("worksheetName");
列映射的属性
可以使用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;
可以使用类的属性上的ExcelColumn属性来交替映射列名。
public class Company
{
[ExcelColumn("Company Title")] //maps the "Name" property to the "Company Title" column
public string Name { get; set; }
[ExcelColumn("Providence")] //maps the "State" property to the "Providence" column
public string State { get; set; }
[ExcelColumn("Employee Count")] //maps the "Employees" property to the "Employee Count" column
public string Employees { get; set; }
}