Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the app soluion is as follows//////////////////////////////////////////////
Posted
Updated 24-Jul-11 2:42am
v2

Plz check This

Or try the following

Method for obtain a Excel file
private string SelectTextFile()
{
   OpenFileDialog dialog = new OpenFileDialog();
   dialog.Filter = "xls file (*.xls)|*.xls |xlsx file(*.xlsx)|*.xlsx|all file(*.*)|*.*";
   dialog.Title = "Select a File";
   return (dialog.ShowDialog() == DialogResult.OK)
    ? dialog.FileName : null;
}


Method for Excel Connection and putting Data in a Datatable

private void getDataFromXLS(string strFilePath)
{
  string strConn = string.Empty;
  try
   {
    if (System.IO.Path.GetExtension(strFilePath).Equals(".xlsx"))
     {
       strConn = "provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + strFilePath + "';Extended Properties=Excel 12.0;";
      }
      else
      {
         strConn = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + strFilePath + "';Extended Properties=Excel 8.0;";
      }
      OleDbConnection cnCSV = new OleDbConnection(strConn);
      cnCSV.Open();
      System.Data.DataTable dtsheetName = new System.Data.DataTable();
      dtsheetName = cnCSV.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
      string excelSheets = string.Empty;
      int i = 0;
      foreach (DataRow row in dtsheetName.Rows)
      {
        excelSheets = row["TABLE_NAME"].ToString();
        break;
      }

      OleDbCommand cmdSelect = new OleDbCommand("SELECT * FROM [" + excelSheets + "]", cnCSV);
      OleDbDataAdapter daCSV = new OleDbDataAdapter();
      daCSV.SelectCommand = cmdSelect;
      dtCSV.Clear();   //dtCSV is a Global Variable of DataTable type
      daCSV.Fill(dtCSV);
      cnCSV.Close();
      daCSV.Dispose();
      cmdSelect.Dispose();
    }
    catch (Exception ex)
    {
     string Message = ex.Message.ToString();
     MessageBox.Show(Message);
    }
}


Method for uploadation
private void Upload()
try
 {
  string fname = SelectTextFile();
  if (fname == null || string.IsNullOrEmpty(fname.Trim()))
  {
   MessageBox.Show("No Excel Sheet is selected to upload.");
  }
  else
  {
   string ifname = fname.Substring(fname.LastIndexOf('\\') + 1);
   string[] filename = ifname.Split('.');
   getDataFromXLS(fname);
   SqlBulkCopy bulk = new SqlBulkCopy(SQLConnection);
   bulk.DestinationTableName = "Test";
   bulk.WriteToServer(dtCSV);
   MessageBox.Show("Data uploaded successfully.");
  }
}
catch (Exception ex)
{
 MessageBox.Show(ex.Message.ToString());
}
 
Share this answer
 
v4
Comments
Dalek Dave 20-Jul-11 3:29am    
Good Answer.
DanHodgson88 20-Jul-11 4:58am    
very nice 5*
Db issues 20-Jul-11 5:38am    
thats good
Db issues 21-Jul-11 5:37am    
finally i solved, thanks Raju, highly obliged
Raju Prajapati 25-Jul-11 2:19am    
Its my plzr..
You can do this by doing a Bulk Copy as shown here[^].
 
Share this answer
 
Comments
Db issues 20-Jul-11 2:50am    
The 'Microsoft
.Jet.OLEDB.4.0' provider is not registered on the local machine.
this is error
//////////////////////////////////////////////////////////////////////
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900