使用顺序位置引用列LinqToExcel [英] Referencing columns in LinqToExcel using ordinal position

查看:203
本文介绍了使用顺序位置引用列LinqToExcel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在有一个Excel上传,其一两种不同的模板不寻常的立场,即是对人友好的,另一种是机器生成。这样,列数据标头是不同的,所以很难将它们对齐。

I am in the unusual position of having two different templates for an Excel upload, one that is 'human friendly' and one that is machine generated. As such, the column data headers are different and it is difficult to align them.

因此​​,我想引用使用序号位置的列映射,而不是列的名称。目前,我有这样的:

As such, I would like to reference the column mappings using the ordinal position rather than the 'name' of the column. Currently I have this:

var excel = new ExcelQueryFactory(excelFileName);
excel.AddMapping<Student>(x => x.FirstName, "First Name");
excel.AddMapping<Student>(x => x.LastName, "Last Name");
excel.AddMapping<Student>(x => x.LastFour, "Last 4 Student ID");
excel.AddMapping<Student>(x => x.LastDate, "Last Date");

和我愿做这样的事情:

var excel = new ExcelQueryFactory(excelFileName);
excel.AddMapping<Student>(x => x.FirstName, "A");
excel.AddMapping<Student>(x => x.LastName, "C");
excel.AddMapping<Student>(x => x.LastFour, "G");
excel.AddMapping<Student>(x => x.LastDate, "H");

其中字母是在Excel中列引用。

where the letters are the column references in Excel.

有没有办法做到这一点?

Is there a way to do this?

推荐答案

如果您订购的是你可以调用相同

if you order is the same you can call

        var columnnames= excel.GetColumnNames("worksheetName");
        excel.AddMapping<Student>(x => x.FirstName, columnnames[0]);
        excel.AddMapping<Student>(x => x.FirstName, columnnames[1]);
        excel.AddMapping<Student>(x => x.LastFour, columnnames[2]);

这篇关于使用顺序位置引用列LinqToExcel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆