怎么把GridView里的数据导成Excel表?

zqg282504061 2011-06-12 11:09:08
订单编号 商品编号 商品名称 商品颜色 商品大小 购买用户 购买时间
001 0012101 asd 黑色 大 123 2011-05-05

前辈们,就像这样的一个GridView的数据,怎么导Excel表啊?
...全文
130 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-06-13
  • 打赏
  • 举报
回复
/// <summary>
/// 数据导出
/// </summary>
/// <param name="FileType"></param>
/// <param name="FileName"></param>
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gridGatewayDetails.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}

zhuwenjuan_1988 2011-06-13
  • 打赏
  • 举报
回复
protected void Button1_Click(object sender, EventArgs e)
{
ExcelOut(this.GridView1);
}
public void ExcelOut(GridView gv)
{
if (gv.Rows.Count > 0)
{
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
}
else
{
Response.Write("没有数据");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
insus 2011-06-13
  • 打赏
  • 举报
回复
myhope88 2011-06-13
  • 打赏
  • 举报
回复
楼上两种方法都可行
lbq0801 2011-06-13
  • 打赏
  • 举报
回复
代码很多
lovehong123 2011-06-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hj_daxian 的回复:]
可以直接讲绑定到Gridview的数据集导出去吧?


C# code

public static void CreateExcel(DataSet ds, string FileName) //ds数据集,FileName要导出的文件名称
{
//resp = Page.Response;
HttpContext.Current.……
[/Quote]

这个方法是对的
moduofanchen 2011-06-13
  • 打赏
  • 举报
回复

这个网上源码应该很多吧
jeje 2011-06-13
  • 打赏
  • 举报
回复

#region 將GridView中的數導入到Excel
//<summary>
//定义导出Excel的函数
//</summary>
//<param name="FileType"></param>
//<param name="FileName"></param>
private void Export(string FileType, string FileName)
{
//string style = @"<style> .text { mso-number-format:\@; } </script> ";
//Response.ClearContent();
//Response.Charset = "GB2312";
//Response.ContentEncoding = System.Text.Encoding.UTF8;
//Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
//Response.ContentType = FileType;
//this.EnableViewState = false;
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
//StringWriter tw = new StringWriter(myCItrad);
//HtmlTextWriter hw = new HtmlTextWriter(tw);
//ASPxPivotGrid1.RenderControl(hw);
////Response.Write(style);
//Response.Write(tw.ToString());
//Response.End();



}
public override void VerifyRenderingInServerForm(Control control)
{

}
#endregion
zhaopu02 2011-06-13
  • 打赏
  • 举报
回复
先记下来,等待学习,谢谢
继续泛泛 2011-06-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wxr0323 的回复:]
C# code
/// <summary>
/// 数据导出
/// </summary>
/// <param name="FileType"></param>
/// <param name="FileName"></param>
private void Export(string FileType, string FileName)
……
[/Quote]
你这个第一次能导出,但是在点击导出就不能用了,怎么解决
继续泛泛 2011-06-13
  • 打赏
  • 举报
回复
5楼老师,没有源码吗,只有DLL
  • 打赏
  • 举报
回复
可以直接讲绑定到Gridview的数据集导出去吧?

 
public static void CreateExcel(DataSet ds, string FileName) //ds数据集,FileName要导出的文件名称
{
//resp = Page.Response;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";
int i = 0;

//定义表对象与行对像,同时用DataSet对其值进行初始化
System.Data.DataTable dt = ds.Tables[0];
DataRow[] myRow = dt.Select("");

//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
for (i = 0; i < dt.Columns.Count - 1; i++)
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
//向HTTP输出流中写入取得的数据信息
HttpContext.Current.Response.Write(colHeaders);
//逐行处理数据
foreach (DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
for (i = 0; i < dt.Columns.Count - 1; i++)
ls_item += row[i].ToString() + "\t";
ls_item += row[i].ToString() + "\n";
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
HttpContext.Current.Response.Write(ls_item);
ls_item = "";
}

//写缓冲区中的数据到HTTP头文件中
HttpContext.Current.Response.End();
}
BlackPointofSun 2011-06-12
  • 打赏
  • 举报
回复
百度 gridview 导出 excel

62,074

社区成员

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

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

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

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