<a href="javascript:void(0);" onclick="toimport();">上传名单</a> <script type="text/javascript"> function toimport() { $('#newwin').window({ width: 800, height: 300, title: '导入员工', href: 'ImportEmp.aspx', modal: true }); } </script>
页面代码:
<table cellpadding="4" cellspacing="0" border="1" width="100%" style="border-collapse: collapse; font-size:11pt; background-color:#e7eaf1;" > <tr> <td align="right" class="style1" ><span lang="zh-cn">提示:</span></td> <td align="left" bgcolor="white" colspan="7"> <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" ForeColor="Red" Text="请按模板格式填表!"></asp:Label> </td> </tr> <tr> <td align="right" class="style1" ><span lang="zh-cn">员工名单导入:</span></td> <td align="left" bgcolor="white" colspan="7"> <asp:FileUpload ID="fuExcel" runat="server" /> <asp:Button ID="btn_import" runat="server" Text="确定导入" onclick="btn_import_Click" UseSubmitBehavior="False" /> </td> </tr> <tr> <td align="left" colspan="8" style="font-size:12pt;" > <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="没有可显示的数据记录。" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"> <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> <Columns> <asp:TemplateField HeaderText="编号" SortExpression="pID"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("pID") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("pID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="pName" HeaderText="姓名" SortExpression="pName" /> <asp:TemplateField HeaderText="密码" SortExpression="ppwd"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ppwd") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("ppwd") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="psex" HeaderText="性别" SortExpression="psex" /> <asp:BoundField DataField="pbirth" HeaderText="出生日期" SortExpression="pbirth" /> <asp:BoundField DataField="pwork" HeaderText="职务" SortExpression="pwork" /> <asp:BoundField DataField="pdepartment" HeaderText="部门" SortExpression="pdepartment" /> <asp:BoundField DataField="pworktime" HeaderText="工作时间" SortExpression="pworktime" /> <asp:BoundField DataField="pblood" HeaderText="血型" SortExpression="pblood" /> <asp:BoundField DataField="phealth" HeaderText="健康状况" SortExpression="phealth" /> <asp:BoundField DataField="pmarriage" HeaderText="婚姻状况" SortExpression="pmarriage" /> <asp:BoundField DataField="premark" HeaderText="备注" SortExpression="premark" /> </Columns> <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="#DCDCDC" /> </asp:GridView> </td> </tr> <tr> <td align="center" colspan="8" > <asp:Button ID="btn_uploadDB" runat="server" Text="提交到数据库" onclick="btn_uploadDB_Click" UseSubmitBehavior="False" /> </td> </tr> </table>
ps:这个页面单独拿出来功能是没问题的.弹出窗口是jeasyui实现的.
后台代码也贴一下吧:
求解救
protected void Page_Load(object sender, EventArgs e) { } protected void btn_import_Click(object sender, EventArgs e) { bool b = Upload(fuExcel); // 上传excel文件 if (!b) { return; } string name = fuExcel.FileName; string filepath = Server.MapPath("~/upload/") + name; DataSet ds = ExcelDataSource(filepath, ExcelSheetName(filepath)[0].ToString()); //导入的EXCEL表的列名和GV中绑定列名不相同,不能正确绑定,需要处理才好 DataTable modiTable = ds.Tables[0]; //modiTable.Columns[0].ColumnName = "tid"; modiTable.Columns[0].ColumnName = "pID"; modiTable.Columns[1].ColumnName = "pName"; modiTable.Columns[2].ColumnName = "ppwd"; modiTable.Columns[3].ColumnName = "psex"; modiTable.Columns[4].ColumnName = "pbirth"; modiTable.Columns[5].ColumnName = "pwork"; modiTable.Columns[6].ColumnName = "pdepartment"; modiTable.Columns[7].ColumnName = "pworktime"; modiTable.Columns[8].ColumnName = "pblood"; modiTable.Columns[9].ColumnName = "phealth"; modiTable.Columns[10].ColumnName = "pmarriage"; modiTable.Columns[11].ColumnName = "premark"; GridView1.DataSource = modiTable; GridView1.DataBind(); } //上传文件方法 private bool Upload(FileUpload myFileUpload) { bool flag = false; //是否允许上载 bool fileAllow = false; //设定允许上载的扩展文件名类型 string[] allowExtensions = { ".xls" }; //取得网站根目录路径 string path = HttpContext.Current.Request.MapPath("~/upload/"); if (myFileUpload.HasFile) { string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower(); for (int i = 0; i < allowExtensions.Length; i++) { if (fileExtension == allowExtensions[i]) { fileAllow = true; } } if (fileAllow) { try { //存储文件到文件夹 myFileUpload.SaveAs(path + myFileUpload.FileName); //lblMes.Text = "文件导入成功"; flag = true; } catch (Exception ex) { // lblMes.Text += ex.Message; flag = false; } } else { // lblMes.Text = "不允许上载:" + myFileUpload.PostedFile.FileName + ",只能上传xls的文件,请检查!"; flag = false; } } else { //lblMes.Text = "请选择要导入的excel文件!"; flag = false; } return flag; } //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径, sheetname为excel文件中的表名 public DataSet ExcelDataSource(string filepath, string sheetname) { string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "]", strConn); DataSet ds = new DataSet(); oada.Fill(ds); conn.Close(); return ds; } //获得Excel中的所有sheetname。 public ArrayList ExcelSheetName(string filepath) { ArrayList al = new ArrayList(); string strConn; strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataTable sheetNames = conn.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); conn.Close(); foreach (DataRow dr in sheetNames.Rows) { al.Add(dr[2]); } return al; } protected void btn_uploadDB_Click(object sender, EventArgs e) { //提交到数据库tab_acc中去 SqlConnection con = BaseDate.returnSqlCon(); SqlTransaction trans = null; SqlCommand cmd = new SqlCommand(); if (GridView1.Rows.Count <= 0) return; try { con.Open(); trans = con.BeginTransaction(); for (int i = 0; i < GridView1.Rows.Count; i++) { int gpID = int.Parse(((Label)GridView1.Rows[i].FindControl("Label1")).Text.ToString().Trim()); string gpName = GridView1.Rows[i].Cells[1].Text.ToString().Trim(); String gpwd =((Label)GridView1.Rows[i].FindControl("Label2")).Text.ToString().Trim(); string gpsex = GridView1.Rows[i].Cells[3].Text.ToString().Trim(); DateTime gpbirth = DateTime.Parse(GridView1.Rows[i].Cells[4].Text.ToString().Trim()); String gpwork = GridView1.Rows[i].Cells[5].Text.ToString().Trim(); String gpdepartment = GridView1.Rows[i].Cells[6].Text.ToString().Trim(); DateTime gpworktime = DateTime.Parse(GridView1.Rows[i].Cells[7].Text.ToString().Trim()); String gpblood = GridView1.Rows[i].Cells[8].Text.ToString().Trim(); String gphealth = GridView1.Rows[i].Cells[9].Text.ToString().Trim(); String gpmarriage = GridView1.Rows[i].Cells[10].Text.ToString().Trim(); String gpremark = GridView1.Rows[i].Cells[11].Text.ToString().Trim(); Employee emp = new Employee(); emp.AccId = BaseDate.getMaxId("tab_acc", "acc_id"); emp.AccName = gpName; emp.AccLoginName = gpName; emp.AccPassWord = gpwd; emp.AccSex = gpsex; emp.Birthday = gpbirth; emp.Work = gpwork; department dep = new department(); dep.DepartmentName = gpdepartment; if ("开发".Equals(gpdepartment)) { dep.DepartmentId = "1"; } else if ("市场".Equals(gpdepartment)) { dep.DepartmentId = "2"; } else if ("运维".Equals(gpdepartment)) { dep.DepartmentId = "3"; } emp.Department = dep; emp.EmpTime = gpworktime; emp.Blood = gpblood; emp.Marriage = gpmarriage; emp.Remark = gpremark; emp.Role = "0"; Record rec = new Record(); rec.RecordId = BaseDate.getMaxId("tab_healthyRecord", "rec_id"); rec.Result = gphealth; rec.Emp = emp; if (EmpAction.addOne(emp) > 0) { HealthyRecordAction.addOne(rec); } } trans.Commit(); btn_uploadDB.Enabled = false; Response.Write("<script language = 'javascript'> alert('恭喜您:数据提交成功!') </script>"); } catch (Exception exc) { trans.Rollback(); Response.Write("<script language = 'javascript'> alert('对不起:数据提交出错,原因是:" + exc.Message + "!') </script>"); } finally { con.Close(); } }
去掉href: 'ImportEmp.aspx'
去掉herf,弹出窗怎么加载这个页面?
@只能穷开心: 把确认导入的后台代码注释掉,再点下看看
在importEmp.aspx后面传递上参数,另外一个页面取值,然后赋值到页面元素上面。
importEmp.aspx?x1=&x2=....
或者post提交过去。。