将大型gridview数据导出到Excel工作表中
问题描述:
将大量数据导出到Excel工作表时出现异常超时错误。
我的gridview中有超过40,000条记录。
我的代码在下面
我使用telerik radgrid将radgrid数据导出到excel。
我收到错误消息
日志条目字符串太长。写入的字符串事件日志不能超过32766个字符。
Getting exception time out error when i export the huge data into excel sheet.
There are more than 40,000 records in my gridview.
My code is below
iam using telerik radgrid to export the radgrid data into excel.
iam getting the error message as
"Log entry string is too long. A string written to the event log cannot exceed 32766 characters."
protected void exportButton_Click(object sender, System.EventArgs e)
{
RadGrid1.ExportSettings.OpenInNewWindow = true;
RadGrid1.ExportSettings.FileName = "AgingDetail";
RadGrid1.ExportSettings.ExportOnlyData = true;
RadGrid1.ExportSettings.OpenInNewWindow = true;
RadGrid1.ExportSettings.IgnorePaging = true;
foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
{
item.Visible = false;
}
RadGrid1.MasterTableView.GetColumn("supname").Display = true;
RadGrid1.MasterTableView.GetColumn("repname").Display = true;
RadGrid1.MasterTableView.GetColumn("centername").Display = true;
RadGrid1.MasterTableView.GetColumn("Priority").Display = false;
RadGrid1.MasterTableView.GetColumn("Cust_Num").Display = false;
RadGrid1.MasterTableView.GetColumn("Cust_Num1").Display = true;
RadGrid1.MasterTableView.GetColumn("ins_num").Display = false;
RadGrid1.MasterTableView.GetColumn("ins_num1").Display = true;
RadGrid1.MasterTableView.ExportToExcel();
}
答
嗨raheem,
尝试这可能对你有用。
Hi raheem,
Try this may it work for you.
string FileName = "StudentAttendanceReport";
// DataTable dt = ds.Tables[0];
Context.Response.Write("<table border=1 width='100%'><tr align = 'center'>");
for (int i = 0; i < dt.Columns.Count; i++)
{
Context.Response.Write("<td><B>" + dt.Columns[i].ToString() + "</B></td>");
}
Context.Response.Write("</tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
Context.Response.Write("<tr align = 'Left'>");
for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
{
switch (dr[iCol].ToString())
{
case "/":
Context.Response.Write("<td nowrap>" + "" + "</td>");
break;
default:
Context.Response.Write("<td nowrap>" + dr[iCol].ToString() + "</td>");
break;
}
}
Context.Response.Write("</tr>");
}
Context.Response.Write("</table>");
Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
Context.Response.ContentType = "application/vnd.ms-excel";
Context.Response.Charset = "";
Context.Response.Flush();
Context.Response.Close();
Context.Response.End();
}
catch (Exception)
{
// throw;
}
由于您有大量数据,因此可以在获取时增加命令对象的超时时间: -
As there is a large amount of data for you you can increase the timeout time for your command object while fetching :-
SqlCommand cmd = new SqlCommand("your query", {connection object});
cmd.CommandTimeout = 999999 ;
希望这会为你工作。
Hope this will work for you.