2017-06-16 109 views
-1

我正在使用SmartXLSX成功讀取excel文件,但我遇到了一個問題。我的程序正在讀取列標題的新行以及電子表格中的所有其他行。我應該如何更新我的程序以跳過列標題並讀取其他行。如何讀取c#中的excel文件跳過列標題?

private void GetCompanies() 
     { 
      int count = 0; 

      Companies = new List<Company>(); 

      string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
      string path = Path.Combine(directory, "Toll-Companies.xlsx"); 


      SmartXLS.WorkBook WB = new WorkBook(); 
      WB.readXLSX(path); 

      DataTable dt = WB.ExportDataTable(); 



       string CurrentType = string.Empty; 
       string CurrentCategory = string.Empty; 
       string Removerow = string.Empty; 

       string b,c; 
       //DataRow rowe = dt.Rows[0]; 
       //dt.Rows.Remove(rowe); 
       //loop through each row 

       foreach (DataRow dr in dt.Rows) 
        { 


        //Get company name in column c from Excel 
        c = dr[2].ToString(); 

        //Get value in column B from Excel 
        b = dr[0].ToString(); 


        if (b.StartsWith("Type:")) 
        { 
         CurrentType = b.Substring(6).Trim(); 
        } 

        if (b.StartsWith("Primary Specialty")) 
        { 
         CurrentCategory = b.Substring(20).Trim(); 
        } 

        //if company name is empy then skip row 
        if (string.IsNullOrEmpty(c)) continue; 

        //string Type = dr[7].ToString(); 
        //dt.Columns.Add(Type) 

        //string cmp_type = dr[0].ToString(); 
        //if (string.IsNullOrEmpty(cmp_type) || cmp_type == "Type") continue; 


        var cmp = new Company(); 


        cmp.company_type = CurrentType; 
        cmp.company_category = CurrentCategory; 
        cmp.name = dr[2].ToString(); 
        Companies.Add(cmp); 
        count++; 
} 

回答

0

這將讓你正在尋找的結果。我不確定您是否正在SmartXLSX庫中尋找方法。

foreach (DataRow dr in dt.Rows) 
{ 
    if(count==0) continue; 
0

您可以使用,並跳過第一行...

相關問題