ExcelReader,第一行被跳过

我正在为ERP系统开发一个小插件,即从Excel文件读取数据,并插入数据库(SQL)。 由于目前这张表的原因,第一行被跳过。 我试着解决这个问题,当抛出一个SQL错误时,当我强制程序插入一些无效的东西时,这行出现在SQL数据库中。

任何人都可以看到我做错了什么,因为我觉得我已经尝试了一切。

客户类别

class Customer { public int ActNo {get;set;} public int CustNo { get; set; } public string DelPri { get; set; } public int CustPrg3 = 22; public string Nm { get; set; } public int Gr6 = 5; public int CreDt { get; set; } public string CreUsr { get; set; } public Customer(int ActNo,int CustNo, string DelPri, string Nm, int CreDt){ this.ActNo = ActNo; this.CustNo = CustNo; this.DelPri = DelPri; this.CustPrg3 = 22; this.Nm = Nm; this.Gr6 = 5; this.CreUsr = "Excel"; this.CreDt = CreDt; } } 

读者

 public class ExcelData { string _path; public ExcelData(string path) { _path = path; } public IExcelDataReader getExcelReader() { // ExcelDataReader works with the binary Excel file, so it needs a FileStream // to get started. This is how we avoid dependencies on ACE or Interop: FileStream stream = File.Open(_path, FileMode.Open, FileAccess.Read); // We return the interface, so that IExcelDataReader reader = null; try { if (_path.EndsWith(".xls")) { reader = ExcelReaderFactory.CreateBinaryReader(stream); } if (_path.EndsWith(".xlsx")) { reader = ExcelReaderFactory.CreateOpenXmlReader(stream); } return reader; } catch (Exception) { throw; } } public IEnumerable<string> getWorksheetNames() { var reader = this.getExcelReader(); var workbook = reader.AsDataSet(); var sheets = from DataTable sheet in workbook.Tables select sheet.TableName; return sheets; } public IEnumerable<DataRow> GetSecondSheetData(bool firstRowIsColumnNames = true) { var reader = this.getExcelReader(); reader.IsFirstRowAsColumnNames = firstRowIsColumnNames; return reader.AsDataSet().Tables[0].AsEnumerable(); } } 

使用Reader,并添加到客户数组

 if (openFileDialog1.ShowDialog() == DialogResult.OK) { path = openFileDialog1.FileName; int maxActNo = 0; int maxCustNo = 0; var excelData = new ExcelData(path); var albums = excelData.GetSecondSheetData(); List<Customer> customers = new List<Customer>(); Customer testCust = new Customer(1, 1, "Test", "Test", Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"))); customers.Add(testCust); SqlDataReader rdr = cmd.ExecuteReader(); rdr.Read(); maxActNo = Convert.ToInt32(rdr["HighestActNo"]); maxCustNo = Convert.ToInt32(rdr["HighestCustNo"]); rdr.Close(); foreach (var row in albums) { if (row.ItemArray.Length == 8 && row.ItemArray[0].ToString() != "Dato" //&& row.ItemArray[0].ToString().Contains(convertedDate) && row.ItemArray[1].ToString() != "Varenummer" && row.ItemArray[2].ToString() != "Varenavn" && row.ItemArray[3].ToString() != "Kundekonto" && row.ItemArray[4].ToString() != "Navn" && row.ItemArray[5].ToString() != "Antall" && row.ItemArray[6].ToString() != "Antall" && row.ItemArray[7].ToString() != "Enhet" //&& row.ItemArray[0].ToString() != "" //&& row.ItemArray[1].ToString() != "" //&& row.ItemArray[2].ToString() != "" //&& row.ItemArray[3].ToString() != "" //&& row.ItemArray[4].ToString() != "" //&& row.ItemArray[5].ToString() != "" //&& row.ItemArray[6].ToString() != "" //&& row.ItemArray[7].ToString() != "" ) { Customer cust = new Customer( maxActNo, maxCustNo, row[3].ToString(), row[4].ToString(), Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"))); if (customers[customers.Count() - 1].DelPri != row[3].ToString()) { customers.Add(cust); maxActNo++; maxCustNo++; } } } customers.RemoveAt(0); ImportController controller = new ImportController(); controller.insertCustomerIfNotExist(customers); button2.Enabled = false; } } 

我select不显示SQL,因为我知道这个问题不在QUERY中,它是在我的if语句的某处,该行正在sorting,但我已经尝试了几个小时,并认为我需要一个提示。

欣赏任何build议

对于任何人看到这个我想出了为什么我的代码不像我期望的那样运行。 在上面的代码中没有错误,我对我的插入循环从索引1开始,而不是索引0。

closures这个问题,但是要感谢任何阅读或试图回答的人。

 for (int i = 1; i < customers.Count; i++) 

需要成为

 for (int i = 0; i < customers.Count; i++) 

在插入function。