2

我能够使用下面的 LinqtoExcel 映射概念从 excel 文件中解析大量数据。

我的要求: 当电子表格中的特定单元格带有 DataLists 选项时,如何进行 LinqtoExcel 映射。是否可以为映射场景做如下图所示

excel.AddMapping<RESULT_DBML>(x => x.Period_Name, "Period_Name" OR "Prd_Date");

在此处输入图像描述

我的 C# 代码:

class Reference
{
    public string Period_Name { get; set; }
    public string Start_Time { get; set; }
    public string End_Time { get; set; }
}

private static bool import_excelres(string _path)
    {
        bool parse_status = false;
        List<string> files = System.IO.Directory.GetFiles(@_path, "*.xlsx", SearchOption.AllDirectories).ToList<string>();

        DataClasses1DataContext dbContext = new DataClasses1DataContext();
        ExcelQueryFactory excel = new ExcelQueryFactory();

        excel.FileName = files.ElementAt(0);


        // Mapping Excel Columns to Database Table Results with Table Column Names
        excel.AddMapping<RESULT_DBML>(x => x.Period_Name, "Period_Name");
        excel.AddMapping<RESULT_DBML>(x => x.Start_Time, "Start_Time");
        excel.AddMapping<RESULT_DBML>(x => x.End_Time, "End_Time");

        var results = (from x in excel.Worksheet<Reference>("Sheet1")
                          select new RESULT_DBML
                          {
                              Period_Name = x.Period_Name,
                              Start_Time = x.Start_Time,
                              End_Time = x.End_Time,

                          });

        dbContext.RESULT_DBMLs.InsertAllOnSubmit(results);
        dbContext.SubmitChanges();
        return parse_status;
    }
4

0 回答 0