首页 > 解决方案 > c# 在 System.OutOfMemoryException 中导出到 excel

问题描述

我有一个程序:1.对MSSQL执行sql查询2.将sql查询中的行插入excel 3.
如果要插入excel文档的行数超过100000,则通过邮件发送excel文档,然后发生错误:系统.OutOfMemoryException 我在项目属性中设置了值 - 平台目标 = 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);
                        }
                    }
                }

标签: c#asp.netexcelexceptionopenxml

解决方案


推荐阅读