Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends i want to give user facility of uploading excel sheet in both .xls and .xlsx format .On Local ,My code is working fine for .xlsx file , but giving error for .xls file .
But on server it is giving error for both formats.
This is my code

using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

 public void ShowStatus()
    {
        DsOrders = ObjStatus.ShowOrderStatus();
        if (DsOrders.Tables.Count > 0)
        {
            if (DsOrders.Tables[0].Rows.Count > 0)
            {
                Grdorders.DataSource = DsOrders;
                Grdorders.DataBind();
            }
            else
            {
                lblstatus.Text = "There is not record ";
            }
        }

    }
    public void saveCustomers(string DirPath, string filename, string FileExtnsn)
    {



        //Declare Variables - Edit these based on your particular situation
        string sSQLTable = "PaymentReport";
        string sExcelFileName = filename;
        string sWorkbook = "[Sheet1$]";
        string extnsn = FileExtnsn;
        string sExcelConnectionString;
        //Create our connection strings


        //Dim sSqlConnectionString As String = Resources.Resource.ConnectionString

        //Series of commands to bulk copy data from the excel file into our SQL table
        if (extnsn == ".xlsx")
        {
            sExcelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + DirPath + sExcelFileName +

";Extended Properties='Excel 12.0 Xml;HDR=YES'";
        }
        else
        {
            sExcelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DirPath + sExcelFileName +
                        ";Extended Properties='Excel 8.0;HDR=YES;'";
        }
        OleDbConnection OleDbConn = new OleDbConnection(sExcelConnectionString);
        //OleDbCommand OleDbCmd = new OleDbCommand(("Select a,b,c,d,e,f,g,h,pk,j,k,l,m,n,o from " + sWorkbook), OleDbConn);
        OleDbCommand OleDbCmd = new OleDbCommand(("Select [Way Bill No],[Stage Description],[Reasion Description],[Remarks],[Updated Location] from " + sWorkbook), OleDbConn);



        // Dim OleDbCmd As OleDbCommand = New OleDbCommand(("SELECT custTitle, custFName, custMName, custLName FROM " & sWorkbook), OleDbConn)


        //OleDbCmd.CommandTimeout = "600";
        OleDbConn.Open();

        //Try

        //Dim productID As Integer
        bool customerExists = false;
        string notAddedIds = " Following User's were not added as the User ID's already exist.<br /> Please try adding these users with different id's. <br />";

        int rcode = 0;
        string stremp = "";


        OleDbDataReader dr = OleDbCmd.ExecuteReader();

        if (dr.HasRows)
        {

            while (dr.Read())
            {

             //    [Way Bill No],[Stage Description,[Reason Description],[Remarks],[Updated Location]

                ObjStatus.intwaybillno = getVal(Convert.ToInt32(dr["Way Bill No"]));
                ObjStatus.strStageDescription = getVal2((dr["Stage Description"]).ToString());
                ObjStatus.strReasionDescription = getVal3((dr["Reasion Description"]).ToString());
                ObjStatus.strremarks = getVal4((dr["Remarks"]).ToString());
                ObjStatus.strUpdatedLocation = getVal5((dr["Updated Location"]).ToString());
                         rcode = ObjStatus.Insertexceldataintoorderstatus();
                if (rcode == 1)
                {
                    lblmsg.Text = "Status has been added successfully";
                 
                }
                else
                {

                }
            }


            dr.Close();
            OleDbConn.Close();
        }

    }




    public int getVal(int myVal)
    {
        if (myVal == 0)
        {
            return (0);
        }
        else
        {

            return myVal;
        }
    }
    public string getVal2(string myVal)
    {
        if (myVal == System.DBNull.Value.ToString())
        {
            return ("");
        }
        else
        {
            myVal = myVal.Trim();
            return (myVal);
        }
    }


    public string getVal3(string myVal)
    {
        if (myVal == System.DBNull.Value.ToString())
        {
            return ("");
        }
        else
        {
            myVal = myVal.Trim();
            return (myVal);
        }
    }
    public string getVal4(string myVal)
    {

        if (myVal == System.DBNull.Value.ToString())
        {
            return ("");
        }
        else
        {
            myVal = myVal.Trim();
            return (myVal);
        }

    }
    public string getVal5(string myVal)
    {
        if (myVal == System.DBNull.Value.ToString())
        {
            return ("");
        }
        else
        {
            myVal = myVal.Trim();
            return (myVal);
        }
    }

    protected void BtnSbmit_Click(object sender, EventArgs e)
    {

        try
        {
            //string edatetime = getDate(DateTime.Now);
            string DirPath = Server.MapPath("ExcelSheet\\");
            string filepath = null;
            string filename = null;
            string Fextnsn = null;
            if (UploadExcel.PostedFile.ContentLength != 0)
            {
                //First Create Director
                if (!Directory.Exists(DirPath))
                {
                    Directory.CreateDirectory(DirPath);
                }
                filepath = UploadExcel.PostedFile.FileName;
                FileInfo fInfo = new FileInfo(filepath);
                filename = fInfo.Name;
                Fextnsn = Path.GetExtension(UploadExcel.FileName).ToLower();
                //save the posted file

                //Err().Visible = false;
                UploadExcel.PostedFile.SaveAs(DirPath + filename);
                try
                {
                    saveCustomers(DirPath, filename, Fextnsn);
                    lblmsg.Text = "Status has been added successfully ";
           
                }
                catch (Exception ex)
                {
                    //err.Visible = True
                    //err.Text = "Please upload the expected format file"
                    //File.Delete(DirPath & filename)
                }
                if (File.Exists(DirPath + filename))
                {
                    File.Delete(DirPath + filename);

                }
            

            }
            else
            {
                //Err().Visible = true;
                //Err().Text = "Some error occured during the Uploading";
            }
            ShowStatus();
         
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }


    }


It is showing error :-The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine with .xlsx file.

Error
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine with .xlsx file.



please help me ,thanks in advance friends.
Posted
Updated 10-Oct-11 19:35pm
v2

1 solution

You are relying on Microsoft Office being installed on your server which is not the case.

For these types of scenarios you are better of using non MSOffice Excel components.
 
Share this answer
 
Comments
pawanvats 11-Oct-11 1:46am    
that is what i was thinking sir , we can't install office on server .Can you please tell me some other way of doing this .
Mehdi Gholam 11-Oct-11 1:53am    
Search for "excel component" and there are lots.
pawanvats 11-Oct-11 2:34am    
i am not finding any appropriate examples for that ,can you please suggest me some examples of using that .
Mehdi Gholam 11-Oct-11 2:54am    
SpreadsheetGear is good, aspose.excel also.

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