输出excel页面自动关闭,无提示“打开”“保存”“取消”对话框
直接运行这个页面,如果在本页设置reportid = "28",直接运行本页面可以提示保存,如果不赋值直接运行本页面也能正常打印出设置的错误提示,但如果是从其他页面提交的数据过来,不论有没有在该页面设置初始值都是一样的情况,新打开的页面闪一下就自动关闭了,没有任何提示!
我的源文件如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.IO;
public partial class ExportExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
string reportid = Request.Form["reportid"];
reportid = "28";
if (reportid == "")
{
Response.Write("页面错误");
}
else
{
//Response.Write(reportid);
Maticsoft.OAchargeReportTitle getTle = new Maticsoft.OAchargeReportTitle();
DataSet getTINFO = getTle.GetList("id=" + reportid);
string reportTile = getTINFO.Tables[0].Rows[0]["report"].ToString();
DateTime st = Convert.ToDateTime(getTINFO.Tables[0].Rows[0]["stime"]);
DateTime et = Convert.ToDateTime(getTINFO.Tables[0].Rows[0]["etime"]);
Maticsoft.OAchargeReport getValueInfo = new Maticsoft.OAchargeReport();
DataSet getValueInfoDS = getValueInfo.GetList("reportid=" + reportid + " and sendtime between '" + st + "' and '" + et + "'");
GridView1.DataSource = getValueInfoDS;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8"; //("GB2312");为简体中文
Response.AppendHeader("Content-Disposition", "attachment;filename=" + reportTile + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); //GB231
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
//Response.End();
}
}
catch
{
Response.Write("页面错误");
}
}
}
public override void VerifyRenderingInServerForm(Control control){}
}
------解决方案--------------------
先绑定数据到gridview,在导出
或使用模板
string fileName = "";
string filePath = Server.MapPath("");//路径
FileInfo fileInfo = new FileInfo(filePa