把excel倒进SQL里
小弟刚刚要弄这个功能,上网找了个代码来改下,上传功能好似没问题,但单击Buttion后,页面就弹出您指定的网页无法访问! 我已经纠结好久了!!!!!一直在线上等答案,小弟是新手,麻烦说得具体点!!!!!(我按F11都没逐步运行,不知道哪里出错了)
[code=C#][/code] public DataSet ExecleDs(string filenameurl, string table)
{
DataSet ds = new DataSet();
string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [学生数据$]", conn);
odda.Fill(ds, table);
return ds;
}
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 = 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 Sid = dr[i]["Sid"].ToString();//日期 excel列名【名称不能变,否则就会出错】
string SNO = dr[i]["SNO"].ToString();//编号 列名 以下类似
string Spassword = dr[i]["Spassword"].ToString();
string Sname = dr[i]["Sname"].ToString();
string Semail = dr[i]["Semail"].ToString();
string Sclass = dr[i]["Sclass"].ToString();
string Sspecialty = dr[i]["Sspecialty"].ToString();
string authority = dr[i]["authority"].ToString();
string sqlcheck = "select count(*) from Student where Sid='" + Sid + "'"; //检查用户是否存在
SqlCommand sqlcmd = new SqlCommand(sqlcheck, cn);
int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into Student (Sid,SNO,Spassword,Sname,Semail,Sclass,Sspecialty,authority) values('" + Sid + "','" + SNO + "','" + Spassword + "','" + Sname + "','" + Semail + "','" + Sclass + "','" + Sspecialty + "','" + authority + "')"