asp.net 中 利用 OleDbConnection 连接 "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;'"; 读取数据到数据库,开始时出现,混合数据类型的列数据丢失现象,我根据网上的资料修改 连接字符串 添加 MaxScanRows=0;IMEX=1;,修改注册表信息 ImportMixedTypes = Text ,路径也按照正确的修改了,还有 ImportMixedTypes 我设置为 0  。这样做之后没有出现数据丢失,却出现了数据和原始值不一样了。  比如:1.59974e+010  ,  该怎么办???   

解决方案 »

  1.   

    建议你用Worksheet来读取或写入Excel数据。
    你这个样子用OLEDB读取 而且还有版本限制,像OFFICE2007的话,这种方式就不行了
      

  2.   

    设置excel单元格格式
    range.NumberFormatLocal = "@";     //设置单元格格式为文本
      

  3.   

    我用 OleDbConnection 连接的方式读取Excel表格也可以像你这样设置它的格式吗?
      

  4.   

     public DataSet ExcelDS(string fileUrl, string table)
        {
          
            string Connstr = "Provider=Microsfot.Jet.Oledb.4.0;" + "data source=" + fileUrl + "; Extended Properties=-'Excel 8.0;'";
      
            OleDbConnection Conn = new OleDbConnection();
            Conn.ConnectionString = connstr;
            OleDbDataAdapter da = new OleDbDataAdapter("select * from [Sheet1$]",Conn);//[Sheet1$]为Excel表格的第一行        DataSet ds = new DataSet();
            da.Fill(ds,table);
            return ds; 
        }   if (FileUpload1.HasFile == false)
            {
                Response.Write("<script>alert('请选择文件上传!')</script>");
                return;
            }
            string FileEx = Path.GetExtension(FileUpload1.PostedFile.FileName).ToString().ToLower();
            if (FileEx != "xls")
            {
                Response.Write("<script>alert('只可以选择Excel文件')</script>");
                return;
            }
            SqlConnection Conn = new SqlConnection(Connstr);
            Conn.Open();
            string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
            string serverpath = Server.MapPath("upfiles/")+filename;
            FileUpload1.PostedFile.SaveAs(serverpath);
            DataSet ds = ExcelDS(serverpath, filename);
            DataRow[] dr = ds.Tables[0].Select();
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('Excel表为空表,无数据!')</script>");
            }
            else 
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
                    string hhaspx_xm = dr[i]["姓名"].ToString();
                    string hhaspx_dx = dr[i]["底薪"].ToString();
                    string hhaspx_kh = dr[i]["考核"].ToString();
                    string hhaspx_jl = dr[i]["奖励"].ToString();
                    string hhaspx_jt = dr[i]["津贴"].ToString();
                    string hhaspx_jb = dr[i]["加班"].ToString();
                    string hhaspx_zb = dr[i]["值班"].ToString();
                    string hhaspx_jx = dr[i]["绩效"].ToString();
                    string hhaspx_hj = dr[i]["合计"].ToString();
                    string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'";
                    SqlCommand Cmd = new SqlCommand(sqlcheck,Conn);
                    int count = Convert.ToInt32(Cmd.ExecuteScalar());
                    if (count < 1)
                    {
                        string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";
                        SqlCommand Cmd = new SqlCommand();
                        Cmd.CommandText = insertstr;
                        Cmd.Connection = Conn;
                        try
                        {
                            Cmd.ExecuteNonQuery();                    }
                        catch (MembershipCreateUserException ex)
                        {
                            Response.Write("<script>alert(" + ex.Message.ToString() + ")</script>");
                        }
                        finally
                        {
                            Conn.Close();
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('导入内容重复!');window.location.href='Default.aspx'</script>");
                        continue;
                    }
     
                }
                Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
            }        Conn.Close();
      

  5.   


    这个Excel导入我知道。但是会出现数据丢失现象,你难道没有遇到过吗?当一列的数据为纯数字时会出现数据丢失。    所以这种方法有缺陷。所以我改了用Excel的Application对象来获得操作Excel。数据提取得很准确。问题已经解决.