/// <summary>
/// 导出文件
/// </summary>
/// <param name="FileType">导出文件的类型,如:"application/ms-excel"</param>
/// <param name="FileName">导出文件的名称,"报表.xls"</param>
/// <param name="gv">从gridview导出</param>
private void Export(string FileType, string FileName,GridView gv)
{
try
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7; // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); //防止中文文件名乱码
Response.ContentType = FileType; //导出的文件类型,表格 application/ms-excel,文档 application/ms-word
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('导出数据出错!" + ex.Message + "')</script>");
}
}
//必须的,否则会报错
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}
/// 导出文件
/// </summary>
/// <param name="FileType">导出文件的类型,如:"application/ms-excel"</param>
/// <param name="FileName">导出文件的名称,"报表.xls"</param>
/// <param name="gv">从gridview导出</param>
private void Export(string FileType, string FileName,GridView gv)
{
try
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7; // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); //防止中文文件名乱码
Response.ContentType = FileType; //导出的文件类型,表格 application/ms-excel,文档 application/ms-word
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
catch (Exception ex)
{
Response.Write("<script language=javascript>alert('导出数据出错!" + ex.Message + "')</script>");
}
}
//必须的,否则会报错
public override void VerifyRenderingInServerForm(Control control)
{
// base.VerifyRenderingInServerForm(control);
}

浙公网安备 33010602011771号