优化的Infragistics Excel

最近我们遇到了关于使用infragistics库将报告导出到excel文件的问题。

我们的报告表包含了或多或less的126万条logging。 我们正在尝试为此创build一个excel(2007)文件,所以大概这将是2个工作表,因为每张工作表只支持大约100万行。

在创build工作表的过程中以及将数据填入工作表的过程中,我们没有任何问题,在这一点上内存是可以pipe理的。

我们的问题是,当写入Excel中的数据stream(文件,内存)。 内存消耗太高,达到1.8GB时将不再继续。

PS这是一个总结报告,客户希望这在一个单一的Excel文件。

你的build议非常感谢!

下面是我已经从实际的源代码,但我已经取代了数据检索部分,而是使用虚拟数据。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; using System.Runtime; using System.Diagnostics; using Infragistics.Excel; namespace ConsoleApplication1 { class Program { static bool tip = true; static void Main(string[] args) { var xls = Workbook.MaxExcelRowCount; var xlsx =Workbook.MaxExcel2007RowCount; var wb = new Workbook(WorkbookFormat.Excel2007, WorkbookPaletteMode.StandardPalette); var ws = wb.Worksheets.Add("Sheet1"); var sheetCtr = 1; var limit = 1256898; var rr = 0; for (var row = 0; row < limit; row++) { if (rr >= xlsx - 1) { //create new worksheet if row exceeded the limit ws = wb.Worksheets.Add("Sheet" + sheetCtr.ToString()); rr = 0; ClearMemory(); } for (var col = 0; col < 10; col++) { ws.Rows[rr].Cells[col].Value = string.Format("data{0}-{1}", row, col); } Console.Write("Row="); Console.Write(row.ToString()); Console.WriteLine(); rr++; } Console.Write("Saving worksheet..."); //this part uses too much memory wb.Save("wb2.xlsx"); Console.WriteLine("Workbook saved."); Console.WriteLine("Done"); Console.Read(); } static void ClearMemory() { try { System.Diagnostics.Process proc = System.Diagnostics.Process.GetCurrentProcess(); if (tip == true) { proc.MaxWorkingSet = (IntPtr)((int)proc.MaxWorkingSet - 1); proc.MinWorkingSet = (IntPtr)((int)proc.MinWorkingSet - 1); } else { proc.MaxWorkingSet = (IntPtr)((int)proc.MaxWorkingSet + 1); proc.MinWorkingSet = (IntPtr)((int)proc.MinWorkingSet + 1); } tip = !tip; } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } } }