Connection String for Excel data into Database

For Excel 2010 Use Below Connection String.
  1. String StrConn;  
  2. StrConn ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="  
  3. + Excelpath +";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
For Excel 2003 Use Below Connection String.
  1. String StrConn;  
  2. StrConn ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="  
  3. + Excelpath +";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  

Use this function to convert excel to csv:

  1. public int ExcelToCVS_Nostro(string Excelpath, string SheetName, string CSVpath, string report_date, string strFileType)  
  2. {  
  3.     
  4.    string StrConn = null;  
  5.   
  6.    OleDbDataAdapter DA = new OleDbDataAdapter();  
  7.    
  8.    DataSet DS = new DataSet();  
  9.    OleDbDataReader DR = default(OleDbDataReader);  
  10.   
  11.    string Str = null;    
  12.    int ColumnCount = 0;    
  13.    int OuterCount = 0;    
  14.    int InnerCount = 0;    
  15.    int RowCount = 0;  
  16.   
  17.    if (strFileType.Trim() == ".xls")    
  18.    {  
  19.   
  20.       StrConn =  "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Excelpath +  
  21.   
  22. ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";  
  23.   
  24.    }  
  25.   
  26.    else if (strFileType.Trim() == ".xlsx")    
  27.    {  
  28.   
  29.    StrConn =  
  30.   
  31.    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excelpath +    
  32. "; Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";  
  33.   
  34.    }  
  35.   
  36.    OleDbConnection objConn = new OleDbConnection(StrConn);  
  37.   
  38.    try    
  39.    {  
  40.   
  41.       if (objConn.State == ConnectionState.Closed)    
  42.       {  
  43.            objConn.Open();    
  44.       }  
  45.   
  46.       else    
  47.       {   
     
  48.       }  
  49.   
  50.    }  
  51.   
  52.    catch (Exception ex)    
  53.    {  
  54.   
  55.       return 0;  
  56.   
  57.    }  
  58.   
  59.    OleDbCommand objCmd = new OleDbCommand("Select * from [" + SheetName + "$]", objConn);  
  60.   
  61.    objCmd.CommandType = CommandType.Text;  
  62.   
  63.    StreamWriter ObjStreamWriter = default(StreamWriter);  
  64.   
  65.    ObjStreamWriter = new StreamWriter(CSVpath);  
  66.   
  67.    int Count = 0;    
  68.    Count = 0;  
  69.   
  70.    try    
  71.    {  
  72.   
  73.       DA.SelectCommand = objCmd;    
  74.       DA.Fill(DS;   
  75.       "XLData");  
  76.   
  77.    }  
  78.   
  79.    catch (Exception ex)   
  80.    {  
  81.   
  82.    }  
  83.   
  84.    RowCount = DS.Tables[0].Rows.Count - 4;  
  85.   
  86.    ColumnCount = DS.Tables[0].Columns.Count;     
  87.   
  88.    for (OuterCount = 0; OuterCount <= DS.Tables[0].Rows.Count - 1; OuterCount++)    
  89.    {  
  90.   
  91.       Str = "";     
  92.   
  93.       for (InnerCount = 1; InnerCount <= 14; InnerCount++)  
  94.       {     
  95.   
  96.          if (DS.Tables[0].Rows[OuterCount][1].ToString().Trim() != string.Empty)    
  97.          {     
  98.   
  99.             if (DS.Tables[0].Rows[OuterCount]
  100.             [1].ToString().Trim().Contains("LONDON") == false && DS.Tables[0].Rows[OuterCount]
  101.             [1].ToString().Trim().Contains("QAR") == false)    
  102.             {   
  103.   
  104.                if (DS.Tables[0].Rows[OuterCount][2].ToString().Trim() != "INR")    
  105.                {  
  106.   
  107.                   Str += DS.Tables[0].Rows[OuterCount][InnerCount].ToString().Trim().Replace(    
  108.                   ",""").Replace(" 12:00:00 AM""") + ",";    
  109.                }    
  110.                else      
  111.                break;    
  112.             }    
  113.          }  
  114.          else   
  115.          {     
  116.             break;    
  117.          }    
  118.       }  

  119.       else    
  120.       {  
  121.           break;    
  122.       }   
  123.      
  124.       if (Str != string.Empty)    
  125.       {    
  126.          Str += report_date;    
  127.          ObjStreamWriter.WriteLine(Str);   
  128.       }  
  129.   
  130.    }  
  131.   
  132.    ObjStreamWriter.Close();    
  133.   
  134.    try    
  135.    {  
  136.   
  137.    }  
      
  138.    catch (Exception ex)    
  139.    {  
  140.   
  141.       objCmd.Dispose();    
  142.       objCmd = null;   
  143.       objConn.Close();   
  144.       objConn.Dispose();   
  145.       objConn = null;      
  146.       return RowCount;  
  147.   
  148.    }  
  149.   
  150. }