您的位置:首页 > 博客中心 > 数据库 >

EXCEL导入GridView,然后再汇入数据库.

时间:2022-03-13 22:59

原文:

近日项目中有一个多笔料号要输入,我做了一个用javascript复制输入框的功能,可以输入多笔料号。
但是使用者反馈,料号太多,可能几百个料号在EXCEL文件里,这样输入很慢,需要增加功能。于是想到了SQLSERVER导入EXCEL的功能。
gxlsystem.com,布布扣declare @strExcelName varchar(50)
gxlsystem.com,布布扣set @strExcelName=‘c:\sl.xls‘
gxlsystem.com,布布扣exec(‘select * into ##tmp from openrowset(‘‘MICROSOFT.JET.OLEDB.4.0‘‘,‘‘Excel 8.0;HDR=No;IMEX=1;DATABASE=‘+@strExcelName+‘‘‘,[Sheet1$]) ‘)
gxlsystem.com,布布扣select * from ##tmp
这样是可以导入数据,但是问题来了。我的数据库服务器和运行程序服务器不在同一服务器,无法导入(可能有方法,但是项目紧,没时间去探索),于是想到另外一个方法:先上传EXCEL文件,然后读入DATASET放入GridView,然后从GridView提交到数据库,这样就没有问题。
gxlsystem.com,布布扣文件上传控件:<input id="myFile" runat="server" type="file" />
gxlsystem.com,布布扣Form里不需要enctype="multipart/form-data"
文件上传代码:
gxlsystem.com,布布扣            if (myFile.PostedFile.FileName != "")
gxlsystem.com,布布扣gxlsystem.com,布布扣            gxlsystem.com,布布扣{
gxlsystem.com,布布扣                //上传文件的绝对路径
gxlsystem.com,布布扣                string sFile = myFile.PostedFile.FileName;
gxlsystem.com,布布扣                //获取文件全名
gxlsystem.com,布布扣                sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
gxlsystem.com,布布扣                //获取后缀名
gxlsystem.com,布布扣                sFile = sFile.Substring(sFile.LastIndexOf("."));
gxlsystem.com,布布扣                if (sFile.ToLower() != ".xls")
gxlsystem.com,布布扣gxlsystem.com,布布扣                gxlsystem.com,布布扣{
gxlsystem.com,布布扣                    Response.Write("请选择Excel文件!");
gxlsystem.com,布布扣                    Response.End();
gxlsystem.com,布布扣                }
gxlsystem.com,布布扣                //为了防止重名,获得日期为文件名年月日时分秒毫秒
gxlsystem.com,布布扣                string datatime = System.DateTime.Now.ToString("yyyMMddHHmmssffff");
gxlsystem.com,布布扣                //上传后文件的新名
gxlsystem.com,布布扣                sFile = datatime + sFile;
gxlsystem.com,布布扣                //AppDomain.CurrentDomain.BaseDirectory.ToString() 获取此项目的根目录
gxlsystem.com,布布扣                //sPath 获取上传后的路径
gxlsystem.com,布布扣                string sPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "ExcelFiles\\" + sFile;
gxlsystem.com,布布扣                //上传文件
gxlsystem.com,布布扣                myFile.PostedFile.SaveAs(sPath);
gxlsystem.com,布布扣                this.myGridView.DataSource = GetExcelContent(sPath);
gxlsystem.com,布布扣                this.myGridView.DataBind();
gxlsystem.com,布布扣            }
读取EXCEL到DATASET代码:
gxlsystem.com,布布扣        private DataSet GetExcelContent(string filepath)
gxlsystem.com,布布扣gxlsystem.com,布布扣        gxlsystem.com,布布扣{
gxlsystem.com,布布扣            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=‘Excel 8.0;HDR=No;IMEX=1‘";
gxlsystem.com,布布扣            System.Data.OleDb.OleDbConnection myConn = new System.Data.OleDb.OleDbConnection(strCon);
gxlsystem.com,布布扣            string strCom = "SELECT F1 as resno,F2 as resname FROM [Sheet1$]";
gxlsystem.com,布布扣            myConn.Open();
gxlsystem.com,布布扣            System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, myConn);
gxlsystem.com,布布扣            //创建一个DataSet对象   
gxlsystem.com,布布扣            DataSet myDataSet = new DataSet();
gxlsystem.com,布布扣            //得到自己的DataSet对象   
gxlsystem.com,布布扣            myCommand.Fill(myDataSet);
gxlsystem.com,布布扣            //关闭此数据链接   
gxlsystem.com,布布扣            myConn.Close();
gxlsystem.com,布布扣            return myDataSet;
gxlsystem.com,布布扣        }
最后是数据提交到数据库代码:
gxlsystem.com,布布扣            string stresno = "";
gxlsystem.com,布布扣            string stresname = "";
gxlsystem.com,布布扣            foreach (GridViewRow row in this.myGridView.Rows)
gxlsystem.com,布布扣gxlsystem.com,布布扣            gxlsystem.com,布布扣{
gxlsystem.com,布布扣                Label txtesno = (Label)row.FindControl("labresno");
gxlsystem.com,布布扣                stresno += txtesno.Text.ToString().Trim().Replace("‘", "‘‘") + ";";
gxlsystem.com,布布扣
gxlsystem.com,布布扣                Label txtresname = (Label)row.FindControl("labresname");
gxlsystem.com,布布扣                stresname += txtresname.Text.ToString().Trim().Replace("‘", "‘‘") + ";";
gxlsystem.com,布布扣            }
gxlsystem.com,布布扣            Response.Write(stresno + "<br/>" + stresname);
gxlsystem.com,布布扣            Response.End();

热门排行

今日推荐

热门手游