有个需求是Excel的记录导入到数据表,但是Excel列数不定,需要从标题行选择导入到数据表的哪个字段中(就像sql的导入功能):
请教,我从Excel导入至Dataset,然后动态绑定到GridView,怎样在标题行动态的添加dropdownlist(dropdownlist里面的选择项固定)呢?谢谢
请教,我从Excel导入至Dataset,然后动态绑定到GridView,怎样在标题行动态的添加dropdownlist(dropdownlist里面的选择项固定)呢?谢谢
调试欢乐多
{
if (FileUpload1.HasFile == false)
{
Response.Write("<script>alter('请选择Excel文件')</scirpt>");
return;
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
if (IsXls != ".xls")
{
Response.Write("<script>alter('只可以选择Excel文件')</scirpt>");
return;
}
SqlConnection con = new SqlConnection(str);
con.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
string savepath = Server.MapPath(("~\\updown\\")+filename);
FileUpload1.SaveAs(savepath);
DataSet ds = ExcleDs(savepath,filename);
DataRow[] dr = ds.Tables[0].Select();
int rowsum = ds.Tables[0].Rows.Count;
if (rowsum == 0)
{ Response.Write("<script>alter('Excel为空数据')</scirpt>"); }
else
{
for (int i = 0; i < dr.Length; i++)
{
string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
string hhaspx_xm = dr[i]["姓名"].ToString();
string hhaspx_dx = dr[i]["底薪"].ToString();
string hhaspx_kh = dr[i]["考核"].ToString();
string hhaspx_jl = dr[i]["奖励"].ToString();
string hhaspx_jt = dr[i]["津贴"].ToString();
string hhaspx_jb = dr[i]["加班"].ToString();
string hhaspx_zb = dr[i]["值班"].ToString();
string hhaspx_jx = dr[i]["绩效"].ToString();
string hhaspx_hj = dr[i]["合计"].ToString();
string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'"; //检查用户是否存在
SqlCommand sqlcmd = new SqlCommand(sqlcheck, con);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')"; SqlCommand cmd = new SqlCommand(insertstr, con);
try
{
cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex) //捕捉异常
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}
}
else
{
Response.Write("<script>alert('内容重复!禁止导入');location='default.aspx'</script></script> ");
continue;
} }
Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
}
con.Close();
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
this.gv1.DataSource = new string[] { "1", "2", "3" };
this.gv1.DataBind();
} protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
DropDownList ddl = new DropDownList();
ddl.Items.Add("11");
ddl.Items.Add("22"); e.Row.Cells[0].Controls.Add(ddl);
}
}