MVC架构

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
using Model;
using BLL;
using NPOI.SS.UserModel; //引用NPOI.dll
using NPOI.HSSF.UserModel;//引用NPOI.dll
using System.IO;
using System.Data;
using NPOI;
using System.Data.SqlClient;

namespace WebApplication1.Controllers
{
public class showController : Controller
{
stuBLL bll = new stuBLL();
// GET: show
public ActionResult Index()
{
var reust = bll.show();
return View(reust);
}
public ActionResult add()
{
var p = new BLL.stuBLL().xia();
SelectList list = new SelectList(p, "Typeid", "TypeName");
ViewBag.xia = list;

List<stuMs> m = new List<stuMs>()
{
new stuMs { ID=1,Name="读书" },
new stuMs { ID=2,Name="写字" },
new stuMs { ID=3,Name="看报" },
};
ViewBag.Aihao = m;
return View();
}
[HttpPost]
public ActionResult add(stuM model)
{
var aihao = Request.Form["Aihao"];

stuModel m = new stuModel();

m.Name = model.Name;
m.Sex = model.Sex;
m.Typeid = model.Typeid;
m.Aihao = aihao;
var p = bll.add(m);
if (p > 0)
{
return Content("<script>alert('添加成功');location.href='/show/Index'</script>");
}
else
{
return Content("失败");
}


}
/// <summary>
/// 删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult shan(int id)
{

var p = bll.shan(id);
if (p > 0)
{
return Content("<script>alert('删除成功');location.href='/show/Index'</script>");
}
else
{
return Content("失败");
}


}
public ActionResult xiu(int id)
{
var p = new BLL.stuBLL().xia();
SelectList list = new SelectList(p, "Typeid", "TypeName");
ViewBag.xia = list;

List<stuMs> m = new List<stuMs>()
{
new stuMs { ID=1,Name="读书" },
new stuMs { ID=2,Name="写字" },
new stuMs { ID=3,Name="看报" },
};
ViewBag.Aihao = m;

var data = new BLL.stuBLL().dan(id).FirstOrDefault();
stuM stu = new stuM()
{
ID = data.ID,
Name=data.Name,
Sex=data.Sex,
Aihao=data.Aihao,
Typeid=data.Typeid

};

return View(stu);
}
/// <summary>
///FileResult 表示一个用于将二进制文件内容发送到响应的基类
/// </summary>
/// <returns>导出</returns>
public FileResult DaoChu()
{
var reust = new BLL.stuBLL().show();
if (reust != null || reust.Count > 0)
{
//创建excel
HSSFWorkbook book = new HSSFWorkbook();
//添加excel表
ISheet sheet = book.CreateSheet("sheet1");
//创建标题
IRow rows = sheet.CreateRow(0);
rows.CreateCell(0).SetCellValue("编号");//取下标从零开始
rows.CreateCell(1).SetCellValue("名称");
rows.CreateCell(2).SetCellValue("性别");
rows.CreateCell(3).SetCellValue("爱好");
rows.CreateCell(4).SetCellValue("类型");
for (int i = 0; i < reust.Count; i++)//循环数据的个数
{
IRow rowss = sheet.CreateRow(i + 1);
rowss.CreateCell(0).SetCellValue(reust[i].ID);//循环字段
rowss.CreateCell(1).SetCellValue(reust[i].Name);
rowss.CreateCell(2).SetCellValue(reust[i].Sex);
rowss.CreateCell(3).SetCellValue(reust[i].Aihao);
rowss.CreateCell(4).SetCellValue(reust[i].Typeid);
}
MemoryStream ms = new MemoryStream();//创建支持存储区的文件流
book.Write(ms); //应该是写入
ms.Seek(0, SeekOrigin.Begin);//当前流的位置设置的指定值
return File(ms, "application/vnd-excel", "学生.xls");

}
else
{
MemoryStream ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application", "");
}

}

//public ActionResult dsa(HttpPostedFileBase filel)
//{
// try
// {
// DataSet ds = NPOIHelper.ImportExceltoDs(filel.InputStream);
// string sql = "";
// DataTable dt = ds.Tables[0];
// foreach (DataRow item in dt.Rows)
// {
// sql += "insert into student values('" + item[1] + "','" + item[2] + "','" + item[3] + "','" + item[4] + "')";

// }
// SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=rikao2;Integrated Security=True");
// conn.Open();
// SqlCommand comm = new SqlCommand(sql, conn);
// comm.ExecuteNonQuery();
// return Content("<script>alert('导入成功');location.href='/show/Index'</script>");
// }
// catch (Exception)
// {
// return Content("<script>alert('导入失败');location.href='/Home/Index'</script>");
// }


//}


/// <summary>
///
/// 导入 Import
/// </summary>
/// <param name="Exc"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Execl(HttpPostedFileBase Exc)
{
#region /// 上传部分

//如果当前的网站目录为E:wwwroot 应用程序虚拟目录为E:wwwrootcompany 浏览的页面路径为E:wwwrootcompany ewsshow.asp
//在show.asp页面中使用
//Server.MapPath("./") 返回路径为:E:wwwrootcompany ews
//Server.MapPath("/") 返回路径为:E:wwwroot
//Server.MapPath("../") 返回路径为:E:wwwrootcompany
//Server.MapPath("~/") 返回路径为:E:wwwrootcompany

string strfileName = Server.MapPath("/Word/"); //存储文件的地方

if (!Directory.Exists(strfileName)) //判断文件路径是否存在
{
Directory.CreateDirectory(strfileName);
}
string allFilePath = strfileName + "/" + Exc.FileName;
string fName = Path.GetFileName(allFilePath); //获取文件名
Exc.SaveAs(strfileName + fName);

#endregion

#region /// Execl导入部分

//execl文件读取
stuBLL bll = new stuBLL();

DataTable dt =bll.ExcelToDS(strfileName + fName);//把数据获取到

//把读取的数据导入到数据库
foreach (DataRow dr in dt.Rows)
{
stuModel student = new stuModel();
student.ID= Convert.ToInt32(dr[0]);
student.Name= dr[1].ToString();
student.Sex=Convert.ToBoolean(dr[2]);
student.Aihao= dr[3].ToString();
student.Typeid=Convert.ToInt32(dr[4]);
bll.Adds(student);
}

#endregion

List<stuModel> ls = bll.show();

return View("Index", ls);
}

}
}

 修改页面


@{
Layout = null;
}
@model WebApplication1.Models.stuM
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>xiu</title>
</head>
<body>
<div>
@using (Html.BeginForm())
{
<table>
<tr>
<td>名称</td>
<td>@Html.TextBoxFor(s=>s.Name)</td>
</tr>
<tr>
<td>性别</td>
<td>@Html.RadioButtonFor(s=>s.Sex,true)男
@Html.RadioButtonFor(s => s.Sex,false)女
</td>
</tr>
<tr>
<td>类型</td>
<td>
@Html.DropDownListFor(s=>s.Typeid,ViewBag.xia as SelectList)
</td>
</tr>
<tr>
<td>爱好</td>
<td>
@foreach (var item in ViewBag.Aihao as List<WebApplication1.Models.stuMs>)
{
var check = Model.Aihao.Contains(item.Name) ? "checked" : "";

<input id="Checkbox1" type="checkbox" name="Aihao" value="@item.Name" @check />@item.Name
}
</td>
</tr>
<tr>
<td> </td>
<td>
<input id="Submit1" type="submit" value="修改" />
</td>
</tr>
</table>

}
</div>
</body>
</html>

原文地址:https://www.cnblogs.com/net520/p/7834627.html