当数据库属性不匹配excel ASp.NET / core中的第一列时,将数据从Excel导入到现有数据表中

图片为excel文件第一列的图片(不是索赔号,其列K):

在这里输入图像说明

我的项目假设从Excel和XML文件导入数据,XML部分完成,但Excel的问题是,我将导入的文件中的第一列(意思是属性)不匹配现有的数据表我有

我有一部分属性:

public string ClaimNo { get; set; } public string ClientName { get; set; } public string Uwyear { get; set; } public string AgreementNo { get; set; } public string BusinessType { get; set; } public DateTime? PeriodStart { get; set; } public DateTime? PeriodEnd { get; set; } public string PolicyNo { get; set; } public string PolicyName { get; set; } public DateTime? DateOfLoss { get; set; } public string ClaimantName { get; set; } public string ClaimedInsured { get; set; } public DateTime? ReportDate { get; set; } 

我正在研究asp.net核心,已经写了一个从数据库导出数据到excel文件的代码,但它没有那么多的属性,像那些我试图导入数据的excel文件,第一列被编码。 我使用了EEPlus.Core。

我已经写了导入xls文件的函数

  [HttpPost] public async Task<IActionResult> XLSPage(IFormFile xlsFile) { var uploadsRoot = hostingEnvironment.WebRootPath; var filePath = Path.Combine(uploadsRoot, xlsFile.FileName).ToString(); if (xlsFile.ContentType.Equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) { try { using (var fileStream = new FileStream(filePath, FileMode.Create)) { await xlsFile.CopyToAsync(fileStream); fileStream.Dispose(); var package = new ExcelPackage(new FileInfo(filePath)); ExcelWorksheet workSheet = package.Workbook.Worksheets.First(); //The code that i need to read data from Excel //and fill in it into my dataTable } } catch (Exception e) { var str = e.Message; TempData["XlsConvertErrorMsg"] = "Converting fail, check if your data is correct"; return View(); } } else { TempData["XlsUploadErrorMsg"] = "Uploadin fail, check if your file is xls"; return View(); } TempData["XlsUploadConvertSuccesMsg"] = "Uploading, converting of Excel succeeded!"; return View(nameof(ImportExportXlsController.Index)); ; } 

好,所以索赔号是在XLSX文件中的K列和XML文件中的第1列上。

这里是从Excel中读取数据并填充dataTable,然后更改列索引的代码:

 //Get the values in the sheet object[,] valueArray = workSheet.Cells.GetValue<object[,]>(); int maxRows = workSheet.Dimension.End.Row; int maxColumns = workSheet.Dimension.End.Column; DataTable dt = new DataTable(); //Column Headers for (int col = 0; col < maxColumns; col++) { dt.Columns.Add(valueArray[0,col]); } //Import Excel Data ot DataTable for (int row = 1; row < maxRows; row++) { DataRow dr = dt.NewRow(); for (int col = 0; col < maxColumns; col++) { dr[col] = valueArray[row,col].ToString(); } dt.Rows.Add(dr); } //Set the Column order dt.Columns["Claim Number"].SetOrdinal(0); 

编辑1:

如果Datatable不可用还有一个logging/声明的列表,并根据标题名称制作列索引的映射。

 public class Claim { public string ClaimNo { get; set; } public string ClientName { get; set; } public string Uwyear { get; set; } public string AgreementNo { get; set; } public string BusinessType { get; set; } public DateTime? PeriodStart { get; set; } public DateTime? PeriodEnd { get; set; } public string PolicyNo { get; set; } public string PolicyName { get; set; } public DateTime? DateOfLoss { get; set; } public string ClaimantName { get; set; } public string ClaimedInsured { get; set; } public DateTime? ReportDate { get; set; } } 

….

 List<Claim> claims = new List<Claim>(); for (int row = 1; row < maxRows; row++) { var c = new Claim(); c.ClaimNo= valueArray[row,MapCol("ClaimNo")].ToString(); c.ClientName= valueArray[row,MapCol("ClientName")].ToString(); ... } claims.Add(c); 

编辑2:

使用字典来保存列号和名称。 然后,当您阅读Excel Header行时,您可以在运行时填充字典而不用硬编码。

 Dictionary<string, int> dictionaryColIndexs = new Dictionary<string, int>(); for (int col = 0; col < maxColumns; col++) { dictionaryColIndexs.Add(valueArray[0,col],col); } 

 private int MapCol(string colName) { if (dictionaryColIndexs.ContainsKey(colName)) { int index= dictionaryColIndexs[colName]; return index; } return -1; }