|
|
#4
哭泣的狼2007-03-22 17:41
小子,这是全部代码?接好了 这是我创建的一个类的全部内容, using System; using System.Data; using System.Data.OleDb; using System.Collections;
namespace localhost { /// <summary> /// myData 的摘要说明。 /// </summary> public class myData { /// <summary> /// 连接 Access 数据库的连接字符串 /// </summary> private OleDbConnection conn=new OleDbConnection(); public myData() {
string path=System.Web.HttpContext.Current.Server.MapPath("db\\mydb.mdb"); conn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+path+";"; // // TODO: 在此处添加构造函数逻辑 // }
/// <summary> /// 查找 Excel 中工作簿的名称 /// </summary> /// <param name="filePath">Excel 文件在服务器端的绝对路径</param> /// <returns></returns> public ArrayList ExcelBookName(string filePath) { ArrayList arr=new ArrayList(); //连接 Excel 的连接字符串 OleDbConnection ExcelConn=new OleDbConnection(); ExcelConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"; ExcelConn.Open(); DataTable dt = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null,null}); ExcelConn.Close();
int n=dt.Rows.Count; for(int i=0;i<n;i++)//返回 Excel 表的工作簿名称 { arr.Add(dt.Rows[i][2].ToString()); } return arr; }
/// <summary> ///将Excel 表中指定工作簿的数据添加进 access 数据库中 /// </summary> /// <param name="filePath">服务器端Excel 表格的绝对路径</param> /// <param name="bookName">Excel 中工作簿的名称</param> /// <returns></returns> public bool addUser(string filePath,string bookName) { try { //将Excel 表格中的信息查找出来,并存放到DataTable 中 OleDbConnection ExcelConn=new OleDbConnection(); ExcelConn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filePath+";Extended Properties=Excel 8.0;"; string sql="select * from ["+bookName+"$]"; OleDbDataAdapter da=new OleDbDataAdapter(sql,ExcelConn); DataTable dt=new DataTable(); da.Fill(dt);
//循环将Excel 表中指定工作簿的数据添加进 access 数据库中 for(int i=0;i<dt.Rows.Count;i++) { sql="insert into [user](编号,用户名,密码) values(@id,@uname,@pwd)"; OleDbCommand comm=new OleDbCommand(sql,conn); comm.Parameters.Add("@id",Convert.ToInt16(dt.Rows[i][0])); comm.Parameters.Add("@uname",dt.Rows[i][1].ToString()); comm.Parameters.Add("@pwd",dt.Rows[i][2].ToString()); conn.Open(); comm.ExecuteNonQuery(); conn.Close(); } return true; } catch(Exception ee) { conn.Close(); string str=ee.ToString(); return false; } }
} } 这是窗体页面的代码,有些是调用上面类中的方法
using System; using System.Collections; using System.ComponentModel; using System.Data; 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.IO;
namespace localhost { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.HtmlControls.HtmlInputFile ff; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Panel Panel1; protected System.Web.UI.WebControls.DropDownList ddl; protected System.Web.UI.HtmlControls.HtmlInputButton Sub; private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 Sub.Attributes.Add("onclick","return sel();"); Button1.Attributes.Add("onclick","return comSel();");
}
#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Sub.ServerClick += new System.EventHandler(this.Sub_ServerClick); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void Sub_ServerClick(object sender, System.EventArgs e) { string fName=ff.PostedFile.FileName; //得到服务器端 Excel 文件夹的绝对路径 string path=Server.MapPath("Excel\\"); //存储文件后缀名 string back="";
if(fName!="") { //得到文件名称 back=fName.Substring(fName.LastIndexOf(".")); fName=DateTime.Now.ToString(); fName=fName.Replace(" ","");//将时间中的 空格 替换为“” fName=fName.Replace("-","");//将时间中的“-”替换为“” fName=fName.Replace(":","");//将时间中的“:”替换为“”
//将选中文件上传到服务器指定文件夹 //Response.Write(path+fName+back); ff.PostedFile.SaveAs(path+fName+back); string filePath=path+fName+back;//服务器端刚上传文件的绝对路径 Session["filePath"]=filePath;//记录服务器端刚上传文件的绝对路径
combox(filePath);
this.Panel1.Visible=true; this.Button1.Visible=true; }
}
//创建一个下拉列表 DropDownList //private DropDownList ddl=new DropDownList();
/// <summary> /// 添加带有选择工作簿的下拉列表 /// </summary> private void combox(string filePath) { myData md=new myData();
//查找上传的工作表中所有工作簿名称 ArrayList arr=md.ExcelBookName(filePath); //将工作簿名称添进下拉列表中 ddl.Items.Clear(); for(int i=0;i<arr.Count;i++) { string temp=arr[i].ToString(); temp=temp.Replace("$","");//工作簿名称后自带的"$"符号,为了好看将它去掉 ddl.Items.Add(temp); }
// Panel1.Controls.Add(Label1); // Panel1.Controls.Add(ddl);//将下拉列表添加进panel(面板) 控件中显示 // Panel1.Controls.Add(Button1); }
private void Button1_Click(object sender, System.EventArgs e) { myData md=new myData(); string filePath=Session["filePath"].ToString(); string bookName=ddl.SelectedItem.Text; if(md.addUser(filePath,bookName)) { Panel1.Visible=false; Response.Write("追加成功!"); } else { Panel1.Visible=true; Response.Write("追加失败!"); }
}
} } 你还要在根目录下创建一个名为[Excel]的文件夹,并且在文件夹属性-安全中加入Everyone的
我的邮箱地址:www.hukelanyanan@163.com
|