Click here to Skip to main content
15,883,647 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
it is giving Exception of type 'System.OutOfMemoryException' was thrown. error.
i'm using package for export to excel.i'm using this code...
C#
public virtual void ExportProductsToXlsx(Stream stream, IList<Product> products)
       {
           if (stream == null)
               throw new ArgumentNullException("stream");

           // ok, we can run the real code of the sample now
           using (var xlPackage = new ExcelPackage(stream))
           {
               // uncomment this line if you want the XML written out to the outputDir
               //xlPackage.DebugMode = true;

               // get handle to the existing worksheet
               var worksheet = xlPackage.Workbook.Worksheets.Add("Products");
               //Create Headers and format them
               var properties = new string[]
               {
                   "ProductTypeId",
                   "ParentGroupedProductId",
                   "VisibleIndividually",
                   "Name",
                   "ShortDescription",
                   "FullDescription",
                   "VendorId",
                   "ProductTemplateId",
                   "ShowOnHomePage",
                   "MetaKeywords",
                   "MetaDescription",
                   "MetaTitle",
                   "SeName",
                   "AllowCustomerReviews",
                   "Published",
                   "SKU",
                   "ManufacturerPartNumber",
                   "Gtin",
                   "IsGiftCard",
                   "GiftCardTypeId",
                   "RequireOtherProducts",
                   "RequiredProductIds",
                   "AutomaticallyAddRequiredProducts",
                   "IsDownload",
                   "DownloadId",
                   "UnlimitedDownloads",
                   "MaxNumberOfDownloads",
                   "DownloadActivationTypeId",
                   "HasSampleDownload",
                   "SampleDownloadId",
                   "HasUserAgreement",
                   "UserAgreementText",
                   "IsRecurring",
                   "RecurringCycleLength",
                   "RecurringCyclePeriodId",
                   "RecurringTotalCycles",
                   "IsShipEnabled",
                   "IsFreeShipping",
                   "AdditionalShippingCharge",
                   "DeliveryDateId",
                   "WarehouseId",
                   "IsTaxExempt",
                   "TaxCategoryId",
                   "ManageInventoryMethodId",
                   "StockQuantity",
                   "DisplayStockAvailability",
                   "DisplayStockQuantity",
                   "MinStockQuantity",
                   "LowStockActivityId",
                   "NotifyAdminForQuantityBelow",
                   "BackorderModeId",
                   "AllowBackInStockSubscriptions",
                   "OrderMinimumQuantity",
                   "OrderMaximumQuantity",
                   "AllowedQuantities",
                   "AllowAddingOnlyExistingAttributeCombinations",
                   "DisableBuyButton",
                   "DisableWishlistButton",
                   "AvailableForPreOrder",
                   "PreOrderAvailabilityStartDateTimeUtc",
                   "CallForPrice",
                   "Price",
                   "OldPrice",
                   "ProductCost",
                   "SpecialPrice",
                   "SpecialPriceStartDateTimeUtc",
                   "SpecialPriceEndDateTimeUtc",
                   "CustomerEntersPrice",
                   "MinimumCustomerEnteredPrice",
                   "MaximumCustomerEnteredPrice",
                   "Weight",
                   "Length",
                   "Width",
                   "Height",
                   "CreatedOnUtc",
                   "CategoryIds",
                   "ManufacturerIds",
                   "Picture1",
                   "Picture2",
                   "Picture3",
               };
               for (int i = 0; i < properties.Length; i++)
               {
                   worksheet.Cells[1, i + 1].Value = properties[i];
                   worksheet.Cells[1, i + 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
                   worksheet.Cells[1, i + 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228));
                   worksheet.Cells[1, i + 1].Style.Font.Bold = true;
               }


               int row = 2;
               foreach (var p in products)
               {
                   int col = 1;

                   worksheet.Cells[row, col].Value = p.ProductTypeId;
                   col++;

                   worksheet.Cells[row, col].Value = p.ParentGroupedProductId;
                   col++;

                   worksheet.Cells[row, col].Value = p.VisibleIndividually;
                   col++;

                   worksheet.Cells[row, col].Value = p.Name;
                   col++;

                   worksheet.Cells[row, col].Value = p.ShortDescription;
                   col++;

                   worksheet.Cells[row, col].Value = p.FullDescription;
                   col++;

                   worksheet.Cells[row, col].Value = p.VendorId;
                   col++;

                   worksheet.Cells[row, col].Value = p.ProductTemplateId;
                   col++;

                   worksheet.Cells[row, col].Value = p.ShowOnHomePage;
                   col++;

                   worksheet.Cells[row, col].Value = p.MetaKeywords;
                   col++;

                   worksheet.Cells[row, col].Value = p.MetaDescription;
                   col++;

                   worksheet.Cells[row, col].Value = p.MetaTitle;
                   col++;

                   worksheet.Cells[row, col].Value = p.GetSeName(0);
                   col++;

                   worksheet.Cells[row, col].Value = p.AllowCustomerReviews;
                   col++;

                   worksheet.Cells[row, col].Value = p.Published;
                   col++;

                   worksheet.Cells[row, col].Value = p.Sku;
                   col++;

                   worksheet.Cells[row, col].Value = p.ManufacturerPartNumber;
                   col++;

                   worksheet.Cells[row, col].Value = p.Gtin;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsGiftCard;
                   col++;

                   worksheet.Cells[row, col].Value = p.GiftCardTypeId;
                   col++;

                   worksheet.Cells[row, col].Value = p.RequireOtherProducts;
                   col++;

                   worksheet.Cells[row, col].Value = p.RequiredProductIds;
                   col++;

                   worksheet.Cells[row, col].Value = p.AutomaticallyAddRequiredProducts;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsDownload;
                   col++;

                   worksheet.Cells[row, col].Value = p.DownloadId;
                   col++;

                   worksheet.Cells[row, col].Value = p.UnlimitedDownloads;
                   col++;

                   worksheet.Cells[row, col].Value = p.MaxNumberOfDownloads;
                   col++;

                   worksheet.Cells[row, col].Value = p.DownloadActivationTypeId;
                   col++;

                   worksheet.Cells[row, col].Value = p.HasSampleDownload;
                   col++;

                   worksheet.Cells[row, col].Value = p.SampleDownloadId;
                   col++;

                   worksheet.Cells[row, col].Value = p.HasUserAgreement;
                   col++;

                   worksheet.Cells[row, col].Value = p.UserAgreementText;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsRecurring;
                   col++;

                   worksheet.Cells[row, col].Value = p.RecurringCycleLength;
                   col++;

                   worksheet.Cells[row, col].Value = p.RecurringCyclePeriodId;
                   col++;

                   worksheet.Cells[row, col].Value = p.RecurringTotalCycles;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsShipEnabled;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsFreeShipping;
                   col++;

                   worksheet.Cells[row, col].Value = p.AdditionalShippingCharge;
                   col++;

                   worksheet.Cells[row, col].Value = p.DeliveryDateId;
                   col++;

                   worksheet.Cells[row, col].Value = p.WarehouseId;
                   col++;

                   worksheet.Cells[row, col].Value = p.IsTaxExempt;
                   col++;

                   worksheet.Cells[row, col].Value = p.TaxCategoryId;
                   col++;

                   worksheet.Cells[row, col].Value = p.ManageInventoryMethodId;
                   col++;

                   worksheet.Cells[row, col].Value = p.StockQuantity;
                   col++;

                   worksheet.Cells[row, col].Value = p.DisplayStockAvailability;
                   col++;

                   worksheet.Cells[row, col].Value = p.DisplayStockQuantity;
                   col++;

                   worksheet.Cells[row, col].Value = p.MinStockQuantity;
                   col++;

                   worksheet.Cells[row, col].Value = p.LowStockActivityId;
                   col++;

                   worksheet.Cells[row, col].Value = p.NotifyAdminForQuantityBelow;
                   col++;

                   worksheet.Cells[row, col].Value = p.BackorderModeId;
                   col++;

                   worksheet.Cells[row, col].Value = p.AllowBackInStockSubscriptions;
                   col++;

                   worksheet.Cells[row, col].Value = p.OrderMinimumQuantity;
                   col++;

                   worksheet.Cells[row, col].Value = p.OrderMaximumQuantity;
                   col++;

                   worksheet.Cells[row, col].Value = p.AllowedQuantities;
                   col++;

                   worksheet.Cells[row, col].Value = p.AllowAddingOnlyExistingAttributeCombinations;
                   col++;

                   worksheet.Cells[row, col].Value = p.DisableBuyButton;
                   col++;

                   worksheet.Cells[row, col].Value = p.DisableWishlistButton;
                   col++;

                   worksheet.Cells[row, col].Value = p.AvailableForPreOrder;
                   col++;

                   worksheet.Cells[row, col].Value = p.PreOrderAvailabilityStartDateTimeUtc;
                   col++;

                   worksheet.Cells[row, col].Value = p.CallForPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.Price;
                   col++;

                   worksheet.Cells[row, col].Value = p.OldPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.ProductCost;
                   col++;

                   worksheet.Cells[row, col].Value = p.SpecialPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.SpecialPriceStartDateTimeUtc;
                   col++;

                   worksheet.Cells[row, col].Value = p.SpecialPriceEndDateTimeUtc;
                   col++;

                   worksheet.Cells[row, col].Value = p.CustomerEntersPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.MinimumCustomerEnteredPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.MaximumCustomerEnteredPrice;
                   col++;

                   worksheet.Cells[row, col].Value = p.Weight;
                   col++;

                   worksheet.Cells[row, col].Value = p.Length;
                   col++;

                   worksheet.Cells[row, col].Value = p.Width;
                   col++;

                   worksheet.Cells[row, col].Value = p.Height;
                   col++;

                   worksheet.Cells[row, col].Value = p.CreatedOnUtc.ToOADate();
                   col++;

                   //category identifiers
                   string categoryIds = null;
                   foreach (var pc in _categoryService.GetProductCategoriesByProductId(p.Id))
                   {
                       categoryIds += pc.CategoryId;
                       categoryIds += ";";
                   }
                   worksheet.Cells[row, col].Value = categoryIds;
                   col++;

                   //manufacturer identifiers
                   string manufacturerIds = null;
                   foreach (var pm in _manufacturerService.GetProductManufacturersByProductId(p.Id))
                   {
                       manufacturerIds += pm.ManufacturerId;
                       manufacturerIds += ";";
                   }
                   worksheet.Cells[row, col].Value = manufacturerIds;
                   col++;

                   //pictures (up to 3 pictures)
                   string picture1 = null;
                   string picture2 = null;
                   string picture3 = null;
                   var pictures = _pictureService.GetPicturesByProductId(p.Id, 3);
                   for (int i = 0; i < pictures.Count; i++)
                   {
                       string pictureLocalPath = _pictureService.GetThumbLocalPath(pictures[i]);
                       switch (i)
                       {
                           case 0:
                               picture1 = pictureLocalPath;
                               break;
                           case 1:
                               picture2 = pictureLocalPath;
                               break;
                           case 2:
                               picture3 = pictureLocalPath;
                               break;
                       }
                   }
                   worksheet.Cells[row, col].Value = picture1;
                   col++;
                   worksheet.Cells[row, col].Value = picture2;
                   col++;
                   worksheet.Cells[row, col].Value = picture3;
                   col++;

                   row++;


               }



            
               xlPackage.Save();
           }
       }
Posted

1 solution

Why don't you ask the authors?
Jump in here: https://excelpackage.codeplex.com/discussions[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900