zoukankan      html  css  js  c++  java
  • ASP.NET导入Excel到SQL数据库

    protected void btnChange_Click(object sender, EventArgs e)

            {

                UserInfoClass tClass = (UserInfoClass)Session["UserInfo"];

                string tLanguageType = tClass.Language;

    //获取文件路径

                string filePath = this.file1.PostedFile.FileName;

                if (filePath != "")

                {

                    if (filePath.Contains("xls"))//判断文件是否存在

                    {

                        InputExcel(filePath);

                    }

                    else

                    {

                        MessageBox.Show("请检查您选择的文件是否为Excel文件!谢谢!");

                    }

                }

                else

                {

                    MessageBox.Show("请先选择导入文件后,再执行导入!谢谢!");

                }

            }

            private void InputExcel(string pPath)

            {

                string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + pPath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";

                OleDbConnection oleCon = new OleDbConnection(conn);

                oleCon.Open();

                string Sql = "select * from [Sheet1$]";

                OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, oleCon);

                DataSet ds = new DataSet();

                mycommand.Fill(ds, "[Sheet1$]");

                oleCon.Close();

                int count = ds.Tables["[Sheet1$]"].Rows.Count;

                for (int i = 0; i < count; i++)

                {

                    string tUserID, tUserName, tDept, tEmail, tLeader, tAngent;

                    tUserID = ds.Tables["[Sheet1$]"].Rows[i]["員工代號"].ToString().Trim();

                    tUserName = ds.Tables["[Sheet1$]"].Rows[i]["員工姓名"].ToString().Trim();

                    tDept = ds.Tables["[Sheet1$]"].Rows[i]["所屬部門代號"].ToString().Trim();

                    tEmail= ds.Tables["[Sheet1$]"].Rows[i]["E-Mail Address"].ToString().Trim();

                    tLeader = ds.Tables["[Sheet1$]"].Rows[i]["直属主管"].ToString().Trim();

                    tAngent = ds.Tables["[Sheet1$]"].Rows[i]["代理人"].ToString().Trim();

                    string excelsql = "insert into " + this.UserInfo.Company + "..[resak] (resak001, resak002, resak015,resak005,resak013,resak009) values ('" + tUserID + "','" + tUserName + "','" + tDept + "','" + tEmail + "','" + tLeader + "','" + tAngent + "')";

                    DBCommand cmd = DscDBData.GetDataDBCommand();

                    cmd.ExeNonQuery(excelsql);

                }

            }

    当然此部分内容拿过来要稍作修改,比如最后的执行Insert语句的部分,等内容。

    上面完成了ASP.NET下导入Excel到数据库的功能。

    人生第一境,每日有所得。
  • 相关阅读:
    Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
    在终端中文本编辑器中显示行数:
    java环境配置:
    TypeError: 'MongoClient' object is not callable
    MongoDB安装
    多线程 -- 实现秒抓
    自动化测试淘宝美食
    自动化淘宝美食(在后台执行,不打开浏览器)
    新浪 抓取详情页
    WINDOWS 2003系统时间24小时制与12小时显示格式不一致问题与解决
  • 原文地址:https://www.cnblogs.com/a-cloud/p/4497422.html
Copyright © 2011-2022 走看看