1、NuGet搜索Npoi並安裝 2、添加引用將包引用進來 3、Controller里引用 4、使用 ...
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"); }