Asp.net数据网格导出到excel c#问题
本文关键字:excel 问题 net 数据 数据网 网格 Asp | 更新日期: 2023-09-27 18:10:12
我有经典的asp.net网站。我有一个数据网格,我想导出为excel表。该站点使用windows身份验证。
我拿起代码,而谷歌的解决方案。以下是我在FF和Chrome中遇到的问题,需要帮助来纠正它:
-
有时文件下载将发生没有任何问题,但如果我保持窗口不活动一分钟左右,然后再试一次,要么我得到页面没有发现问题,要么浏览器将继续要求登录(一种卡住在循环中)。
-
有时当导出按钮被点击,FF只是继续显示窗口登录弹出,就像它被困在一个循环。
按钮:
<asp:Button ID="cmdExportDataGridToExcel_Click" runat="server" OnClick="cmdExportToExcel_Click" CssClass="StandardText" Text="Export to Excel" Width="120px" Height="33px"></asp:Button>
代码:
protected void cmdExportDataGridToExcel_Click(object sender, System.EventArgs e)
{
//export to excel
Response.Clear();
var userName = CommonMethods.GetUserId();
var fileName = string.Format("ScheduledOrdersReport-{0}-{1:yyyyMMddhhmmsstt}.xls", userName, DateTime.Now);
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
hsGrid.AllowPaging = false;
this.BindHSGrid(false, string.Empty);
//this.EnableViewState = false;
var oStringWriter = new StringWriter();
var oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
this.ClearControls(hsGrid);
hsGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
private void ClearControls(Control control)
{
for (int i = control.Controls.Count - 1; i >= 0; i--)
{
ClearControls(control.Controls[i]);
}
if (!(control is TableCell))
{
if (control.GetType().GetProperty("SelectedItem") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
try
{
literal.Text = (string)control.GetType().GetProperty("SelectedItem").GetValue(control, null);
}
catch
{
}
control.Parent.Controls.Remove(control);
}
else if (control.GetType().GetProperty("Text") != null)
{
LiteralControl literal = new LiteralControl();
control.Parent.Controls.Add(literal);
literal.Text = (string) control.GetType().GetProperty("Text").GetValue(control, null);
control.Parent.Controls.Remove(control);
}
}
}
更新1 我通过将数据放在会话中然后从会话创建excel来测试它。它也会导致同样的错误。
var dv = (DataView) Session["ScheduledOrdersReport"];
Response.Clear();
var userName = CommonMethods.GetUserId();
var fileName = string.Format("ScheduledOrdersReport-{0}-{1:yyyyMMddhhmmsstt}.xls", userName, DateTime.Now);
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel"; //"application/vnd.xls";
var stringWrite = new StringWriter();
var htmlWrite = new HtmlTextWriter(stringWrite);
var g = new GridView();
g.DataSource = dv;
g.DataBind();
g.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
更新2 我只是做了另一个测试,打开一个弹出窗口,然后使用更新1代码。没有问题,点击或保持页面一段时间,然后再次点击按钮。
看起来是post back和response clear和end的问题。
我现在根本找不到任何东西来正确地解决这个问题。也许我需要休息一下。
如何启动web客户端?并设置NetworkCredentials?那么你不能每次都登录…
:
`using(Webclient webclient = new Webclient())
{
webClient http = new Webclient();
http.Credentials = new NetworkCredential("YourUsername", "YourPassword");
}
"