asp.net 导出mysql_ASP.NET 导出数据表格

功能:可以实现导出整个数据表格或整个页面

public bool ExportGv(string fileType, string fileName)

{

bool flag = false;

try

{

//定义文档类型、字符编码

Response.Clear();

Response.Buffer = true;

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

//2.定义导入文档的类型

HttpContext.Current.Response.ContentType = fileType;

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +

System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

this.Gv.Page.EnableViewState = false;

//定义一个输入流

System.IO.StringWriter tw = new System.IO.StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

//将目标数据绑定到输入流输出,这里的this可以换成要导出的控件数据

this.RenderControl(hw);

Response.Output.Write(tw.ToString());

Response.Flush();

Response.End();

this.Gv.AllowPaging = false;

flag = true;

}

catch (Exception ex)

{

flag = false;

throw ex;

}

return flag;

}

有时候导出时出现缺少CSS样式的特性,自此补充

public bool exportAndPrint(string fileType, string fileName)

{

bool flag = false;

try

{

//定义文档类型、字符编码

Response.Clear();

Response.Buffer = true;

HttpContext.Current.Response.Charset = "GB2312";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

//2.定义导入文档的类型

HttpContext.Current.Response.ContentType = fileType;

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +

System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

//定义一个输入流

System.IO.StringWriter tw = new System.IO.StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

//将目标数据绑定到输入流输出

this.RenderControl(hw);

string outStr = "

using (System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/CSS/APDetailInfo.css")))

{

outStr += sr.ReadToEnd();

}

outStr += "  ";

outStr = outStr.Replace("/r/n", "");

outStr = tw.ToString().Replace("", "");

Response.Output.Write(outStr);

Response.Flush();

Response.End();

flag = true;

}

catch (Exception ex)

{

flag = false;

throw ex;

}

return flag;

}

protected void Export_Click(object sender, EventArgs e)

{

TYKY_OA.Web.Listnation pageList = (Listnation)Session["ADAFOTRlistnation"];

if (pageList != null)

{

//由于是导出整个页面,有些控件不想被导出,所以置为空

this.Gv.Columns[10].Visible = false;

//这里是整个DIV的隐藏

this.head.Visible = false;

this.boot.Visible = false;

this.Gv.DataSource = pageList.ObjList;

this.Gv.DataBind();

//ms-word/ms-txt/ms-html/ms-excel

if (!ExportGv("application/ms-excel", "ExportGv.xls"))

{

ClientScript.RegisterClientScriptBlock(this.GetType(), "ExportFailed", "");

}

this.prev.Disabled = !pageList.FlagPrivous;

this.next.Disabled = !pageList.FlagNext;

this.pageIndex.Text = "当前第" + pageList.CurrentPage + "页" + "(共" + pageList.PageCount + "页)";

this.Gv.DataSource = pageList.GetList();

this.Gv.DataBind();

}

}

public override void VerifyRenderingInServerForm(Control control)

{

// base.VerifyRenderingInServerForm(control);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值