net npoi将List<实体>导出excel的最简单方法

只是临时导数据用的。方便。最基本的方法,  
[HttpGet] [Route("ExportEnterprise")] public BaseResponse ExportEnterprise() { IWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("onesheet"); IRow row0 = sheet.CreateRow(0); row0.CreateCell(0).SetCellValue("顺序号"); row0.CreateCell(1).SetCellValue("企业名"); row0.CreateCell(2).SetCellValue("行业门类"); row0.CreateCell(3).SetCellValue("行业大类"); row0.CreateCell(4).SetCellValue("经营属地"); row0.CreateCell(5).SetCellValue("法人"); row0.CreateCell(6).SetCellValue("法人手机"); row0.CreateCell(7).SetCellValue("法人固话"); row0.CreateCell(8).SetCellValue("日常联系人"); row0.CreateCell(9).SetCellValue("日常联系人手机"); row0.CreateCell(10).SetCellValue("日常联系人电话"); row0.CreateCell(11).SetCellValue("安全人"); row0.CreateCell(12).SetCellValue("安全人手机"); row0.CreateCell(13).SetCellValue("安全人电话"); row0.CreateCell(14).SetCellValue("生产人"); row0.CreateCell(15).SetCellValue("生产人手机"); row0.CreateCell(16).SetCellValue("生产人电话"); row0.CreateCell(17).SetCellValue("技术人"); row0.CreateCell(18).SetCellValue("技术人手机"); row0.CreateCell(19).SetCellValue("技术人电话"); row0.CreateCell(20).SetCellValue("综合人"); row0.CreateCell(21).SetCellValue("综合人电话"); var enterprises = _enterpriseService.GetEnterprisesOfTest().ToList(); var lineNo = 1; foreach (var enterprise in enterprises) { IRow row = sheet.CreateRow(lineNo); row.CreateCell(0).SetCellValue(lineNo); row.CreateCell(1).SetCellValue(enterprise.EnterpriseName); var doorDescr = _industryCategoryService.GetDescriptionBy(enterprise.IndustryCategoryCode); row.CreateCell(2).SetCellValue(doorDescr); var industryGeneraDescr = _industryCategoryService.GetDescriptionBy(enterprise.IndustryGeneraCode); row.CreateCell(3).SetCellValue(industryGeneraDescr); row.CreateCell(4).SetCellValue(_administrativeDivisionService.GetDescriptionBy(enterprise.BusinessAddressDivisonCode)); row.CreateCell(5).SetCellValue(enterprise.LegalPersonName); row.CreateCell(6).SetCellValue(enterprise.LegalPersonPhone); row.CreateCell(7).SetCellValue(enterprise.LegalPersonFixedPhone); row.CreateCell(8).SetCellValue(enterprise.DailyContacts); row.CreateCell(9).SetCellValue(enterprise.DailyContactsPhone); row.CreateCell(10).SetCellValue(enterprise.DailyContactsFixedPhone); row.CreateCell(11).SetCellValue(enterprise.SafetyOrganizationManagerName); row.CreateCell(12).SetCellValue(enterprise.SafetyOrganizationManagerPhone); row.CreateCell(13).SetCellValue(enterprise.SafetyOrganizationManagerFox); row.CreateCell(14).SetCellValue(enterprise.ProductionManagerName); row.CreateCell(15).SetCellValue(enterprise.ProductionManagerPhone); row.CreateCell(16).SetCellValue(enterprise.ProductionManagerFixedPhone); row.CreateCell(17).SetCellValue(enterprise.InformationTechnologyPrincipal); row.CreateCell(18).SetCellValue(enterprise.InformationTechnologyPrincipalPhone); row.CreateCell(19).SetCellValue(enterprise.InformationTechnologyPrincipalLandlineTelephone); var contact = string.Empty; var contactPhone = string.Empty; if (!string.IsNullOrEmpty(enterprise.LegalPersonName)) { contact = enterprise.LegalPersonName; if (!string.IsNullOrEmpty(enterprise.LegalPersonPhone)) { contactPhone = enterprise.LegalPersonPhone; } else { contactPhone = enterprise.LegalPersonFixedPhone; } }else if (!string.IsNullOrEmpty(enterprise.DailyContacts)) { contact = enterprise.DailyContacts; if (!string.IsNullOrEmpty(enterprise.DailyContactsPhone)) { contactPhone = enterprise.DailyContactsPhone; } else { contactPhone = enterprise.DailyContactsFixedPhone; } } else if (!string.IsNullOrEmpty(enterprise.SafetyOrganizationManagerName)) { contact = enterprise.SafetyOrganizationManagerName; if (!string.IsNullOrEmpty(enterprise.SafetyOrganizationManagerPhone)) { contactPhone = enterprise.SafetyOrganizationManagerPhone; } else { contactPhone = enterprise.SafetyOrganizationManagerPhone; } } else if(!string.IsNullOrEmpty(enterprise.ProductionManagerName)) { contact = enterprise.ProductionManagerName; if (!string.IsNullOrEmpty(enterprise.ProductionManagerPhone)) { contactPhone = enterprise.ProductionManagerFixedPhone; } else { contactPhone = enterprise.DailyContactsFixedPhone; } } if (!string.IsNullOrEmpty(enterprise.InformationTechnologyPrincipal)) { contact = enterprise.InformationTechnologyPrincipal; if (!string.IsNullOrEmpty(enterprise.InformationTechnologyPrincipalPhone)) { contactPhone = enterprise.InformationTechnologyPrincipalPhone; } else { contactPhone = enterprise.InformationTechnologyPrincipalLandlineTelephone; } } row.CreateCell(20).SetCellValue(contact); row.CreateCell(21).SetCellValue(contactPhone); lineNo++; } //创建流对象并设置存储Excel文件的路径 using (FileStream url = new FileStream(HttpContext.Current.Server.MapPath("/App_Data/test.xls"), FileMode.OpenOrCreate, FileAccess.ReadWrite)) { //导出Excel文件 workbook.Write(url); }; return Success(new BaseResponse()); }

  

猜你喜欢

转载自www.cnblogs.com/taoshengyujiu/p/9139458.html