.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();
    }
}

posted @ 2012-03-23 10:20  甜菜波波  阅读(299)  评论(0编辑  收藏  举报