//判断有没有选择文件

if(file == null)
{
return Content("<script>alert('请选择文件夹');location.href='/Home/Get_Add'</script>");
}

else
{
var filePath = Path.Combine(Request.MapPath("~/Files"), Path.GetFileName(file.FileName));
file.SaveAs(filePath);

s.Name = file.FileName;
s.Url = "/Files/" + file.FileName;

if(bll.Add_Stu(s)>0)
{
return Content("<script>alert('上传成功!');location.href='/Home/Get_Add'</script>");
}
else
{
return Content("<script>alert('上传失败!');location.href='/Home/Get_Add'</script>");
}

}

 

//导入

public ActionResult lead()
{
List<StuModel> list = new List<StuModel>();
DataTable table = this.ImportExcelFile("/Files/工作文档.xls");//导入的文件地址
for(int i =0;i<table.Rows.Count;i++)//循环行
{
StuModel s = new StuModel();

s.province_name = table.Rows[i][0].ToString();
s.City_name= table.Rows[i][1].ToString();
s.Area_code = table.Rows[i][2].ToString();
bll.Add_China(s);
list.Add(s);
}
if(list!=null)

}

 

//导出

public ActionResult Export()
{
List<StuModel> list = bll.Show_China();
if (list.Count > 0)
{
//创建Excel对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();

//添加一个shee1t工作表
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("sheet1");

//给sheet1工作表添加一行类标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("省份");
row1.CreateCell(1).SetCellValue("地区");
row1.CreateCell(2).SetCellValue("长途区号");

//循环list集合,把数据写入excel中
for (int i = 0; i < list.Count; i++)
{
NPOI.SS.UserModel.IRow row2 = sheet1.CreateRow(i + 1);
row2.CreateCell(0).SetCellValue(list[i].province_name);
row2.CreateCell(1).SetCellValue(list[i].City_name);
row2.CreateCell(2).SetCellValue(list[i].Area_code);
}

//写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/vnd.ms-excel", "区域号.xls");

}
else
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
return File(ms, "application/vnd.ms-excel", "无数据");
}

}

posted on 2017-11-22 10:11  小新嘿嘿嘿  阅读(175)  评论(0编辑  收藏  举报