Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Here I want to Bind the data from Excel Sheet to Database....I want to skip First 6 rows and then i want to bind the data to database ...this is my code how to do this....Please help me

C#
string connectionString = "";
       if (FileUpload1.HasFile)
       {
           string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
           string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
           string fileLocation = Server.MapPath(fileName);
           FileUpload1.SaveAs(fileLocation);

           //Check whether file extension is xls or xslx

           if (fileExtension == ".xls")
           {
               connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
           }
           else if (fileExtension == ".xlsx")
           {
               connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
           }

           //Create OleDB Connection and OleDb Command

           OleDbConnection con = new OleDbConnection(connectionString);
           OleDbCommand cmd = new OleDbCommand();
           cmd.CommandType = System.Data.CommandType.Text;
           cmd.Connection = con;
           OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
           DataTable dtExcelRecords = new DataTable();
           con.Open();
           DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
           string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
           cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
           dAdapter.SelectCommand = cmd;
           dAdapter.Fill(dtExcelRecords);
           con.Close();
           GridView1.DataSource = dtExcelRecords;
           GridView1.DataBind();
Posted
Comments
Sunasara Imdadhusen 22-May-14 8:28am    
Bind data to database means what? DO you want to save records in db starts from 7th row?
phmani469 22-May-14 8:49am    
yes

you can do it with Linq
C#
GridView1.DataSource = dtExcelRecords.AsEnumerable().Skip(6).CopyToDataTable();


Ref:
http://msdn.microsoft.com/en-us/library/bb396189(v=vs.90).aspx[^]
 
Share this answer
 
v2
Comments
CHill60 22-May-14 8:32am    
5'd - for giving a solution that actually works and isn't a sql server function!! ;-)
DamithSL 22-May-14 8:36am    
Thank you CHill(Hawkeye)60
Sunasara Imdadhusen 22-May-14 8:52am    
Greate job DamithSL
DamithSL 22-May-14 9:03am    
Thank you Sunasara
phmani469 22-May-14 8:56am    
Thank you
You can delete top 6 rows from datatable before binding it to GridView:
C#
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
con.Close();
//This will delete top 6 rows from datatable
for(int i = 0; i< = 5; i++)
{
    dtExcelRecords.Rows[0].Delete();
}
GridView1.DataSource = dtExcelRecords;
GridView1.DataBind();
 
Share this answer
 
Comments
phmani469 22-May-14 8:56am    
thanks Sunasara

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