asp.net(C#)如何将查询到的数据导出Excel表格?
我是菜鸟,望前辈详细指点:说明:我是用asp.net(C#)做查询的,查询数据显示在页面的lable控件上.现在我想根据查询条件将查询到的数据导出到Excel表格中,每一...
我是菜鸟,望前辈详细指点:
说明:我是用asp.net(C#)做查询的,查询数据显示在页面的lable控件上.现在我想根据查询条件将查询到的数据导出到Excel表格中,每一步都要怎么做.能不能讲详细一点?采纳者追加100分!
是用lable显示,不用其它控件显示 展开
说明:我是用asp.net(C#)做查询的,查询数据显示在页面的lable控件上.现在我想根据查询条件将查询到的数据导出到Excel表格中,每一步都要怎么做.能不能讲详细一点?采纳者追加100分!
是用lable显示,不用其它控件显示 展开
展开全部
你好,可以根据DataGrid将数据导出到excel的方法,原理都是一样的
public void ExportToExcel(Page myPage,DataGrid ctl,string filename)
{
HttpResponse Response;
Response=myPage.Response;
bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true;
Response.AppendHeader("Content-Disposition","attachment;filename="+filename+".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}
public void ExportToExcel(Page myPage,DataGrid ctl,string filename)
{
HttpResponse Response;
Response=myPage.Response;
bool CurrCtlVisible=ctl.Visible;
ctl.Visible=true;
Response.AppendHeader("Content-Disposition","attachment;filename="+filename+".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
ctl.Page.EnableViewState = true;
ctl.Visible=CurrCtlVisible;
}
展开全部
建议将查询出来的数据绑定到DATAGRID里面显示,这样的话导出EXCEL就很方便了,具体代码为:
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Today.ToString("yyyyMMdd") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
// 将datagrid输出,你的控件是label此处写上label即可
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Today.ToString("yyyyMMdd") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
// 将datagrid输出,你的控件是label此处写上label即可
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有个最简单的方法,直接使用报表控件。水晶报表也可以。里面都有导出成EXCEL和PDF的功能。对于初学者是个很好的功能。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
参考资料: http://zhidao.baidu.com/question/81623619.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询