Hi friend,
The below Code may Help You, as i have created it in asp .net C#...
protected void btnUpload_Click(object sender, EventArgs e)
{
if (string.Compare(System.IO.Path.GetExtension(FileUpload1.FileName), ".xls", true) != 0)
{
filename = (FileUpload1.FileName);
file1 = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
path = Server.MapPath(@"~/Docs/" + file1 + "");
if (!System.IO.File.Exists(path))
{
FileUpload1.PostedFile.SaveAs(path);
}
string strQuery = string.Empty;
strExcelCon = ConfigurationSettings.AppSettings["ExcelSource"] + path;
OleDbConnection excelCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strExcelCon + ";Extended Properties=Excel 12.0");
string command = "Select * FROM [Sheet1$]";
excelCon.Open();
OleDbDataAdapter da = new OleDbDataAdapter(command, excelCon);
ds = new DataSet();
da.Fill(ds, "Sheet1");
GridView1.DataSource = ds;
GridView1.DataBind();
excelCon.Close();
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string DateTime = System.DateTime.Now.Day + "/" + System.DateTime.Now.Month + "/" + System.DateTime.Now.Year;
SqlCommand comm;
if (ddlOptions.SelectedItem.Text == "Normal")
{
comm = new SqlCommand("Insert into UploadQuestion(BatchID,SchoolName,Question,Class,Subject,ExamType,Chapter,Topic,Marks,UploadedDate,Status) values('" + batch + "','" + txtSchoolName.Text + "','" + GridView1.Rows[i].Cells[0].Text + "','" + GridView1.Rows[i].Cells[1].Text + "','" + GridView1.Rows[i].Cells[2].Text + "','" + GridView1.Rows[i].Cells[3].Text + "','" + GridView1.Rows[i].Cells[4].Text + "','" + GridView1.Rows[i].Cells[5].Text + "','" + txtMarks.Text + "','" + System.DateTime.Now + "','" + "0" + "')", gc.Con);
}
else
{
comm = new SqlCommand("Insert into Questions(BatchID,SchoolName,que,O1,O2,O3,O4,ans,class,Subject,ExamType,Chapter,Topic,marks,UploadedDate) values('" + batch + "','" + txtSchoolName.Text + "','" + GridView1.Rows[i].Cells[0].Text + "','" + GridView1.Rows[i].Cells[1].Text + "','" + GridView1.Rows[i].Cells[2].Text + "','" + GridView1.Rows[i].Cells[3].Text + "','" + GridView1.Rows[i].Cells[4].Text + "','" + GridView1.Rows[i].Cells[5].Text + "','" + GridView1.Rows[i].Cells[6].Text + "','" + GridView1.Rows[i].Cells[7].Text + "','" + GridView1.Rows[i].Cells[8].Text + "','" + GridView1.Rows[i].Cells[9].Text + "','" + GridView1.Rows[i].Cells[10].Text + "','" + txtMarks.Text + "','" + System.DateTime.Now + "')", gc.Con);
}
gc.getconnection();
comm.ExecuteNonQuery();
}
lblMessage.Text = "Data has been Uploaded successfully.";
}
}