Как исправить ошибку системы.исключению нехватки памяти в ASP.NET
Привет, я действительно Новичок в этом деле. asp.net я создал простое веб-приложение, которое пользователь установит определенный диапазон дат и загрузит все данные с таким видом, я получаю такую ошибку из-за слишком большого количества данных, которые я хочу загрузить в excel, Как я могу этого избежать, есть ли другой способ загрузить это или мне действительно нужно сократить объем данных, которые я загружаю, потому что пользователь будет загружать целый месяц, и у меня есть проблема с загрузкой 2-дневных данных из базы данных в excel.
вот мой текущий код на ниже
private void excelLogic() { List<indexDO> indexDo = getIteminDatagrid(); ExcelPackage excel = new ExcelPackage(); var workSheet = excel.Workbook.Worksheets.Add("Sheet1"); workSheet.TabColor = Color.Black; workSheet.DefaultRowHeight = 12; //Header of table // using (ExcelRange excelRannge = workSheet.Cells["A1:AL1"]) { //excelRannge.Style.Border.Top.Style = ExcelBorderStyle.Thick; //excelRannge.Style.Border.Bottom.Style = ExcelBorderStyle.Thick; //excelRannge.Style.Border.Left.Style = ExcelBorderStyle.Thick; //excelRannge.Style.Border.Right.Style = ExcelBorderStyle.Thick; //excelRannge.Style.Border.Top.Color.SetColor(Color.White); //excelRannge.Style.Border.Bottom.Color.SetColor(Color.White); //excelRannge.Style.Border.Left.Color.SetColor(Color.White); //excelRannge.Style.Border.Right.Color.SetColor(Color.White); excelRannge.Style.Fill.PatternType = ExcelFillStyle.Solid; excelRannge.Style.Font.Color.SetColor(Color.White); excelRannge.Style.Fill.BackgroundColor.SetColor(Color.DarkGreen); workSheet.Cells["G1:G10000"].Style.Numberformat.Format = "0"; } workSheet.Row(1).Height = 20; workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; workSheet.Row(1).Style.Font.Bold = true; workSheet.Cells[1, 1].Value = "ID"; workSheet.Cells[1, 2].Value = "ActionCode"; workSheet.Cells[1, 3].Value = "BookReferenceNo"; workSheet.Cells[1, 4].Value = "BookingStatus"; workSheet.Cells[1, 5].Value = "BookingType"; workSheet.Cells[1, 6].Value = "CarrierClass"; workSheet.Cells[1, 7].Value = "CarrierCode"; workSheet.Cells[1, 8].Value = "ComRelNo"; workSheet.Cells[1, 9].Value = "ContainerCommodity"; workSheet.Cells[1, 10].Value = "ContainerFrom"; workSheet.Cells[1, 11].Value = "ContainerHistory"; workSheet.Cells[1, 12].Value = "ContainerNo"; workSheet.Cells[1, 13].Value = "ContainerSize"; workSheet.Cells[1, 14].Value = "ContainerTo"; workSheet.Cells[1, 15].Value = "CreateBookingDate"; workSheet.Cells[1, 16].Value = "DateProcessed"; workSheet.Cells[1, 17].Value = "DateReceived"; workSheet.Cells[1, 18].Value = "Direction"; workSheet.Cells[1, 19].Value = "GKey"; workSheet.Cells[1, 20].Value = "InGateDateTime"; workSheet.Cells[1, 21].Value = "LloydNo"; workSheet.Cells[1, 22].Value = "LoadOrder"; workSheet.Cells[1, 23].Value = "ModifiedBy"; workSheet.Cells[1, 24].Value = "ModifiedDate"; workSheet.Cells[1, 25].Value = "OutGateDateTime"; workSheet.Cells[1, 26].Value = "PKey"; workSheet.Cells[1, 27].Value = "PlateNo"; workSheet.Cells[1, 28].Value = "RunDateTime"; workSheet.Cells[1, 29].Value = "SecondContainer"; workSheet.Cells[1, 30].Value = "SlotExchangeDate"; workSheet.Cells[1, 31].Value = "SlotExchangeFlag"; workSheet.Cells[1, 32].Value = "SlotType"; workSheet.Cells[1, 33].Value = "Status"; workSheet.Cells[1, 34].Value = "TerminalID"; workSheet.Cells[1, 35].Value = "TimeSlotID"; workSheet.Cells[1, 36].Value = "TimeZone"; workSheet.Cells[1, 37].Value = "VBSUser"; workSheet.Cells[1, 38].Value = "VoyageNo"; //Body of table // int recordIndex = 2; foreach (var item in indexDo) { workSheet.Cells[recordIndex, 1].Value = item.ID; workSheet.Cells[recordIndex, 2].Value = item.ActionCode; workSheet.Cells[recordIndex, 3].Value = item.BookReferenceNo; workSheet.Cells[recordIndex, 4].Value = item.BookingStatus; workSheet.Cells[recordIndex, 5].Value = item.BookingType; workSheet.Cells[recordIndex, 6].Value = item.CarrierClass; workSheet.Cells[recordIndex, 7].Value = item.CarrierCode; workSheet.Cells[recordIndex, 8].Value = item.ComRelNo; workSheet.Cells[recordIndex, 9].Value = item.ContainerCommodity; workSheet.Cells[recordIndex, 10].Value = item.ContainerFrom; workSheet.Cells[recordIndex, 11].Value = item.ContainerHistory; workSheet.Cells[recordIndex, 12].Value = item.ContainerNo; workSheet.Cells[recordIndex, 13].Value = item.ContainerNo; workSheet.Cells[recordIndex, 14].Value = item.ContainerTo; workSheet.Cells[recordIndex, 15].Value = item.CreateBookingDate; workSheet.Cells[recordIndex, 16].Value = item.DateProcessed; workSheet.Cells[recordIndex, 17].Value = item.DateReceived; workSheet.Cells[recordIndex, 18].Value = item.Direction; workSheet.Cells[recordIndex, 19].Value = item.GKey; workSheet.Cells[recordIndex, 20].Value = item.InGateDateTime; workSheet.Cells[recordIndex, 21].Value = item.LloydNo; workSheet.Cells[recordIndex, 22].Value = item.LoadOrder; workSheet.Cells[recordIndex, 23].Value = item.ModifiedBy; workSheet.Cells[recordIndex, 24].Value = item.ModifiedDate; workSheet.Cells[recordIndex, 25].Value = item.OutGateDateTime; workSheet.Cells[recordIndex, 26].Value = item.PKey; workSheet.Cells[recordIndex, 27].Value = item.PlateNo; workSheet.Cells[recordIndex, 28].Value = item.RunDateTime; workSheet.Cells[recordIndex, 29].Value = item.SecondContainer; workSheet.Cells[recordIndex, 30].Value = item.SlotExchangeDate; workSheet.Cells[recordIndex, 31].Value = item.SlotExchangeFlag; workSheet.Cells[recordIndex, 32].Value = item.SlotType; workSheet.Cells[recordIndex, 33].Value = item.Status; workSheet.Cells[recordIndex, 34].Value = item.TerminalID; workSheet.Cells[recordIndex, 35].Value = item.TimeSlotID; workSheet.Cells[recordIndex, 36].Value = item.TimeZone; workSheet.Cells[recordIndex, 37].Value = item.VBSUser; workSheet.Cells[recordIndex, 38].Value = item.VoyageNo; recordIndex++; } workSheet.Column(1).AutoFit(); workSheet.Column(2).AutoFit(); workSheet.Column(3).AutoFit(); workSheet.Column(4).AutoFit(); workSheet.Column(5).AutoFit(); workSheet.Column(6).AutoFit(); workSheet.Column(7).AutoFit(); workSheet.Column(8).AutoFit(); workSheet.Column(9).AutoFit(); workSheet.Column(10).AutoFit(); workSheet.Column(11).AutoFit(); workSheet.Column(12).AutoFit(); workSheet.Column(13).AutoFit(); workSheet.Column(14).AutoFit(); workSheet.Column(15).AutoFit(); workSheet.Column(16).AutoFit(); workSheet.Column(17).AutoFit(); workSheet.Column(18).AutoFit(); workSheet.Column(19).AutoFit(); workSheet.Column(20).AutoFit(); workSheet.Column(21).AutoFit(); workSheet.Column(22).AutoFit(); workSheet.Column(23).AutoFit(); workSheet.Column(24).AutoFit(); workSheet.Column(25).AutoFit(); workSheet.Column(26).AutoFit(); workSheet.Column(27).AutoFit(); workSheet.Column(28).AutoFit(); workSheet.Column(29).AutoFit(); workSheet.Column(30).AutoFit(); workSheet.Column(31).AutoFit(); workSheet.Column(32).AutoFit(); workSheet.Column(33).AutoFit(); workSheet.Column(34).AutoFit(); workSheet.Column(35).AutoFit(); workSheet.Column(36).AutoFit(); workSheet.Column(37).AutoFit(); workSheet.Column(38).AutoFit(); string excelName = DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Year.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString(); using (var memoryStream = new MemoryStream()) { Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=" + excelName + ".xlsx"); excel.SaveAs(memoryStream); memoryStream.WriteTo(Response.OutputStream); Response.Flush(); Response.End(); } }
public List<indexDO> getIteminDatagrid() { string stop = string.Empty; long lastValueBookReferenceNo; long BookReferenceNo; var indeDo = new List<indexDO>(); for (int i = 0; i <= gridviewDetails.Rows.Count; i++) { for (int j = i; j <= gridviewDetails.Rows.Count; i++) { long ID = Convert.ToInt64(gridviewDetails.Rows[i].Cells[0].Text); string ActionCode = gridviewDetails.Rows[i].Cells[1].Text; BookReferenceNo = Convert.ToInt64(gridviewDetails.Rows[i].Cells[2].Text); long BookingStatus = Convert.ToInt64(gridviewDetails.Rows[i].Cells[3].Text); string BookingType = gridviewDetails.Rows[i].Cells[4].Text; string CarrierClass = gridviewDetails.Rows[i].Cells[5].Text; long CarrierCode = Convert.ToInt64(gridviewDetails.Rows[i].Cells[6].Text); string ComRelNo = gridviewDetails.Rows[i].Cells[7].Text; string ContainerCommodity = gridviewDetails.Rows[i].Cells[8].Text; long ContainerFrom = Convert.ToInt64(gridviewDetails.Rows[i].Cells[9].Text); long ContainerHistory = Convert.ToInt64(gridviewDetails.Rows[i].Cells[10].Text); string ContainerNo = gridviewDetails.Rows[i].Cells[11].Text; string ContainerSize = gridviewDetails.Rows[i].Cells[12].Text; long ContainerTo = Convert.ToInt64(gridviewDetails.Rows[i].Cells[13].Text); string CreateBookingDate = gridviewDetails.Rows[i].Cells[14].Text; string DateProcessed = gridviewDetails.Rows[i].Cells[15].Text; string DateReceived = gridviewDetails.Rows[i].Cells[16].Text; string Direction = gridviewDetails.Rows[i].Cells[17].Text; string GKey = gridviewDetails.Rows[i].Cells[18].Text; string InGateDateTime = gridviewDetails.Rows[i].Cells[19].Text; string LloydNo = gridviewDetails.Rows[i].Cells[20].Text; long LoadOrder = Convert.ToInt64(gridviewDetails.Rows[i].Cells[21].Text); string ModifiedBy = gridviewDetails.Rows[i].Cells[22].Text; string ModifiedDate = gridviewDetails.Rows[i].Cells[23].Text; string OutGateDateTime = gridviewDetails.Rows[i].Cells[24].Text; string PKey = gridviewDetails.Rows[i].Cells[25].Text; string PlateNo = gridviewDetails.Rows[i].Cells[26].Text; string RunDateTime = gridviewDetails.Rows[i].Cells[27].Text; string SecondContainer = gridviewDetails.Rows[i].Cells[28].Text; string SlotExchangeDate = gridviewDetails.Rows[i].Cells[29].Text; string SlotExchangeFlag = gridviewDetails.Rows[i].Cells[30].Text; string SlotType = gridviewDetails.Rows[i].Cells[31].Text; string Status = gridviewDetails.Rows[i].Cells[32].Text; string TerminalID = gridviewDetails.Rows[i].Cells[33].Text; long TimeSlotID = Convert.ToInt64(gridviewDetails.Rows[i].Cells[34].Text); long TimeZone = Convert.ToInt64(gridviewDetails.Rows[i].Cells[35].Text); string VBSUser = gridviewDetails.Rows[i].Cells[36].Text; string VoyageNo = gridviewDetails.Rows[i].Cells[37].Text; indeDo.Add(new indexDO { ID = ID, ActionCode = ActionCode, BookReferenceNo = BookReferenceNo, BookingStatus = BookingStatus, BookingType = BookingType, CarrierClass = CarrierClass, CarrierCode = CarrierCode, ComRelNo = ComRelNo, ContainerCommodity = ContainerCommodity, ContainerFrom = ContainerFrom, ContainerHistory = ContainerHistory, ContainerNo = ContainerNo, ContainerSize = ContainerSize, ContainerTo = ContainerTo, CreateBookingDate = CreateBookingDate, DateProcessed = DateProcessed, DateReceived = DateReceived, Direction = Direction, GKey = GKey, InGateDateTime = InGateDateTime, LloydNo = LloydNo, LoadOrder = LoadOrder, ModifiedBy = ModifiedBy, ModifiedDate = ModifiedDate, OutGateDateTime = OutGateDateTime, PKey = PKey, PlateNo = PlateNo, RunDateTime = RunDateTime, SecondContainer = SecondContainer, SlotExchangeDate = SlotExchangeDate, SlotExchangeFlag = SlotExchangeFlag, SlotType = SlotType, Status = Status, TerminalID = TerminalID, TimeSlotID = TimeSlotID, TimeZone = TimeZone, VBSUser = VBSUser, VoyageNo = VoyageNo }); int LastRow = gridviewDetails.Rows.Count - 1; lastValueBookReferenceNo = Convert.ToInt64(gridviewDetails.Rows[LastRow].Cells[2].Text); if (lastValueBookReferenceNo == BookReferenceNo) { stop = "Stop"; break; } } if (stop == "Stop") { break; } } return indeDo; }
Что я уже пробовал:
я попытался загрузить данные из gridview и установить gridview на 1000 на страницу, но это повлияет на время загрузки, это лучший способ?