Click here to Skip to main content
15,891,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
String sPath = Server.MapPath("./UploadedReports") + "/" + Convert.ToString(Session["path"]);
        string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + sPath + "; Extended Properties=\"Excel 12.0;HDR=No;IMEX=1;\"";

        OleDbConnection objConn = new OleDbConnection(strConn);
        try
        {
            // Code to save Final Report Header.
            FinalReportHeader objrep = new FinalReportHeader();
            objrep.UserId = Convert.ToInt32(Session["UserId"]);
            objrep.SubmitMonth = Convert.ToDateTime(TxtDate.Text);
            objrep.ReportId = Convert.ToInt32(ddrepname.SelectedItem.Value);
            objrep.ReportName = Convert.ToString(ddrepname.SelectedItem.Text);
            objrep.path = Convert.ToString(Session["path"]);
            objrep.remarks = Txtremark.Text;
            objrep.InsertReport();
 // Code to save Final Report Header.
            FinalReportDetail objFRD = new FinalReportDetail();
             DataTable dt = new DataTable();
            if (objConn.State == ConnectionState.Closed)
            {
                objConn.Open();
            }
            dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            DataSet myDataset = new DataSet();
            OleDbDataAdapter myData = new OleDbDataAdapter("SELECT * FROM [" + dt.Rows[0]["TABLE_NAME"] + "]", strConn);
            myData.TableMappings.Add("Table", "ExcelTest");
            myData.Fill(myDataset);
             //for (int i = 0; i < myDataset.Tables[0].Rows.Count; i++)
            //{
Posted
Updated 3-Aug-11 0:58am
v2
Comments
Syed Salman Raza Zaidi 3-Aug-11 6:59am    
Whats the problem arising?
BobJanova 3-Aug-11 8:01am    
What is your question? myDataset should include the data you want at the end of that code.

1 solution

In following code i moving the data from excel to grid view.
I am using one file upload control of asp.net
location of excel file is fixed in my project but u can select it from anywhere in your computer using file upload control property.

So now you have to transfer the data to table from grid, i hope that u know.


button_clik()


DataTable dt = new DataTable();
// string target = Server.MapPath("~/Excel");

if (FileUpload1.HasFile)
{


string pathname = "D:\\Test.xls";


string connectionString = String.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", pathname);

OleDbConnection oledbConn = new OleDbConnection(connectionString);//@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathname + ";\'Extended Properties=Excel 8.0\'");
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.DataSource = ds.Tables[0];
GridView1.DataBind();
GridView1.Visible = true;

}
catch
{

}
finally
{
// Close connection
oledbConn.Close();
}
}

}

Jagriti30-05-11 proof load
 
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