we are having an application with large number of data.
while exporting the data from rad grid to excel format we are getting the error :
Exception type: OutOfMemoryException
the Rad Grid contains the 25 columns and nearly 35,000 rows
there is one aspx page on that page load the data will fetch into the RadGrid and then the data will export to Excel or CSV
i pasted the HTML and cs code
can you please assist us on this
<telerik:RadGrid ID="radGrid" runat="server" Visible="false" AutoGenerateColumns="false" OnGridExporting="radGrid_GridExporting"
OnExcelExportCellFormatting="RadGrid1_ExcelExportCellFormatting">
<ExportSettings HideStructureColumns="true" />
</telerik:RadGrid>
the .CS Code :
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["downloadoption"] != null)
{
LoadData();
}
}
private void LoadData()
{
Logger.LogInformation("Start", "btnSaveAs_Click", "DownloadFormat");
if (Request.QueryString["ReportPagerName"] != null)
{
ReportPager = Request.QueryString["ReportPagerName"];
}
if (Session[ReportPager] != null)
{
int reportIntId = 0;
Int32.TryParse(Request.QueryString[ReportID], out reportIntId);
//string reportID = Request.QueryString[ReportID];
this.CheckDataAccess(DataAccessType.Report, null, null, null, null, null, null, null, reportIntId);
if (!IsTransactionDetailsPage(reportIntId))
{
reportPager = (ReportPager)Session[ReportPager];
AddColumnsToGrid();
SetExportSettings();
BindDataTable();
}
else
{
if (Session["TransactionDetails"] != null)
{
DataTable dt = (DataTable)Session["TransactionDetails"];
this.radGrid.MasterTableView.Columns.Clear();
foreach (DataColumn dc in dt.Columns)
{
GridBoundColumn gbc1 = new GridBoundColumn();
gbc1.DataField = dc.ColumnName;
gbc1.HeaderText = string.Empty;
this.radGrid.MasterTableView.Columns.Add(gbc1);
}
this.radGrid.DataSource = dt;
this.radGrid.DataBind();
}
}
DownloadDataGrid();
}
Logger.LogInformation("End", "btnSaveAs_Click", "DownloadFormat");
}
private void AddColumnsToGrid()
{
Logger.LogInformation("Start", "AddColumnsToGrid", "DownloadFormat");
this.radGrid.MasterTableView.Columns.Clear();
foreach (GridField entry in reportPager.ColumnDictionary.Values)
{
if (entry.IsDisplayField)
{
GridBoundColumn gbc1 = new GridBoundColumn();
gbc1.DataField = entry.DataField;
gbc1.HeaderText = entry.HeaderText;
this.radGrid.MasterTableView.Columns.Add(gbc1);
}
}
Logger.LogInformation("End", "AddColumnsToGrid", "DownloadFormat");
}
private void SetExportSettings()
{
Logger.LogInformation("Start", "SetExportSettings", "DownloadFormat");
this.radGrid.ExportSettings.ExportOnlyData = true;
this.radGrid.ExportSettings.IgnorePaging = true;
this.radGrid.ExportSettings.OpenInNewWindow = true;
this.radGrid.ExportSettings.FileName = ConfigurationManager.AppSettings["DownloadDataFileName"];
Logger.LogInformation("End", "SetExportSettings", "DownloadFormat");
}
private void BindDataTable()
{
Logger.LogInformation("Start", "BindDataTable", "DownloadFormat");
this.reportPager.StartRow = 1;
this.reportPager.EndRow = this.reportPager.GetRowCount();
DataTable dt;
//Excel
if (Request.QueryString["downloadoption"].ToString() == "EXCEL")
dt = this.reportPager.GetDataTable(false, false);
else
dt = this.reportPager.GetDataTable(false);
this.radGrid.DataSource = dt;
this.radGrid.DataBind();
Logger.LogInformation("End", "BindDataTable", "DownloadFormat");
}
private void DownloadDataGrid()
{
Logger.LogInformation("Start", "DownloadData", "DownloadFormat");
this.radGrid.Visible = true;
//CSV
if (Request.QueryString["downloadoption"].ToString() == "CSV")
{
//if (SelRadioButtonList1.SelectedItem.Text == SelRadioButtonList1.Items[2].Text){
this.SetCSVDelimiter();
this.radGrid.MasterTableView.ExportToCSV();
}
//Excel
else if (Request.QueryString["downloadoption"].ToString() == "EXCEL")
{
//else if (SelRadioButtonList1.SelectedItem.Text == SelRadioButtonList1.Items[0].Text)
//{
this.radGrid.MasterTableView.ExportToExcel();
}
}
protected void radGrid_GridExporting(object source, GridExportingArgs e)
{
Logger.LogInformation("Start", "radGrid_GridExporting", "DownloadFormat");
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.BufferOutput = true;
if (e.ExportType == ExportType.Excel || e.ExportType == ExportType.Csv)
{
string filename = radGrid.ExportSettings.FileName;
string fileExtension = radGrid.ExportSettings.Excel.FileExtension;
if (e.ExportType == ExportType.Csv)
fileExtension = radGrid.ExportSettings.Csv.FileExtension;
this.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "." + fileExtension + "\"");
if (e.ExportType == ExportType.Excel)
{
//specify the number decimal separator and group separator being used.
string css = "<style type='text/css'><!--table {mso-displayed-decimal-separator:'\\" + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
css += "'; mso-displayed-thousand-separator:'\\" + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator + "';} ";
css += this.AddDateFormatCSSClass();
css += "--> </style>";
//css += @"<script type=""text/javascript"" language=""javascript"">window.opener.Invoked();</script>";
e.ExportOutput = e.ExportOutput.Replace("</head>", css + "</head>");
}
}
if (Request.QueryString["downloadoption"].ToString() == "CSV")
{
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Type", "text/csv");
Response.BinaryWrite(new UnicodeEncoding().GetBytes(e.ExportOutput));
Response.Flush();
Response.End();
}
Logger.LogInformation("End", "radGrid_GridExporting", "DownloadFormat");
}
while exporting the data from rad grid to excel format we are getting the error :
Exception type: OutOfMemoryException
the Rad Grid contains the 25 columns and nearly 35,000 rows
there is one aspx page on that page load the data will fetch into the RadGrid and then the data will export to Excel or CSV
i pasted the HTML and cs code
can you please assist us on this
OnExcelExportCellFormatting="RadGrid1_ExcelExportCellFormatting">
<ExportSettings HideStructureColumns="true" />
</telerik:RadGrid>
the .CS Code :
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["downloadoption"] != null)
{
LoadData();
}
}
private void LoadData()
{
Logger.LogInformation("Start", "btnSaveAs_Click", "DownloadFormat");
if (Request.QueryString["ReportPagerName"] != null)
{
ReportPager = Request.QueryString["ReportPagerName"];
}
if (Session[ReportPager] != null)
{
int reportIntId = 0;
Int32.TryParse(Request.QueryString[ReportID], out reportIntId);
//string reportID = Request.QueryString[ReportID];
this.CheckDataAccess(DataAccessType.Report, null, null, null, null, null, null, null, reportIntId);
if (!IsTransactionDetailsPage(reportIntId))
{
reportPager = (ReportPager)Session[ReportPager];
AddColumnsToGrid();
SetExportSettings();
BindDataTable();
}
else
{
if (Session["TransactionDetails"] != null)
{
DataTable dt = (DataTable)Session["TransactionDetails"];
this.radGrid.MasterTableView.Columns.Clear();
foreach (DataColumn dc in dt.Columns)
{
GridBoundColumn gbc1 = new GridBoundColumn();
gbc1.DataField = dc.ColumnName;
gbc1.HeaderText = string.Empty;
this.radGrid.MasterTableView.Columns.Add(gbc1);
}
this.radGrid.DataSource = dt;
this.radGrid.DataBind();
}
}
DownloadDataGrid();
}
Logger.LogInformation("End", "btnSaveAs_Click", "DownloadFormat");
}
private void AddColumnsToGrid()
{
Logger.LogInformation("Start", "AddColumnsToGrid", "DownloadFormat");
this.radGrid.MasterTableView.Columns.Clear();
foreach (GridField entry in reportPager.ColumnDictionary.Values)
{
if (entry.IsDisplayField)
{
GridBoundColumn gbc1 = new GridBoundColumn();
gbc1.DataField = entry.DataField;
gbc1.HeaderText = entry.HeaderText;
this.radGrid.MasterTableView.Columns.Add(gbc1);
}
}
Logger.LogInformation("End", "AddColumnsToGrid", "DownloadFormat");
}
private void SetExportSettings()
{
Logger.LogInformation("Start", "SetExportSettings", "DownloadFormat");
this.radGrid.ExportSettings.ExportOnlyData = true;
this.radGrid.ExportSettings.IgnorePaging = true;
this.radGrid.ExportSettings.OpenInNewWindow = true;
this.radGrid.ExportSettings.FileName = ConfigurationManager.AppSettings["DownloadDataFileName"];
Logger.LogInformation("End", "SetExportSettings", "DownloadFormat");
}
private void BindDataTable()
{
Logger.LogInformation("Start", "BindDataTable", "DownloadFormat");
this.reportPager.StartRow = 1;
this.reportPager.EndRow = this.reportPager.GetRowCount();
DataTable dt;
//Excel
if (Request.QueryString["downloadoption"].ToString() == "EXCEL")
dt = this.reportPager.GetDataTable(false, false);
else
dt = this.reportPager.GetDataTable(false);
this.radGrid.DataSource = dt;
this.radGrid.DataBind();
Logger.LogInformation("End", "BindDataTable", "DownloadFormat");
}
private void DownloadDataGrid()
{
Logger.LogInformation("Start", "DownloadData", "DownloadFormat");
this.radGrid.Visible = true;
//CSV
if (Request.QueryString["downloadoption"].ToString() == "CSV")
{
//if (SelRadioButtonList1.SelectedItem.Text == SelRadioButtonList1.Items[2].Text){
this.SetCSVDelimiter();
this.radGrid.MasterTableView.ExportToCSV();
}
//Excel
else if (Request.QueryString["downloadoption"].ToString() == "EXCEL")
{
//else if (SelRadioButtonList1.SelectedItem.Text == SelRadioButtonList1.Items[0].Text)
//{
this.radGrid.MasterTableView.ExportToExcel();
}
}
protected void radGrid_GridExporting(object source, GridExportingArgs e)
{
Logger.LogInformation("Start", "radGrid_GridExporting", "DownloadFormat");
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.BufferOutput = true;
if (e.ExportType == ExportType.Excel || e.ExportType == ExportType.Csv)
{
string filename = radGrid.ExportSettings.FileName;
string fileExtension = radGrid.ExportSettings.Excel.FileExtension;
if (e.ExportType == ExportType.Csv)
fileExtension = radGrid.ExportSettings.Csv.FileExtension;
this.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "." + fileExtension + "\"");
if (e.ExportType == ExportType.Excel)
{
//specify the number decimal separator and group separator being used.
string css = "<style type='text/css'><!--table {mso-displayed-decimal-separator:'\\" + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator;
css += "'; mso-displayed-thousand-separator:'\\" + Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberGroupSeparator + "';} ";
css += this.AddDateFormatCSSClass();
css += "--> </style>";
//css += @"<script type=""text/javascript"" language=""javascript"">window.opener.Invoked();</script>";
e.ExportOutput = e.ExportOutput.Replace("</head>", css + "</head>");
}
}
if (Request.QueryString["downloadoption"].ToString() == "CSV")
{
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Type", "text/csv");
Response.BinaryWrite(new UnicodeEncoding().GetBytes(e.ExportOutput));
Response.Flush();
Response.End();
}
Logger.LogInformation("End", "radGrid_GridExporting", "DownloadFormat");
}