/// <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>");
}