• .NET读excl数据


    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.OleDb;
    using System.Data.SqlClient;
    public partial class sql : System.Web.UI.Page
    {
        public static string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + HttpContext.Current.Server.MapPath("~/App_Data/excl.xls") + "';Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";

      public static OleDbConnection con = new OleDbConnection(ConnStr);
      public static string sqlconstr = @"server=.; database=yoy_.312; uid=sa; pwd=j;";
      public static SqlConnection sqlcon = new SqlConnection(sqlconstr);

     
        protected void Page_Load(object sender, EventArgs e)
        {
           
           
        }

        public static void createcmd(string str)
        {
            SqlCommand cmd = new SqlCommand(str,sqlcon);
            if (sqlcon.State.ToString()== "Close")
            {
                sqlcon.Open();
            }
            else
            {

                sqlcon.Open();
            }
            cmd.ExecuteNonQuery();
            sqlcon.Close();
            
        
        }


        public void bind()
        {
            string p_tagName = "disijie";
            string sql = string.Format("select * from [{0}$]", p_tagName);

            DataSet ds = new DataSet();
            OleDbDataAdapter sqld = new OleDbDataAdapter(sql,con);
            con.Open();
            sqld.Fill(ds);
            con.Close();
          
            int i = 0;

            string sql1 = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string sheng = dr[1].ToString();
                string shi = dr[2].ToString();

                string quxian = dr[3].ToString();
                string xiang = dr[4].ToString();
                string name = dr[5].ToString();
                string address = dr[6].ToString();
                string lianxiren = dr[7].ToString();
                int quhao = 0;
                string phone = dr[9].ToString();
                string mobile = dr[10].ToString();
                string youbian = dr[11].ToString();
                string email = dr[13].ToString();
               string uid = dr[14].ToString();
               string pwd = dr[15].ToString();
                sql1 = string.Format("insert into youeryuan(sheng,shi,quxian,name,address,lianxiren,quhao,phone,mobile,youbian,email,uid,pwd,xiang) values('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}','{8}','{9}','{10}','{11}','{12}','{13}')", sheng, shi, quxian, name, address, lianxiren, quhao, phone, mobile, youbian, email, uid, pwd, xiang);
                createcmd(sql1);

            

              
            }

            Response.Write("添加成功!");

           
        
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            bind();
        }
    }

  • 相关阅读:
    日期型数据知识
    如何让VS检查函数和类Comment的添加情况
    HTTP request is unauthorized with client authentication scheme 'Anonymous'.
    将SerializableAttribute序列化为xml
    使用EnterpriseLibrary Validation Block对WCF做验证
    表达式树中递归方法
    使用SignalR在Asp.net中实现进度条
    SQLServer中列出数据库的所有表的创建时间
    用Knockoutjs与Asp.net MVC实现级联下拉列表
    使用UnityAutoMoq简化单元测试
  • 原文地址:https://www.cnblogs.com/tiancai/p/2413226.html
Copyright © 2020-2023  润新知