毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

c# EXCEL如何导入到SQLserver

更新时间:2015-12-18:  来源:毕业论文
 protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileName = Server.HtmlEncode(FileUpload1.FileName);
                string extension = System.IO.Path.GetExtension(fileName);
                if ((extension == ".xlsx") || (extension == ".xls"))
                {
                    ExalToSql();
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('请选择需要上传的Excel!!!')</script>");
                return;
            }
        }
 
        private void ExalToSql()
        {
            string sheet = FileUpload1.FileName.ToString();
            string path = Server.MapPath(FileUpload1.FileName);
            FileUpload1.PostedFile.SaveAs(path);//保存文件          
            string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";Extended Properties='Excel 8.0;IMEX=1'";
            //建立EXCEL的连接
            OleDbConnection objConn = new OleDbConnection(sConnectionString);
            int count = 0;//用来记录出错的条数
            try
            {
                string strCom = string.Format("select * from [upload$] ", sheet);
                objConn.Open();
 
                OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, objConn);
                DataSet ds = new DataSet();
                myCommand.Fill(ds, "[upload$]");
                DataTable dt = ds.Tables[0];
 
                foreach (DataRow myDrv in dt.Rows)
                {
                    count++;
                    if (!string.IsNullOrWhiteSpace(myDrv[1].ToString().Trim()))
                    {
                        /////// 先判断一下 是否已经上传过该笔数据
                        string XXX = "0";
                        string sqlXXX = " select COUNT(*) TOT from plate_issue_upload WHERE UP_SCORD=" + myDrv[1].ToString().Trim() + " AND UP_SORD=" + myDrv[2].ToString().Trim() + " AND  " +
                                        " UP_LINE=" + myDrv[3].ToString().Trim() + " AND UP_MORD='" + myDrv[4].ToString().Trim() + "' AND UP_MPROD='" + myDrv[5].ToString().Trim() + "' AND  " +
                                        " UP_QTY=" + myDrv[6].ToString().Trim() + " AND UP_MWHS='" + myDrv[7].ToString().Trim() + "' AND UP_MLLOC='" + myDrv[8].ToString().Trim() + "'";
                        SqlDataReader myreader;
                        myreader = SQL.GetMDCSQL(sqlXXX);
                        while (myreader.Read())
                        {
                            XXX = myreader["TOT"].ToString(); //0表示没有上传过该笔数据   1 表示已经上传过该笔记录
                        }
                        myreader.Close();
                        if (XXX == "0")
                        {
                            string sql = @"insert into plate_issue_upload (up_time,up_scord,up_sord,up_line,up_mord,up_mprod,up_qty,up_mwhs,up_mlloc,up_flag,UP_ISSUE) " +
                                     "values  ( GETDATE(), " +
                                     "" + myDrv[1].ToString().Trim() + ", " +
                                     "" + myDrv[2].ToString().Trim() + ", " +
                                     "" + myDrv[3].ToString().Trim() + ", " +
                                     "'" + myDrv[4].ToString().Trim() + "', " +
                                     "'" + myDrv[5].ToString().Trim() + "', " +
                                     "" + myDrv[6].ToString().Trim() + ", " +
                                     "'" + myDrv[7].ToString().Trim() + "', " +
                                     "'" + myDrv[8].ToString().Trim() + "', " +
                                     "'A',0)";
                            SQL.ExcutesMDCSQL(sql);
                        }
                    }
                }
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('第" + count.ToString() + "条数据出错,请核对!!!')</script>");
                return;
            }
            finally
            {
                objConn.Close();//关闭EXCEL的连接
                ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('上传成功!!!')</script>");
            }
        }
服务端安装excel了吗
对于xlsx,驱动是Provider=Microsoft.ACE.OLEDB.12.0;,分情况处理
最后的我的解决办法为:在对应的 IIS 应用程序池中,“设置应用程序池默认属性”/“常规”/”启用32位应用程序” 设置为true即可!!!  (服务器为X64位 ,当时默认32位为false)
设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。