public ActionResult daochu()
{
//读取数据
UserDBEntities context = new UserDBEntities();
var result = context.UserInfo.Where(u=>true);
//读取模板
Workbook book = new Workbook();
book.Open(Server.MapPath("/Test/User.xlsx"));
//获取sheet
var sheet=book.Worksheets[0];
//获取单元格
var cell = sheet.Cells;
int row = 3;
//插入数据
foreach (var item in result)
{
cell[row, 0].PutValue(
item.id);
cell[row, 1].PutValue(
item.name);
cell[row, 2].PutValue(item.age);
row++;
}
//保存
string fileName = Guid.NewGuid().ToString() + ".xlsx";
book.Save(Server.MapPath("/Test")+"\\"+fileName);
//下载
return File(Server.MapPath("/Test") + "\\" + fileName,"application/book","Users.xlsx");
}
}
}