导出excel
public void GridViewToExcel(System.Web.UI.Control ctl, string ExportFileName)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel"; ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End();
}
由于 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8) + ".xls");重写了header使得原来正常的脚本出现了“未指明错误”。请问大家都是这么处理或避免这个问题?要是使用弹出第三个页面导出的话该如何传这个gridview过去呢?请求高手高招!
public void GridViewToExcel(System.Web.UI.Control ctl, string ExportFileName)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8) + ".xls"); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/ms-excel"; ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End();
}
由于 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(ExportFileName, System.Text.Encoding.UTF8) + ".xls");重写了header使得原来正常的脚本出现了“未指明错误”。请问大家都是这么处理或避免这个问题?要是使用弹出第三个页面导出的话该如何传这个gridview过去呢?请求高手高招!
<%response.ContentType ="application/vnd.ms-Excel"%>
Response.Buffer= true;
Response.Charset="utf-8";
Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
----------------------------------------------
你看的文件名是否出现了问题,你作为变量传入的,可以跟综下
/// <summary>
/// DataTable To Excel
/// </summary>
/// <param name="dt">DataTable Name</param>
/// <param name="typeid">1,Excel 2,XML</param>
/// <param name="FileName">文件名</param>
public void CreateExcel(DataTable dt, string typeid, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.ContentType = "application/ms-excel";
resp.AddHeader("Content-Disposition","attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
this.EnableViewState = false;
string colHeaders = "", Is_item = ""; //colHeaders,表头,Is_item ,详细数据
int i = 0; //定义表对象与行对象,同时使用DataSet对其值进行初始化
DataRow[] myRow = dt.Select("");
//typeid=="1"时导出为Excel格式文件;typeid=="2"时导出为XML文件
if (typeid == "1")
{
//取得数据表各列标题,标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < dt.Columns.Count; i++)
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
colHeaders += "\n"; resp.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列取得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dt.Columns.Count; i++)
{
Is_item += row[i].ToString() + "\t";
}
Is_item += "\n";
resp.Write(Is_item);
Is_item = "";
}
}
else
{
if (typeid == "2")
{
//从DataTable中直接导出XML数据并且写到HTTP输出流中
resp.Write(dt.DataSet.GetXml());
}
}
//写缓冲区中的数据到HTTP头文件中
resp.End();
}