[英]How to show Gridview after exporting to Excel in asp.net c#?
I have wrote a program where the requirement is to when user select particular date then should show one report. 我编写了一个程序,要求用户选择特定日期时应显示一份报告。 And here the report I am showing is in Gridview in a separate page under button click even.
在这里,我显示的报告在Gridview的单独页面中,甚至在按钮单击下。 But now it is required to download the report at the same time they click to view and store into some particular folder.
但是,现在需要在他们单击以查看并存储到某些特定文件夹的同时下载报告。 So, I have written the below code and it is working to download the report into excel format into the particular folder but I cannot see the gridview anymore.
因此,我编写了以下代码,并且正在将报告以excel格式下载到特定的文件夹中,但是我再也看不到gridview了。 I have put the below function into the pageload after binding data with the gridview but now I can see that while debuging the pointer coming to the binding grid and exporting excel again and again and the page shows error " The Web Page cannot be displayed."
在使用gridview绑定数据之后,我将以下函数放入了页面加载中,但是现在我可以看到,调试指向绑定网格的指针并一次又一次导出excel时,页面显示错误“无法显示Web页面”。 Please help me on this how may I fix this.
请帮我解决这个问题。
Here is my code example to write into excel and store into a location: 这是我写到excel并存储到位置的代码示例:
private void ExportPagetoExl()
{
Response.Clear();
Response.Buffer = true;
if (string.IsNullOrEmpty(cpIdStr))
Response.AddHeader("content-disposition", "attachment;filename="+ Request["year"] +"_" + Request["month"] + "_Transaction_" + requestChannel + ".xls");
else
{
Response.AddHeader("content-disposition", "attachment;filename=" + Session["cpUsername"] + "_" + Request["year"] + "_" + Request["fullmonth"] + "_" + requestChannel + ".xls");
}
Response.Charset = string.Empty;
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
FileInfo FI = new FileInfo(@"C:\MyPC\Projects\report\transaction_" + requestChannel + "_" + Request["year"] + "_" + Request["fullmonth"] + ".xls");
GridView1.AllowPaging = false;
GridView1.DataSource = myData.DefaultView;
GridView1.DataBind();
try
{
GridViewRow header = GridView1.HeaderRow;
GridView1.RenderControl(hw);
string style = "<style>.textmode{mso-number-format:\\@;}</style>";
string directory = @"C:\MyPC\Projects\report\";
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
System.IO.StreamWriter vw = new System.IO.StreamWriter(@"C:\MyPC\Projects\report\" + Request["year"] + "_" + Request["month"] + "_Transaction_" + requestChannel + ".xls", true);
sw.ToString().Normalize();
vw.Write(sw.ToString());
vw.Flush();
vw.Close();
Response.Flush();
Response.BufferOutput = true;
Response.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Thanks in advance. 提前致谢。
ASPX Code
ASPX代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
function ExportExcel() { //function to be executed from server side after gridview is bound
$('#btnexport').click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnsubmit" Text="Submit" runat="server" ClientIDMode="Static" OnClick="btnsubmit_Click" />
<asp:Button ID="btnexport" runat="server" ClientIDMode="Static" style="display:none;" OnClick="btnexport_Click" /><!--Hidden button -->
<asp:GridView ID="Gridview1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Code Behind
背后的代码
protected void btnsubmit_Click(object sender, EventArgs e)
{
//Bind Gridview
LoadGridview();
//Call "ExportExcel();" Javascript function which has been defined in the aspx page
ScriptManager.RegisterStartupScript(this, this.GetType(), "com", "ExportExcel();", true);
}
protected void btnexport_Click(object sender, EventArgs e)
{
//export the file here
ExportToExcel();
}
private void LoadGridview()
{
//Your code to bind gridview goes here
}
private void ExportToExcel()
{
//Your code to export goes here
}
Explanation
说明
You need to change the way of doing it. 您需要更改执行方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.