"Unable to evaluate expression because the code is optimized or a native frame is on top of the stack" 同时将网格导出到 excel
"Unable to evaluate expression because the code is optimized or a native frame is on top of the stack" while export the grid into excel
将选定的网格行导出到 excel 时出错。
错误:无法计算表达式,因为代码已优化或本机框架位于堆栈顶部。
其实我想把选中的行从网格中导出到excel,这样用户就可以方便的下载选中的记录
请参考导出代码到 excel Onclick 方法。
protected void btnExportExcel_Click(object sender, EventArgs e)
{
bool isSelected = false;
foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
{
CheckBox cb = (CheckBox)i.FindControl("Chkbox");
if (cb != null && cb.Checked)
{
isSelected = true;
break;
}
}
if (isSelected)
{
GridView gvExport = gvDistrictSchoolReport;
// this below line for not export checkbox to excel file
gvExport.Columns[0].Visible = false;
foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
{
gvExport.Rows[i.RowIndex].Visible = false;
CheckBox cb = (CheckBox)i.FindControl("Chkbox");
if (cb != null && cb.Checked)
{
gvExport.Rows[i.RowIndex].Visible = true;
}
}
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htW = new HtmlTextWriter(sw);
gvExport.RenderControl(htW);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
如果您使用的是更新面板,请像这样使用 PostBack 触发器
<asp:UpdatePanel ID="up1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="gvDistrictSchoolReport" /> -- for grid
<asp:PostBackTrigger ControlID="Button6" /> -- for export to excel button
</Triggers>
格子前。希望这会奏效
将选定的网格行导出到 excel 时出错。 错误:无法计算表达式,因为代码已优化或本机框架位于堆栈顶部。
其实我想把选中的行从网格中导出到excel,这样用户就可以方便的下载选中的记录 请参考导出代码到 excel Onclick 方法。
protected void btnExportExcel_Click(object sender, EventArgs e)
{
bool isSelected = false;
foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
{
CheckBox cb = (CheckBox)i.FindControl("Chkbox");
if (cb != null && cb.Checked)
{
isSelected = true;
break;
}
}
if (isSelected)
{
GridView gvExport = gvDistrictSchoolReport;
// this below line for not export checkbox to excel file
gvExport.Columns[0].Visible = false;
foreach (GridViewRow i in gvDistrictSchoolReport.Rows)
{
gvExport.Rows[i.RowIndex].Visible = false;
CheckBox cb = (CheckBox)i.FindControl("Chkbox");
if (cb != null && cb.Checked)
{
gvExport.Rows[i.RowIndex].Visible = true;
}
}
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htW = new HtmlTextWriter(sw);
gvExport.RenderControl(htW);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
如果您使用的是更新面板,请像这样使用 PostBack 触发器
<asp:UpdatePanel ID="up1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="gvDistrictSchoolReport" /> -- for grid
<asp:PostBackTrigger ControlID="Button6" /> -- for export to excel button
</Triggers>
格子前。希望这会奏效