ExcelReader,第一行被跳过

时间:2015-11-09 22:20:53

标签: c# sql excel

我正在为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,并添加到customer array

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;

        }

    }

我选择不显示SQL,因为我知道问题不在QUERY中,它在我的if语句中的哪个行已经排序,但我已经尝试了几个小时并且认为我需要一个提示。

感谢任何建议

1 个答案:

答案 0 :(得分:0)

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

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

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

需要

for (int i = 0; i < customers.Count; i++)
插入函数中的