c # Экспорт в Excel в System.OutOfMemoryException - PullRequest
0 голосов
/ 29 октября 2019

У меня есть программа, которая: 1. выполняет SQL-запрос к MSSQL 2. вставляет строки из SQL-запроса в Excel 3. отправляет документ Excel по почте
, если число строк, которые нужно вставить в документ Excel, больше 100000затем возникает ошибка: System.OutOfMemoryException Я устанавливаю значение в свойствах проекта - target target = x64, но это не помогло

  using (SqlConnection sqlConn = new SqlConnection(connectionString))
                using (SqlCommand cmd = new SqlCommand(Qry, sqlConn))
                {
                    sqlConn.Open();
                    cmd.CommandTimeout = 0;
                    {
                        string Recipients = configMan.GetAppSetting("Recipients");
                        string CcRecipients = "";
                        string Subject = configMan.GetAppSetting("Subject");
                        string Body;

                        if (string.IsNullOrWhiteSpace(Recipients) || Recipients.IndexOf('@') == -1)
                        {
                        }
                        Body = $"<h3>{Subject} {configMan.GetAppSetting("Body")}</h3>";
                        string fileExcel = configMan.GetAppSetting("FilePathNameExcel");
                        int iCount;
                        ExcelLibSql excelLib = new ExcelLibSql();
                        iCount = excelLib.SaveFromSQLToNewExcelNewSheet(cmd, fileExcel, "report", log);
                        List<string> arrAttachFiles = new List<string>();
                        if (File.Exists(fileExcel))
                        {
                            arrAttachFiles.Add(fileExcel);
                        }
                        else
                        {
                        }
                        if (CcRecipients.Length > 0)
                        {
                            CcRecipients += ";";
                        }
                        CcRecipients += configMan.GetAppSetting("RecipientsTEST");
                        if (configMan.GetAppSetting("Test") == "True")
                        {
                            Body = "<b>Тест.</b><br/> <br/>" + "Recipients: " + Recipients + "<br/>CcRecipients: " + CcRecipients + "<br/><br/>" + Body;
                            Recipients = configMan.GetAppSetting("RecipientsTEST");
                            CcRecipients = "";
                        }
                        if (ews.SendMessage(log, Recipients, CcRecipients, Subject, Body, BodyType.HTML, arrAttachFiles))
                        {
                            log.WriteToFile($"send mail: {Recipients} copy: {CcRecipients}", true);
                        }
                    }
                }
...