XtraGrid – 导出到Excel

我正在使用Developer Express XtraGrid组件来显示一些数据。 我的Windows应用程序表单上有2个XtraGrid 。 两个网格都有超过200k +行和8列数据,我已导出到excel按钮。 有两种方法(据我所知)将网格数据导出为ex​​cel。

1- grid.ExportToXls();grid.ExportToXlsx();

2-使用Office Interop和OpenXML Utilities

如果我使用grid.ExportToXls();grid.ExportToXlsx(); ,处理时间比Office Interop代码快(对于大约2k行数据)。 但是,这种方法只能用于1格。 因此,结果显示在2个不同的Excel文件中。 因此,我使用Office Interop在流程完成后合并工作簿。 这是出现问题。 通过这两种方式,我总是得到System.OutOfMemory Exception。 (见下面的记忆图) 在此处输入图像描述

我被困在这里,因为我知道导出excel的方法是抛出System.OutOfMemory Exception。 您有什么建议吗,如何向Excel输出超过200k-300k +的数据行? 我在Visual Studio 2010上使用.Net Framework 3.5 。 您可以在下面找到我的Interop和Document.Format OpenXML Utility代码。

 try { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Title = SaveAsTitle; saveDialog.Filter = G.Instance.MessageManager.GetResourceMessage("EXCEL_FILES_FILTER"); saveDialog.ShowDialog(); if (string.IsNullOrEmpty(saveDialog.FileName)) { // Showing Warning return; } List exportToExcel = new List(); exportToExcel.Add(dataGrid); exportToExcel.Add(summaryGrid); ExportXtraGridToExcel2007(saveDialog.FileName, exportToExcel); } catch (Exception ex) { // Showing Error } 

这是我的ExportXtraGridToExcel2007(); function代码

 public void ExportXtraGridToExcel2007(string path, List grids) { try { DisableMdiParent(); string tmpPath = Path.GetTempPath(); List exportedFiles = new List(); for (int i = 0; i  0) { OpenXmlUtilities.MergeWorkbooks(path, exportedFiles.ToArray()); foreach (string excel in exportedFiles) { if (File.Exists(excel)) { try { File.Delete(excel); } catch (Exception ex) { EventLog.WriteEntry("Application", ex.Message); } } } } } catch (Exception ex) { // showing error } finally { EnableMdiParent(); } } 

这是OpenXML Merge Work Books Codes

 public static void MergeWorkbooks(string path, string[] sourceWorkbookNames) { WorkbookPart mergedWorkbookPart = null; WorksheetPart mergedWorksheetPart = null; WorksheetPart childWorksheetPart = null; Sheets mergedWorkbookSheets = null; Sheets childWorkbookSheets = null; Sheet newMergedSheet = null; SheetData mergedSheetData = null; SharedStringTablePart mergedSharedStringTablePart = null; SharedStringTablePart childSharedStringTablePart = null; // Create the merged workbook package. using (SpreadsheetDocument mergedWorkbook = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook)) { // Add the merged workbook part to the new package. mergedWorkbookPart = mergedWorkbook.AddWorkbookPart(); GenerateMergedWorkbook().Save(mergedWorkbookPart); // Get the Sheets element in the merged workbook for use later. mergedWorkbookSheets = mergedWorkbookPart.Workbook.GetFirstChild(); // Create the Shared String Table part in the merged workbook. mergedSharedStringTablePart = mergedWorkbookPart.AddNewPart(); GenerateSharedStringTablePart().Save(mergedSharedStringTablePart); // For each source workbook to merge... foreach (string workbookName in sourceWorkbookNames) { // Open the source workbook. The following will throw an exception if // the source workbook does not exist. using (SpreadsheetDocument childWorkbook = SpreadsheetDocument.Open(workbookName, false)) { // Get the Sheets element in the source workbook. childWorkbookSheets = childWorkbook.WorkbookPart.Workbook.GetFirstChild(); // Get the Shared String Table part of the source workbook. childSharedStringTablePart = childWorkbook.WorkbookPart.SharedStringTablePart; // For each worksheet in the source workbook... foreach (Sheet childSheet in childWorkbookSheets) { // Get a worksheet part for the source worksheet using it's relationship Id. childWorksheetPart = (WorksheetPart)childWorkbook.WorkbookPart.GetPartById(childSheet.Id); // Add a worksheet part to the merged workbook based on the source worksheet. mergedWorksheetPart = mergedWorkbookPart.AddPart(childWorksheetPart); // There should be only one worksheet that is set as the main view. CleanView(mergedWorksheetPart); // Create a Sheet element for the new sheet in the merged workbook. newMergedSheet = new Sheet(); // Set the Name, Id, and SheetId attributes of the new Sheet element. newMergedSheet.Name = GenerateWorksheetName(mergedWorkbookSheets, childSheet.Name.Value); newMergedSheet.Id = mergedWorkbookPart.GetIdOfPart(mergedWorksheetPart); newMergedSheet.SheetId = (uint)mergedWorkbookSheets.ChildElements.Count + 1; // Add the new Sheet element to the Sheets element in the merged workbook. mergedWorkbookSheets.Append(newMergedSheet); // Get the SheetData element of the new worksheet part in the merged workbook. mergedSheetData = mergedWorksheetPart.Worksheet.GetFirstChild(); // For each row of data... foreach (Row row in mergedSheetData.Elements()) { // For each cell in the row... foreach (Cell cell in row.Elements()) { // If the cell is using a shared string then merge the string // from the source workbook into the merged workbook. if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString) { ProcessCellSharedString(mergedWorksheetPart, cell, mergedSharedStringTablePart, childSharedStringTablePart); } } } } } } //Save the changes to the merged workbook. mergedWorkbookPart.Workbook.Save(); } } 

我会使用XtraGrid中的内置方法导出到excel,因为你说它更快。

生成两个excel文件后,我将仅使用Excel Interop程序集将同一文件中的两个结果文件合并到同一工作簿中的两个单独的Excel表格中。

所以你的问题不再是XtraGrid,而只是如何将两个文件合并到一个分离的工作表中,已经讨论了很多次,你会在网上找到解决方案,例如: 如何将2个Excel文件合并到一个excel文件中分开的床单?

我知道这是一个迟到的答案但是,您可以使用devExpress方法和组件将多个网格导出到同一个excel文件。 (也许在旧版本中不是这种情况,我不确定它什么时候可用)

将printableComponentLink添加到每个gridControl,然后创建一个可以添加每个printableComponent链接的compositeLink。

然后,您将使用compositeLink.ExportToXlsx方法。 如果使用XlsxExportOptions.ExportMode属性等于SingleFilePageByPage创建XlsxExportOptions并将其传递给CompositeLink.ExportToXlsx方法,则每个页面都将导出到单独的工作表。

这篇文章引起了我的注意。