度量快速开发平台-专业、快速的软件定制快开平台
标题:
net mvc 利用NPOI导入导出excel
[打印本页]
作者:
Adam丶鱼
时间:
2020-6-28 18:15
标题:
net mvc 利用NPOI导入导出excel
1、导出Excel
:
首先引用NPOI包(Action一定要用FileResult)
/// <summary>
/// 批量导出需要导出的列表
/// </summary>
/// <returns></returns>
public FileResult ExportStu2()
{
//获取list数据
var checkList = (from oc in db.OrganizeCustoms
join o in db.Organizes.DefaultIfEmpty() on oc.custom_id equals o.id
where oc.organize_id == 1
select new
{
customer_id = o.id,
customer_name = o.name
}).ToList();
//创建Excel文件的对象
NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
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("姓名");
//....N行
//将数据逐步写入sheet1各个行
for (int i = 0; i < checkList.Count; i++)
{
NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
rowtemp.CreateCell(0).SetCellValue(checkList
.customer_id.ToString());
rowtemp.CreateCell(1).SetCellValue(checkList
.customer_name.ToString());
//....N行
}
// 写入到客户端
System.IO.MemoryStream ms = new System.IO.MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
DateTime dt = DateTime.Now;
string dateTime = dt.ToString("yyMMddHHmmssfff");
string fileName = "查询结果" + dateTime + ".xls";
return File(ms, "application/vnd.ms-excel", fileName);
}
前台直接写
@Html.ActionLink(
"
点击导出Excel
"
,
"
ExportStu2
"
)
作者:
Adam丶鱼
时间:
2020-6-28 18:16
作者:
fteair
时间:
2020-6-29 14:15
作者:
张兴康
时间:
2020-6-30 14:13
欢迎光临 度量快速开发平台-专业、快速的软件定制快开平台 (http://plat.delit.cn/)
Powered by Discuz! X3.2