2015-01-29 3 views
0

데이터 테이블의 헤더가 테이블의 레코드로 삽입됩니다. 행 번호가 증가하지만 헤더가 삽입됩니다. 누군가 그 문제점이 무엇인지 말해 줄 수 있습니까? 아래 코드를 보면 rows.MoveNext를하고 있습니다.SQLBulkcopy - 데이터베이스 테이블에 삽입되는 데이터 테이블의 헤더

 XSSFWorkbook xssfwb; 
     using (FileStream file = new FileStream(excelPath, FileMode.Open, FileAccess.Read)) 
     { 
      xssfwb = new XSSFWorkbook(file); 
     } 


     var sheet = xssfwb.GetSheetAt(0); // Change this to the worksheet you want to import. 
     var rows = sheet.GetRowEnumerator(); 

     var dtExcelData = new DataTable(); 
     var linenumber = 0; 
     DataRow dr; 


     dtExcelData.Columns.AddRange(new DataColumn[3] { 
     new DataColumn("AccountNumber", typeof(string)), 
     new DataColumn("Amount", typeof(decimal)), 
     new DataColumn("Sedol",typeof(string)) }); 



     while (rows.MoveNext()) 
     { 
      IRow row = (XSSFRow)rows.Current; 
      linenumber++; 

      row.GetCell(4).SetCellType(CellType.Numeric); 

      if (row.GetCell(0) != null) 
      { 
       dr = dtExcelData.NewRow(); 
       dr["AccountNumber"] = row.GetCell(1).ToString(); 
       dr["Amount"] = decimal.Parse(row.GetCell(4).ToString()); 
       //dr["Amount"] = (decimal)row.GetCell(4).NumericCellValue; 
       dr["Sedol"] = row.GetCell(11).ToString(); 


       dtExcelData.Rows.Add(dr); 
      } 
     } 

     //DealingContext.ExecuteCommand("TRUNCATE TABLE [dbDealing].[MESSAGING].[Rebate]"); 
     DealingContext.ExecuteCommand("TRUNCATE TABLE [dbo].[Testxyz]"); 




     //Set the database table name 
     using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(DealingContext.Connection.ConnectionString)) 
     { 
      //sqlBulkCopy.DestinationTableName = "[dbo].[Testxyz]"; 
      sqlBulkCopy.DestinationTableName = "[dbo].[Testxyz]"; 

      //[OPTIONAL]: Map the Excel columns with that of the database table 

      sqlBulkCopy.ColumnMappings.Add("AccountNumber", "AccountNumber"); 
      sqlBulkCopy.ColumnMappings.Add("Amount", "Amount"); 
      sqlBulkCopy.ColumnMappings.Add("Sedol", "Sedol"); 
      sqlBulkCopy.WriteToServer(dtExcelData); 


     } 

답변

0

헤더는 당신이 가져올 때 그냥 행을 건너, rows에서 반환 된 첫 번째 행입니다.

//(snip) 
    dtExcelData.Columns.AddRange(new DataColumn[3] { 
    new DataColumn("AccountNumber", typeof(string)), 
    new DataColumn("Amount", typeof(decimal)), 
    new DataColumn("Sedol",typeof(string)) }); 


    //Makes the header row the current row. 
    rows.MoveNext(); 

    //Moves to the first row with data and loops till done. 
    while (rows.MoveNext()) 
    { 
     IRow row = (XSSFRow)rows.Current; 
     linenumber++; 

     row.GetCell(4).SetCellType(CellType.Numeric); 

     if (row.GetCell(0) != null) 
     { 
//(snip) 
관련 문제