Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I making an application which upload data directly from excel, I have connected to excel with oledb connection object but i am unable to read the excel.

My code :
C#
 private string GetConnection()
    {
        string connectionString="";
        string path = fpUpload.FileName;
        string ext = System.IO.Path.GetExtension(path);
        string fileName = System.IO.Path.GetFileName(path);
        if (ext.ToString() == ".xls")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.4.0;Data Source=" + fileName + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (ext.ToString() == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }
        else
        {
            Response.Write("<script type=text/javascript language=javascript>alert('Please select proper file')</script>");
        }
        return connectionString;
    }
private void GetData()
    {
        string conn = GetConnection();
        DataTable dt = new DataTable();

        string query = "SELECT * from [Sheet1$]";
        olCn = new OleDbConnection();
        olCn.ConnectionString = conn;
        olCn.Open();
     //   dt = olCn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        olCmd = new OleDbCommand(query, olCn);
        olDa = new OleDbDataAdapter(olCmd );
        ds = new DataSet();
        olDa.Fill(ds);
        UploadData(ds);

    }


and though the excel sheet contains Sheet1 in it but it gives me error that it could not find Sheet1 in excel .

Please help me regarding this..

Thanks & Regards,
Krunal Panchal
Posted

The only one reason you can't read your Excel files is bad IMEX option value!

Default IMEX option should be 0 (zero).
If you would like to import data as a text, set IMEX = 1.
 
Share this answer
 
v2
Comments
VJ Reddy 16-May-12 10:44am    
Good point. 5!
Maciej Los 16-May-12 10:56am    
Thank you, VJ ;)
Please Check my codes:

C#
String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + Server.MapPath(" test.xls") + ";" +
                "Extended Properties=Excel 8.0;";       
            OleDbConnection objConn = new OleDbConnection(sConnectionString);
            objConn.Open();

            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * from [Sheet1$]", objConn);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
             objAdapter1.SelectCommand = objCmdSelect;

           
            DataSet objDataset1 = new DataSet();            
            objAdapter1.Fill(objDataset1, "XLData");
            GridView1.DataSource = objDataset1.Tables[0].DefaultView;
            GridView1.DataBind();

Regards

Also a link available; http://forums.asp.net/t/1039391.aspx/1[^]
 
Share this answer
 
v3
Comments
Maciej Los 15-May-12 4:58am    
Good example, but without HDR and IMEX options. 4+
Have a look at this Tip that is working: Read Excel in ASP.NET[^]
 
Share this answer
 
 
Share this answer
 
have a look at this
i hope that it will certainely help you

http://forums.asp.net/t/1192930.aspx[^]
Programmatically Convert Documents to PDFs the Easy Way[^]

Thanks
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 15-May-12 2:29am    
Links formatted
There are some problems in your code:
the first one is that you build the connection string with the file name and not with the path of the Excel file;
then you use a provider (Microsoft.ACE.OLEDB.4.0) that doesn't exist.
The IMEX value is wrong, but I think that it isn't the main problem.
In the following I have tried to revise it:

C++
private string GetConnection(string filePath)
{
	string connectionString="";
	string ext = System.IO.Path.GetExtension(path);
	if (ext.ToString() == ".xls")
	{
		connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
	}
	else if (ext.ToString() == ".xlsx")
	{
		connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=1\"";
	}
	else
	{
		Response.Write("<script type=text/javascript language="javascript">alert('Please select proper file')</script>");
	}
	return connectionString;
}

private DataTable GetData(string filePath)
{
	string conn = GetConnection(filePath);
	DataTable dt = new DataTable();

	string query = "SELECT * from [Sheet1$]";
	OleDbConnection olCn = new OleDbConnection();
	olCn.ConnectionString = conn;
	olCn.Open();
	OleDbCommand olCmd = new OleDbCommand(query, olCn);
	OleDbDataAdapter olDa = new OleDbDataAdapter(olCmd );
	DataSet ds = new DataSet();
	olDa.Fill(ds);
	dt = ds.Tables[0];
	
	return dt;
}

I hope this helps you.
 
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