public ActionResult ExportToExcel(long?categoryId = null, string brandName = "", string productCode = "", int?auditStatus = null, string ids = "", string keyWords = "", string shopName = "", int?saleStatus = null, sbyte?productType = null) { var query = new ProductQuery() { PageSize = int.MaxValue, PageNo = 1, BrandName = brandName, KeyWords = keyWords, CategoryId = categoryId, Ids = string.IsNullOrWhiteSpace(ids) ? null : ids.Split(',').Select(item => long.Parse(item)), ShopName = shopName, ProductCode = productCode, NotIncludedInDraft = true }; if (productType.HasValue && productType.Value > -1) { query.ProductType = productType.Value; } if (auditStatus.HasValue) { query.AuditStatus = new ProductInfo.ProductAuditStatus[] { (ProductInfo.ProductAuditStatus)auditStatus }; if (auditStatus == (int)ProductInfo.ProductAuditStatus.WaitForAuditing) { query.SaleStatus = ProductInfo.ProductSaleStatus.OnSale; } } if (saleStatus.HasValue) { query.SaleStatus = (ProductInfo.ProductSaleStatus)saleStatus; } var data = ProductManagerApplication.GetProducts(query); #region 先查询到列表,便于下面循环读取 List <CategoryInfo> listproductcate = new List <CategoryInfo>(); //商品分类 List <BrandInfo> listbrand = new List <BrandInfo>(); //品牌 List <FreightTemplateInfo> listtemplate = new List <FreightTemplateInfo>(); //运费模板 List <Mall.DTO.ProductShopCategory> listshopcate = new List <DTO.ProductShopCategory>(); //商铺分类 Dictionary <long, string> shopnames = new Dictionary <long, string>(); if (data.Models != null) { List <long> cateids = data.Models.Select(p => p.CategoryId).ToList(); listproductcate = CategoryApplication.GetCateogryListByIds(cateids);//平台分类 if (listproductcate == null) { listproductcate = new List <CategoryInfo>(); } List <long> templateIds = data.Models.Select(p => p.FreightTemplateId).ToList(); listtemplate = FreightTemplateApplication.GetFreightTemplateList(templateIds);//运费模板 if (listtemplate == null) { listtemplate = new List <FreightTemplateInfo>(); //实例下便免下面循环里判断空 } IEnumerable <long> brandIds = data.Models.Select(p => p.BrandId); //品牌 listbrand = BrandApplication.GetBrands(brandIds); if (listbrand == null) { listbrand = new List <BrandInfo>(); } List <long> productids = data.Models.Select(p => p.Id).ToList();//商铺分类 listshopcate = ShopCategoryApplication.GetCategorysByProduct(productids); if (listshopcate == null) { listshopcate = new List <DTO.ProductShopCategory>(); } List <long> shopids = data.Models.Select(p => p.ShopId).ToList(); shopnames = ShopApplication.GetShopNames(shopids); if (shopnames == null) { shopnames = new Dictionary <long, string>(); } } #endregion var products = data.Models.Select(item => { var desc = ProductManagerApplication.GetProductDescription(item.Id); var skus = _iProductService.GetSKUs(item.Id); var brand = (item.BrandId <= 0) ? null : listbrand.Where(p => p.Id == item.BrandId).FirstOrDefault(); //品牌 var freightTem = (item.FreightTemplateId <= 0) ? null : listtemplate.Where(p => p.Id == item.FreightTemplateId).FirstOrDefault(); //运费模板 var platFormCate = (item.CategoryId <= 0) ? null : listproductcate.Where(p => p.Id == item.CategoryId).FirstOrDefault(); //平台分类 var shopcate = listshopcate.Where(p => p.ProductId == item.Id).FirstOrDefault(); //商铺分类 return(new ProductModel() { //TODO:FG 循环内查询调用(大数据量) name = item.ProductName, brandName = brand == null ? "" : brand.Name, id = item.Id, imgUrl = item.GetImage(ImageSize.Size_50), price = item.MinSalePrice, state = item.ShowProductState, auditStatus = (int)item.AuditStatus, url = "", auditReason = desc?.AuditReason ?? string.Empty, //shopName = shopService.GetShopBasicInfo(item.ShopId) == null ? "" : shopService.GetShopBasicInfo(item.ShopId).ShopName, shopName = shopnames[item.ShopId], saleStatus = (int)item.SaleStatus, productCode = item.ProductCode, categoryName = platFormCate == null ? "" : platFormCate.Name, //平台分类 ShopCategoryName = shopcate == null ? "" : shopcate.ShopCategoryName, //商家分类 AuditStatusText = (item.AuditStatus == ProductInfo.ProductAuditStatus.Audited ? "已审核" : "待审核"), MeasureUnit = item.MeasureUnit, HasSKU = item.HasSKU, SKUInfo = skus, FreightTemplateName = freightTem == null ? "" : freightTem.Name, //运费模板名称 IsOpenLadder = item.IsOpenLadder, //是否开启阶梯批发 ProductLadderPrice = ProductManagerApplication.GetLadderPriceInfosByProductId(item.Id, item.IsOpenLadder), ProductTypeName = item.ProductType == 1 ? "虚拟商品" : "实物商品", //商品类型(虚拟或实物) VirtualProduct = (item.ProductType == 1 ? ProductManagerApplication.GetVirtualProductInfoByProductId(item.Id) : null), //虚拟商品 AuditReason = desc == null?"":desc.AuditReason, //审核备注 AddedDate = item.AddedDate.ToString(), //发布日期 MarketPrice = item.MarketPrice, //市场价 }); }); #region 构建Excel文档 ViewData.Model = products; string viewHtml = RenderPartialViewToString(this, "ExportProductinfo"); return(File(System.Text.Encoding.UTF8.GetBytes(viewHtml), "application/ms-excel", string.Format("平台商品信息_{0}.xls", DateTime.Now.ToString("yyyy-MM-dd")))); #endregion }