• Excel导入导出


    /// <summary>
    /// 数据导出
    /// </summary>
    /// <returns></returns>
    public FileResult daochu()
    {
    string filename = Server.MapPath("/ExcelFile.xlsx");//在bin文件下创建ExcelFile文件夹放excel
    con.Open();
    SqlCommand comm = new SqlCommand("select * from daoru", con);
    SqlDataAdapter adap = new SqlDataAdapter(comm);
    DataTable dt = new DataTable();
    adap.Fill(dt);
    con.Close();
    NPOIHelper.ExportDTtoExcel(dt, "班级列表", filename);//保存excel并打开excel
    return File(filename, ".xlsx", "班级表.xlsx");

    }
    /// <summary>
    ///数据导入
    /// </summary>
    /// <param name="file1"></param>
    /// <returns></returns>
    public ActionResult DaoRu(HttpPostedFileBase file1)
    {
    DataSet ds = NPOIHelper.ImportExceltoDs(file1.InputStream);//接受一个excel文件并转换为dataset
    string sql = "";
    DataTable de = ds.Tables[0];//选择第一个excel文件
    foreach (DataRow item in de.Rows)
    {
    sql += "insert into daoru values('" + item[1] + "','" + item[2] + "','" + item[3] + "') ";
    }
    con.Open();
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.ExecuteNonQuery();
    con.Close();
    return Content("<script>alert('导入成功');location.href='/student/Index'</script>");
    }

  • 相关阅读:
    2020重新出发,MySql基础,MySql用户管理
    2020重新出发,MySql基础,MySql字符集
    2020重新出发,MySql基础,MySql事务
    python全栈day10
    python全栈day9
    python全栈day8
    python全栈day7
    python全栈day6
    python全栈day5
    python全栈day4
  • 原文地址:https://www.cnblogs.com/htbmvc/p/7880800.html
Copyright © 2020-2023  润新知