excel导入数据库sql server access
excel导入数据库sql server access
将上传的文件内容保存在服务器上
DataSet ds = ExecleDs(savePath, filename); //调用自定义方法
DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组
int rowsnum = ds.Tables[0].Rows.Count;
if (rowsnum == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); //当Excel表为空时,对用户进行提示
}
else
{
for (int i = 0; i < dr.Length; i++)
{
// string hhaspx_rq = dr[i]["日期"].ToString();//日期 excel列名【名称不能变,否则就会出错】
// string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
string Tnamexm = dr[i]["姓名"].ToString();
string Tagenl=dr[i]["年龄"].ToString();
string Taddressdz=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 + "'"; //检查用户是否存在
string sqlcheck = "select count(*) from IntoExcel where Tname='"+Tnamexm+"'";
SqlCommand sqlcmd = new SqlCommand(sqlcheck,cn);
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 + "')";
string insertstr = "insert into IntoExcel (Tname,Tage,Taddress) values('"+Tnamexm+"','"+Tagenl+"','"+Taddressdz+"')";
SqlCommand cmd = new SqlCommand(insertstr, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex) //捕捉异常
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}
}
else
{
Response.Write("<script>alert('内容


