如何把datagrid中显示的数据导出到excel表中

alangfl 2006-02-10 09:45:25
如何把datagrid中显示的数据导出到excel表中,我是初学者,详细点好吗?谢谢!!!
...全文
142 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
marysxj 2006-02-10
  • 打赏
  • 举报
回复
楼上各位的方法如果DataGrid有分页了,实际上只能导出当前的页的内容,怎么解决这个问题呢?
qhh389 2006-02-10
  • 打赏
  • 举报
回复
public static void Export(System.Web.UI.Page page,System.Data.DataTable tab,string FileName)
{
System.Web.HttpResponse httpResponse = page.Response;
System.Web.UI.WebControls.DataGrid dataGrid=new System.Web.UI.WebControls.DataGrid();
dataGrid.DataSource=tab.DefaultView;
dataGrid.AllowPaging = false;
//dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Green;
dataGrid.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
//dataGrid.HeaderStyle.Font.Bold = true;
dataGrid.DataBind();
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8)); //filename="*.xls";
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
httpResponse.ContentType ="application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dataGrid.RenderControl(hw);

string filePath = System.Configuration.ConfigurationSettings.AppSettings["upload"] +FileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();

DownFile(httpResponse,FileName,filePath);

httpResponse.End();
}
blackhero 2006-02-10
  • 打赏
  • 举报
回复
用第三方控件:farpoint
songxiaozhao 2006-02-10
  • 打赏
  • 举报
回复
其中LogGrid是定义而且绑定好了的DataGrid
songxiaozhao 2006-02-10
  • 打赏
  • 举报
回复
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text=="")
{
Response.Write("<script language=javascript>");
Response.Write("window.alert(\"请输入文件名\")");
Response.Write("</script>");
}
else
{
Response.Clear();
Response.Buffer=true;
Response.Charset="gb2312";//设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename="+TextBox1.Text+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.LogGrid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
}
}
luoyi107 2006-02-10
  • 打赏
  • 举报
回复
用报表就好容易实现
lgxysl 2006-02-10
  • 打赏
  • 举报
回复
用lgxgrid控件吧。
在http://lgxyslldw.512j.com

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧