|
一个sheet表导入能够实现,能够正确导入。但多个sheet表导入,发现如果不读列名,程序只读取sheet1中的数据,然后导入第二个sheet表对应的数据库中。之所以不读列名,是因为sheet1和sheet2中有相同的列名存在,读列名的话两个表的列名不能有相同的。
代码如下:
namespace ReportImport
{
public partial class Import : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
OleDbConnection ExcelConn = new OleDbConnection(strCon);
try
{
string strCom = string.Format("SELECT * FROM [Sheet1$],[Sheet2$]");
ExcelConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[" + tableName + "$]");
ExcelConn.Close();
return ds;
}
catch
{
ExcelConn.Close();
return null;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
{
Response.Write("<script>alert('请您选择Excel文件')</script> ");
return;//当无文件时,返回
}
string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
if (IsXls != ".xls")
{
Response.Write("<script>alert('只可以选择Excel文件')</script>");
return;//当选择的不是Excel文件时,返回
}
SqlConnection cn = new SqlConnection(strConn);
cn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;//获取Execle文件名 DateTime日期函数
string savePath = Server.MapPath(("upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径
FileUpload1.SaveAs(savePath); //SaveAs 将上传的文件内容保存在服务器上
DataSet ds = ExcelSqlConnection(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 = 1; i < dr.Length; i++)
{
string hhaspx_rq = dr[i][0].ToString();
string hhaspx_bh = dr[i][1].ToString();
string hhaspx_xm = dr[i][2].ToString();
string hhaspx_dx = dr[i][3].ToString();
string hhaspx_kh = dr[i][4].ToString();
string hhaspx_jl = dr[i][5].ToString();
string hhaspx_jt = dr[i][6].ToString();
string hhaspx_jb = dr[i][7].ToString();
string hhaspx_zb = dr[i][8].ToString();
string hhaspx_jx = dr[i][9].ToString();
string hhaspx_hj = dr[i][10].ToString();
string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'"; //检查用户是否存在
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 + "')";
SqlCommand cmd = new SqlCommand(insertstr, cn);
try
{
cmd.ExecuteNonQuery();
}
catch (MembershipCreateUserException ex) //捕捉异常
{
Response.Write("<script>alert('导入内容:" + ex.Message + "')</script>");
}
}
/**
else
{
Response.Write("<script>alert('内容重复!禁止导入!');location='default.aspx'</script></script> ");
continue;
}
**/
}
for (int j = 1; j < dr.Length; j++)
{
string 班级 = dr[j][0].ToString();
string 姓名 = dr[j][1].ToString();
string 就业标志 = dr[j][2].ToString();
string 用人单位名称 = dr[j][3].ToString();
string 现在的就业单位 = dr[j][4].ToString();
string sl1 = dr[j][5].ToString();
string je1 = dr[j][6].ToString();
string sl2 = dr[j][7].ToString();
string je2 = dr[j][8].ToString();
string sqlcheck = "select count(*) from hhaspx_tj where 班级='" + 班级 + "'And 姓名='" + 姓名 + "'"; //检查用户是否存在
SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into hhaspx_tj (班级,姓名,就业标志,用人单位名称,现在的就业单位,sl1,je1,sl2,je2) values('" + 班级 + "','" + 姓名 + "','" + 就业标志 + "','" + 用人单位名称 + "','" + 现在的就业单位 + "','" + sl1 + "','" + je1 + "','" + sl2 + "','" + je2 + "')";
SqlCommand cmd = new SqlCommand(insertstr, cn);
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表导入成功!');</script>");
cn.Close();
}
}
}
求指教! |
|