Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to show Excel file data to Grid view

save Grid data into sql table

Pls, help me ASAP.

Thank you.
Posted

Try this:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        string connString = ConfigurationManager.ConnectionStrings["xls"].ConnectionString;
        // Create the connection object
        OleDbConnection oledbConn = new OleDbConnection(connString);
        try
        {
            // Open connection
            oledbConn.Open();
 
            // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
 
            // Create new OleDbDataAdapter
            OleDbDataAdapter oleda = new OleDbDataAdapter();
 
            oleda.SelectCommand = cmd;
 
            // Create a DataSet which will hold the data extracted from the worksheet.
            DataSet ds = new DataSet();
 
            // Fill the DataSet from the data extracted from the worksheet.
            oleda.Fill(ds, "Employees");
 
            // Bind the data to the GridView
            GridView1.DataSource = ds.Tables[0].DefaultView;
            GridView1.DataBind();
        }
        catch
        {
        }
        finally
        {
            // Close connection
            oledbConn.Close();
        } 
    }

Ref.:Read Data From an Excel File (.xls) in ASP.NET[^]

Please refer:
import from excel to gridview[^]
 
Share this answer
 
Go step wise. Start from showing excel data in gridview, like:
C#
public DataSet GetDataSetForGrid()
{
  string cnstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\\MyExcelSheet.xls;" + "Extended Properties=Excel 9.0";
  OleDbConnection oledbConn = new OleDbConnection(cnstr);
  string strSQL = "SELECT * FROM [Sheet1$]";
  OleDbCommand cmd = new OleDbCommand(strSQL, oledbConn);
  DataSet ds = new DataSet(); 
  OleDbDataAdapter da = new OleDbDataAdapter(cmd);
  da.Fill(ds); 
  return ds;
}


An article discussing same: Import Data from Excel to DataGridView in C#[^]

Try out!
 
Share this answer
 

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