基于Asp.net下Excel批量导入SQLserver数据库

关键代码

protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件
        {
            Response.Write(" ");
            return;//当无文件时,返回
        }
        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名
        if (IsXls != ".xls")
        {
            Response.Write("");
            return;//当选择的不是Excel文件时,返回
        }
        string filename = 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("");   //当Excel表为空时,对用户进行提示
        }
        else
        {
            for (int i = 0; i < dr.Length; i++)
            {
                //前面除了你需要在建立一个“upfiles”的文件夹外,其他的都不用管了,你只需要通过下面的方式获取Excel的值,然后再将这些值用你的方式去插入到数据库里面
                string title = dr[i]["姓名"].ToString();
                string linkurl = dr[i]["工号"].ToString();
                string categoryname = dr[i]["性别"].ToString();
                string customername = dr[i]["出生日期"].ToString();

                //Response.Write("");
            }
            Response.Write("");
        }
    }

   /// 
    /// 连接Excel  读取Excel数据   并返回DataSet数据集合
    /// 
    /// Excel服务器路径
    /// Excel表名称
    /// 
    public static 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$]");
            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;
        }
    }

下载Excel样例

//c#标准下载文件代码
protected
void LinkButton1_Click(object sender, EventArgs e) { string _filepath = "~/upfiles/样本.xls"; string _filename = "样本.xls"; if (System.IO.File.Exists(MapPath(_filepath))) { Response.Clear(); Response.Buffer = true; Response.AddHeader("Content-Disposition", "attachment;filename=" + _filename); Response.ContentType = "application/unknow"; Response.TransmitFile(_filepath); Response.End(); } }

测试Html

"FileUpload1" runat="server" />
"Button1" runat="server" Text="批量导入" οnclick="Button1_Click" />

"LinkButton1" runat="server" οnclick="LinkButton1_Click">下载样本示例

 

转载于:https://www.cnblogs.com/hyyweb/p/7646543.html

你可能感兴趣的:(数据库,c#)