【发布时间】:2015-03-03 02:38:49
【问题描述】:
我有一个 xls 格式的 excel 表,我正在使用 LinqToExcel 读取它,然后将其导入我的数据库。
此表由大约 3K 行和仅 6 列组成。我正在使用 .addmapping 将我的类属性映射到列名
我遇到的问题是:“web-code”列的单元格有时会返回为空,尽管单元格中有数据。
这是一个以空值形式出现的示例数据!
我的代码手表
这是一个数据正确的示例数据:
我的代码手表
我曾尝试应用 ExcelColumn 属性进行映射,但没有成功!
代码:
var factory = new ExcelQueryFactory(_excelFilePath);
factory.AddMapping<ExcelPriceEntity>(x => x.WebCode, "WEB-CODE");
factory.AddMapping<ExcelPriceEntity>(x => x.Type, "TYPE");
factory.AddMapping<ExcelPriceEntity>(x => x.Style, "STYLE");
factory.AddMapping<ExcelPriceEntity>(x => x.Qty, "QTY");
factory.AddMapping<ExcelPriceEntity>(x => x.UnitPrice, "Unit Price");
factory.AddMapping<ExcelPriceEntity>(x => x.Bucket, "WEBCODE W/BUCKET");
factory.StrictMapping = StrictMappingType.ClassStrict;
factory.TrimSpaces = TrimSpacesType.Both;
factory.ReadOnly = true;
var prices = factory.Worksheet<ExcelPriceEntity>(_allPricesSheetName).ToList();
var priccerNP = prices.Where(p => p.Type.Contains("900 ARROW TAPE")).ToList();
我的 PriceEntity 类:
public class ExcelPriceEntity
{
//[ExcelColumn("TYPE")]
public string Type { get; set; }
public string WebCode { get; set; }
//[ExcelColumn("STYLE")]
public string Style { get; set; }
//[ExcelColumn("QTY")]
public string Qty { get; set; }
//[ExcelColumn("Unit Price")]
public string UnitPrice { get; set; }
//[ExcelColumn("WEBCODE W/BUCKET")]
public string Bucket { get; set; }
}
【问题讨论】:
-
如果可能,请以文本形式复制代码
-
除了以文本形式显示代码外,您还需要向我们展示一些返回商品结果的行和一些返回错误的行。仅仅说““网络代码”列的单元格有时会返回为空是不够的。你需要给我们看。
-
使用代码中返回对象的类型字段在excel图片中查找。我现在在火车上。
-
@Engimativity 在我的问题中,您有两个 excel 图像,第一个是网络代码即将到来的集合为空。 excel 的第二个图像是填充网络代码列的位置。题里说的很清楚,再看一遍。
-
您确定 ExcelPriceEntity 类中 WebCode 的类型是字符串吗?
标签: c# linq import-from-excel linq-to-excel