public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.bd();
}
}
//初始绑定
private void bd()
{
string sql = "select * from UserSet";
this.GridView1.DataSource = new s.query.Helper().execquery(sql);
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=tuican.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); //设置输出流为简体中文
Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("http://www.lswjsdc.com/users/", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);//导出GridView1中的数据(想导出几个就写几个)
Response.Write(oStringWriter.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{ }
}
不是很明白你的意思,这是导出信息,你参考下吧
谢谢您的回复,不过我的应用想在winform里面实现。有什么好的方法吗?
for (int i = 0; i < gv.Columns.Count; i++) { if (gv.Columns[i].HeaderText == string.Empty || !gv.Columns[i].ShowHeader) { gv.Columns.RemoveAt(i); } }
遍历DataGridView,提取需要的列,写入一个DataTable或者List<Model>,然后再导出不就行了,还可以在后台重新构造合适的sql语句,查询数据,然后导出。
简单点 直接重新写个获取你想要的信息的方法 再绑定过来