返回首页

节能Excel工作表2003或2007 | SQL Server数据库:朋友你好,我想给用户上传的Excel工作表的设施。xls和。xlsx格式。在地方,我的代码是正常的。xlsx文件,但给错误的。xls文件。
但它是在服务器上提供两种格式的错误。
这是我的代码

   

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);

        }

 



    }

它显示错误的。'Microsoft.ACE.OLEDB.12.0"供应商未在本地计算机上注册xlsx文件

错误
'Microsoft.Jet.OLEDB.4.0'提供未在本地计算机上注册。xlsx文件。

请帮助我,感谢朋友提前。| pawanvats

回答