excel 導入數據庫的代碼

excel导入数据库的代码 ......
excel导入数据库的代码
using XFXOA;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data .OleDb ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
namespace OA
{



public class ExceltoAccess : System.Web.UI.Page
{



OAConfig OAConfig1=new OAConfig();
private void Page_Load(object sender, System.EventArgs e)
{
//try{if(!OAConfig1.LogonChk((Request.Cookies["logonyn"].Value).ToString()))Response.Redirect("/logon_wrong.aspx");}catch{Response.Redirect("/logon_wrong.aspx");}

//if (!IsPostBack)
//{
try
{

OleDbDataReader dr1 = OAConfig1.MyReaderexcel("/source_file.xls","00",3,"SELECT * FROM [空间$]");
if(dr1.Read())
{
do
{
string ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("/targetdb.mdb")+";Jet OLEDB:Database Password=;";
OleDbConnection Connother = new OleDbConnection(ConnString);
if (Connother.State == ConnectionState.Closed)Connother.Open();
OleDbCommand MyComm=new OleDbCommand("insert into 空间 (A,B,C,D,E,F,G) values ('"+dr1[0].ToString()+"','"+dr1[1].ToString()+"','"+dr1[2].ToString()+"','"+dr1[3].ToString()+"','"+dr1[4].ToString()+"','"+dr1[5].ToString()+"','"+dr1.ToString()+"')", Connother);
MyComm.ExecuteNonQuery();
MyComm.Dispose();
Connother.Close();
}
while(dr1.Read());

}
else
{
Response.Write("此Excel表是空的");
}
dr1.Close();
}
finally
{

OAConfig1.CloseConnexcel();

}
Response.Write("此Excel导入ACCESS成功");

}



//public void Button1_click(object sender, System.EventArgs e)
//{




//}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
//this.Button1.Click += new System.EventHandler(this.Button1_click);
//this.Button2.Click += new System.EventHandler(this.Button2_click);
}
#endregion
}
}
http://www.fishpro.cn/post/23.html

你可能感兴趣的:(Excel)