xls表格導(dǎo)入數(shù)據(jù)庫(kù)功能實(shí)例代碼
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "shifou", "<script>alert('請(qǐng)您選擇 Excel文件')</script> ");
return;
}
string Name = FileUpload1.PostedFile.FileName; //獲取初始文件名
int j = Name.LastIndexOf("."); //取得文件名中最后一個(gè)"."的索引
string newext1 = Name.Substring(j); //獲取文件擴(kuò)展名
if (newext1 != ".xls" && newext1 != ".xlsx")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "wenjian", "<script>alert('只可以選擇 Excel文件')</script>");
return;//當(dāng)選擇的不是Excel文件時(shí),返回
}
SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
cn.Open();
//savePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);// FileUpload1.PostedFile.FileName.ToString();
if (FileUpload1.HasFile == false)//HasFile用來(lái)檢查FileUpload是否有指定文件
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "wenjian", "<script>alert('Excel路徑有 問(wèn)題')</script>");
return;//當(dāng)無(wú)文件時(shí),返回
}
filename = FileUpload1.FileName; //Path.GetFileNameWithoutExtension (FileUpload1.PostedFile.FileName);
savePath = Server.MapPath(("../../UploadFiles/xls/") + filename);
FileUpload1.SaveAs(savePath); //保存XLS文件
DataSet ds = ExecleDs(savePath, filename); //調(diào)用自定義方法
DataRow[] dr = ds.Tables[0].Select(); //定義一個(gè)DataRow數(shù)組
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
//當(dāng)Excel表為空時(shí),對(duì)用戶進(jìn)行提示
Page.ClientScript.RegisterStartupScript(this.GetType(), "kong", "<script>alert('Excel表為空表, 無(wú)數(shù)據(jù)!')</script>");
}
else
{
for (int i = 0; i < dr.Length; i++)
{
//自定義的model-student
student.belong = "";
student.birthday = Convert.ToDateTime("1990-1-1");
student.classid = 1;
student.id = 1;
student.inschool = Convert.ToDateTime("1990-1-1");
student.phone = "";
student.sex = "";
student.type = "";
student.isaudite = "否";
student.roleid = Convert.ToInt32("22");
try
{
student.stuname = dr[i]["姓名"].ToString();
student.stuid = dr[i]["學(xué)號(hào)"].ToString();
student.username = dr[i]["姓名"].ToString();
student.userpassword = dr[i]["學(xué)號(hào)"].ToString();
}
catch(Exception ex) {
Response.Write("<script>alert('"+ex.Message+"')</script>");
return;
}
DataSet ds2 = studentbll.GetList("stuid='" + dr[i]["學(xué)號(hào)"].ToString() + "'");
if (ds2.Tables[0].Rows.Count == 0)
{
try
{
bool addstudent = Convert.ToBoolean(studentbll.Add(student));
if (addstudent)
Page.ClientScript.RegisterStartupScript(this.GetType(), "daochu", "<script>alert('Excle表導(dǎo)入成功!')</script>");
}
catch (MembershipCreateUserException ex) //捕捉異常
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "chongfu", "<script>alert('導(dǎo)入內(nèi)容:" + ex.Message + "')</script> ");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "chongfu", "<script>alert ('內(nèi)容重復(fù)!禁止導(dǎo)入')</script> ");
continue;
}
}
}
}
public DataSet ExecleDs(string filenameurl, string table)
{
string strConn = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + filenameurl + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;'";//這段字符串注意空格別寫錯(cuò)否則會(huì)報(bào)錯(cuò)
OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet18$]", conn);//【sheet18$】是表格名
DataSet ds = new DataSet();
odda.Fill(ds, table);
return ds;
}
相關(guān)文章
asp.net 仿騰訊微薄提示 還能輸入*個(gè)字符 的實(shí)現(xiàn)代碼
asp.net 仿騰訊微薄提示 還能輸入*個(gè)字符 的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10
將DataRow轉(zhuǎn)成指定類型的類,并返回這個(gè)類的對(duì)象(帶值)
由于實(shí)際需要 將DataRow轉(zhuǎn)成指定類型的類,并返回這個(gè)類的對(duì)象(帶值) ,實(shí)現(xiàn)方法看下面的代碼。2008-04-04
aspxgridview CustomButtonCallback 不支持彈出消息提示解決方法
aspxgridveiw是devexpress的一個(gè)grid控件,使用起來(lái)還不錯(cuò),不能再 CustomButtonCallback 事件中使用response.write,具體的解決方法如下,感興趣的朋友可以參考下哈2013-06-06
.NET Core讀取配置文件方式詳細(xì)總結(jié)
這篇文章主要為大家詳細(xì)總結(jié)了.NET Core讀取配置文件方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
ASP.NET Core應(yīng)用錯(cuò)誤處理之DeveloperExceptionPageMiddleware中間件呈現(xiàn)“開發(fā)者
這篇文章主要給大家介紹了關(guān)于ASP.NET Core應(yīng)用錯(cuò)誤處理之DeveloperExceptionPageMiddleware中間件呈現(xiàn)“開發(fā)者異常頁(yè)面”的相關(guān)資料,需要的朋友可以參考下2019-01-01
使用jQuery Uploader顯示文件上傳進(jìn)度
在上傳大文件的時(shí)候, 我們都希望可以向用戶顯示上傳的進(jìn)度, 那么這里介紹的就是使用 jQuery Uploader 來(lái)實(shí)現(xiàn)這樣的效果2011-12-12
asp.net(C#)函數(shù)對(duì)象參數(shù)傳遞的問(wèn)題
我們知道在.net里class是引用類型,在函數(shù)參數(shù)表中的對(duì)象傳遞的都是對(duì)象的引用,所以在函數(shù)體內(nèi)對(duì)其對(duì)象參數(shù)的修改會(huì)影響函數(shù)外對(duì)應(yīng)的對(duì)象本身,例如下面的程序.2009-12-12

