.net 导出Excel插件Npoi的使用
1、NuGet搜索Npoi并安装
2、添加引用将包引用进来
3、Controller里引用
4、使用
public ActionResult ExportExcel() { plist = 数据源 HSSFWorkbook hssfworkbook = new HSSFWorkbook(); HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.CreateSheet("Sheet1"); HSSFRow row = (HSSFRow)sheet1.CreateRow(0); row.CreateCell(0).SetCellValue("姓名"); row.CreateCell(1).SetCellValue("序号"); row.CreateCell(2).SetCellValue("组别"); row.CreateCell(3).SetCellValue("手机号"); row.CreateCell(4).SetCellValue("职务"); row.CreateCell(5).SetCellValue("连锁"); row.CreateCell(6).SetCellValue("门店"); row.CreateCell(7).SetCellValue("省份"); row.CreateCell(8).SetCellValue("城市"); HSSFRow[] rowList = new HSSFRow[plist.Count()]; int i = 0; foreach (var item in plist) { sheet1.SetColumnWidth(i, 30 * 256); rowList[i] = (HSSFRow)sheet1.CreateRow(i + 1); sheet1.SetColumnWidth(0, 8 * 256);//将第一列编号的单元格的宽度设置为8个字符 rowList[i].CreateCell(0).SetCellValue(item.WeiXinUser3.Name); rowList[i].CreateCell(1).SetCellValue(item.WeiXinUser3.SerialNum.ToString()); rowList[i].CreateCell(2).SetCellValue(item.GroupName); rowList[i].CreateCell(3).SetCellValue(item.WeiXinUser3.MobilePhone); rowList[i].CreateCell(4).SetCellValue(item.WeiXinUser3.WorkDuty); rowList[i].CreateCell(5).SetCellValue(item.WeiXinUser3.ChainStoreName); rowList[i].CreateCell(6).SetCellValue(item.WeiXinUser3.ShopName); rowList[i].CreateCell(7).SetCellValue(item.WeiXinUser3.Province.Name); rowList[i].CreateCell(8).SetCellValue(item.WeiXinUser3.City.Name); i++; } System.IO.MemoryStream ms = new System.IO.MemoryStream(); hssfworkbook.Write(ms); return File(ms.ToArray(), "application/ms-excel", area.AreaName + "明细.xls"); }