protected void uploadBtn_Click(object sender, EventArgs e) { classdetailBLL ClassDetailBLL = new classdetailBLL(); int result = ClassDetailBLL.Add(this, excelFU); if (result == 0) MessageBox.Show(this, "上传失败!"); else MessageBox.ShowAndRedirect(this, "成功导入" + result + "门课程信息","class.aspx"); }
/// <summary> /// 增加excel数据库 /// </summary> public int Add(Page page, FileUpload fu) { classtypeBLL ClassTypeBLL=new classtypeBLL(); classdetailBLL ClassDetailBLL = new classdetailBLL(); try { int i = 0; if (fu.HasFile == false) { MessageBox.Show(page, "请选择您要上传的Excel文件!"); return 0;//当无文件时,返回 } string IsXls = System.IO.Path.GetExtension(fu.FileName).ToString().ToLower(); if (IsXls != ".xls") { MessageBox.Show(page, "只可以上传Excel文件!"); return 0;//当选择的不是Excel文件时,返回 } string path = page.Server.MapPath("storage/classInput/"); string strpath = fu.PostedFile.FileName.ToString(); //获取Execle文件路径 string filename = "批量课程信息" + System.DateTime.Now.ToString("yyyyMMddHHmmss").Trim() + ".xls"; //从时间获取文件路径 fu.PostedFile.SaveAs(path + filename); DataSet ds = Tools.ExecleDs(path + filename, filename); DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { //Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); MessageBox.Show(page, "Excel表为空表,无数据!");//当Excel表为空时,对用户进行提示 return 0; } else { for (i = 0; i < dr.Length; i++) { classdetail ClassDetail = new classdetail(); string name = dr[i]["课程名称"].ToString(); if (name.Length < 1) { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程名称不能为空,请检查数据"); return i; } else ClassDetail.name = name; int classType=0; try { classType = Convert.ToInt32(dr[i]["课程类别"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程类别必须为数字,请检查数据"); return i; } if (!(ClassTypeBLL.Exists(classType.ToString()))) { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程类别不存在,请检查数据"); return i; } else ClassDetail.type = classType; ClassDetail.teacher = dr[i]["教师姓名"].ToString(); try { ClassDetail.startTime = Convert.ToDateTime(dr[i]["开始时间"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,开始时间格式不正确,请确保为“yyyy/mm/dd hh:mm:ss”,请检查数据"); return i; } try { ClassDetail.endTime = Convert.ToDateTime(dr[i]["结束时间"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,结束时间格式不正确,请确保为“yyyy/mm/dd hh:mm:ss”,请检查数据"); return i; } ClassDetail.period = dr[i]["学时"].ToString(); ClassDetail.credit = dr[i]["学分"].ToString(); ClassDetail.location = dr[i]["开课地点"].ToString(); ClassDetail.remark = dr[i]["备注"].ToString(); ClassDetailBLL.Add(ClassDetail); } return i ; } } catch { return 0; } finally { } }
protected void addStudentBtn_Click(object sender, EventArgs e) { classdetail ClassDetail = new classdetail(); if (isTextBoxEmpty(nameTB, "课程名称")) ClassDetail.name = getText(nameTB); else return; ClassDetail.type = Convert.ToInt32(typeDDL.SelectedItem.Value); if (isTextBoxEmpty(teacherTB, "教师姓名")) ClassDetail.teacher= getText(teacherTB); else return; string startTimeStr = ""; if (isTextBoxEmpty(startYearTB, "开始时间-年")) { string year = getText(startYearTB); if (year.Length != 4) { MessageBox.Show(this, "开始时间-年:格式不正确!"); return; } startTimeStr += year+"-"; } if (isTextBoxEmpty(startMonthTB, "开始时间-月")) { int month = Convert.ToInt32(getText(startMonthTB)); if (month<1||month>12) { MessageBox.Show(this, "开始时间-月:格式不正确!"); return; } if (month < 10) startTimeStr += "0" + month.ToString()+"-"; else startTimeStr += month.ToString()+"-"; } if (isTextBoxEmpty(startDayTB, "开始时间-日")) { int day = Convert.ToInt32(getText(startDayTB)); if (day < 1 || day > 31) { MessageBox.Show(this, "开始时间-日:格式不正确!"); return; } if(day<10) startTimeStr += "0"+day.ToString(); else startTimeStr += day.ToString() ; } ClassDetail.startTime=Convert.ToDateTime(startTimeStr); string endTimeStr = ""; if (isTextBoxEmpty(endYearTB, "结束时间-年")) { string year = getText(endYearTB); if (year.Length != 4) { MessageBox.Show(this, "结束时间-年:格式不正确!"); return; } endTimeStr += year + "-"; } if (isTextBoxEmpty(endMonthTB, "结束时间-月")) { int month = Convert.ToInt32(getText(endMonthTB)); if (month<1||month>12) { MessageBox.Show(this, "结束时间-月:格式不正确!"); return; } if (month < 10) endTimeStr += "0" + month.ToString() + "-"; else endTimeStr += month.ToString() + "-"; } if (isTextBoxEmpty(endDayTB, "结束时间-日")) { int day = Convert.ToInt32(getText(endDayTB)); if (day < 1 || day > 31) { MessageBox.Show(this, "结束时间-日:格式不正确!"); return; } if (day < 10) endTimeStr += "0" + day.ToString(); else endTimeStr += day.ToString(); } ClassDetail.endTime=Convert.ToDateTime(endTimeStr); if (isTextBoxEmpty(periodTB, "学时")) ClassDetail.period = getText(periodTB); else return; if (isTextBoxEmpty(creditTB, "学分")) ClassDetail.credit = getText(creditTB); else return; if (isTextBoxEmpty(locationTB, "开课地点")) ClassDetail.location = getText(locationTB); else return; ClassDetail.remark=getText(remarkTB); try { classdetailBLL ClassDetailBLL = new classdetailBLL(); ClassDetailBLL.Add(ClassDetail); } catch(Exception ex) { MessageBox.Show(this, "添加失败!"); Console.WriteLine(ex.Message); } MessageBox.Show(this, "添加成功!"); }