1
我正在開發ERP系統的一個小插件,即從Excel文件讀取數據,並插入數據庫(SQL)。由於目前工作表的原因,第一行被跳過。我試着解決這個問題,當拋出一個SQL錯誤時,當我強制程序插入一些無效的東西時,這一行出現在SQL數據庫中。ExcelReader,第一行跳過
任何人都可以看到我做錯了什麼,因爲我覺得我已經嘗試了一切。
Customer類
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();
}
}
用該閱讀器,並加入到客戶陣列
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語句中某處正在排序,但我已經嘗試了幾個小時,並且認爲我需要提示。
欣賞任何建議