zoukankan      html  css  js  c++  java
  • 将xls文件导入数据库

    一点点代码,上传xls,然后将它导入到数据库相应的表

    protected void Click()
        {
            string postName=file1.PostedFile.FileName;
            if (postName == "")
            {
                Response.Write("<script>alert('请先选择文件.');location='add.aspx';</script>");
            }
            else
            {
                string str = this.UpLoadFile();
                if (str == "Error")
                {
                    Response.Write("<script>alert('上传xls文件失败.');location='add.aspx';</script>");
                }
                else
                {
                    string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = "+str+";Extended Properties=Excel 8.0";
                    OleDbConnection cnnxls = new OleDbConnection(mystring);
                    OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [Sheet1$]", cnnxls);
                    DataSet myDs = new DataSet();
                    myDa.Fill(myDs);
                    if (myDs.Tables[0].Rows.Count > 0)
                    {
                        string strSql = "";
                        string CnnString = "Provider=SQLOLEDB;database=testnews;server=(local);uid=sa;pwd=";
                        OleDbConnection conn = new OleDbConnection(CnnString);
                        conn.Open();
                        OleDbCommand myCmd = null;
                        for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
                        {
                            strSql = "insert into website(sname,surl) values ('";
                            strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";
                            strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "')";
                            try
                            {
                                myCmd = new OleDbCommand(strSql, conn);
                                myCmd.ExecuteNonQuery();
                                Response.Write("<script language=javascript>alert('数据导入成功.');location='index.aspx';</script>");
                            }
                            catch
                            {
                                Response.Write("<script language=javascript>alert('数据导入失败.');location='add.aspx';</script>");
                            }
                        }
                    }
                 }
            }

    噢耶游戏是中国最大的轻社交游戏开发商,致力于手机页游的研发及推广业务。我们首创性地提出了HTML5游戏中心思路,在第三方App 中嵌入式休闲游戏,为开发者提供了全新的应用内游戏解决方案。
  • 相关阅读:
    scrapy 多个爬虫运行
    scrapy不抓取重复的网页解决办法
    centos7 安装mysql
    20个免费和开源数据可视化工具
    转载 CSDN 谈谈我对证券公司一些部门的理解(前、中、后台)
    scrapy 教程
    django 常见错误汇总
    python 常见错误
    python 基础知识整理
    使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件
  • 原文地址:https://www.cnblogs.com/yintian2/p/914765.html
Copyright © 2011-2022 走看看