Asp.Net导出数据到Excel
对应的详细信息:如下图
如图,根据当前的gv标题对应的id,然后生成excel表格
代码如下:

protected void btnToExcel_Click(object sender, EventArgs e)
{
DGToExcel();
}
public void DGToExcel()
{
GridView gvNew = new GridView();
gvNew.DataSource = GetData();
gvNew.DataBind();
System.Web.UI.Control ctl = gvNew;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
//ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private DataTable GetData()
{
DataTable dt = CreateStructure();
for (int i = 0; i < gvMsg.Rows.Count; i++)
{
CheckBox cb = (gvMsg.Rows[i].FindControl("chkSelect")) as CheckBox;
if (cb.Checked)
{
int MsgId = 0;
try
{
MsgId = int.Parse((gvMsg.Rows[i].FindControl("lblId") as Label).Text);
}
catch (Exception)
{
continue;
}
Message msg = MessageManage.GetMessageById(MsgId);
DataRow dr = dt.NewRow();
dr["编号"] = msg.Id;
dr["标题"] = msg.Title;
dr["内容"] = msg.Content;
dr["消息从何处发送"] = this.GetMsgAddress(msg.MsgType).ToString();
dr["意向产品"] = GetProduct(MsgId);
dr["代理人/发信人"] = msg.AgentManName;
dr["代理区域"] = msg.AgentArea;
dr["代理产品类型"] = GetType(msg.AgentType);
dr["电话"] = msg.Phone;
dr["地址"] = msg.Address;
dr["发送时间"] = msg.SendTime;
dt.Rows.Add(dr);
}
}
return dt;
}
private DataTable CreateStructure()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("编号", typeof(int)));
dt.Columns.Add(new DataColumn("标题", typeof(string)));
dt.Columns.Add(new DataColumn("内容", typeof(string)));
dt.Columns.Add(new DataColumn("消息从何处发送", typeof(string)));
dt.Columns.Add(new DataColumn("意向产品", typeof(string)));
dt.Columns.Add(new DataColumn("代理人/发信人", typeof(string)));
dt.Columns.Add(new DataColumn("代理区域", typeof(string)));
dt.Columns.Add(new DataColumn("代理产品类型", typeof(string)));
dt.Columns.Add(new DataColumn("电话", typeof(string)));
dt.Columns.Add(new DataColumn("地址", typeof(string)));
dt.Columns.Add(new DataColumn("发送时间", typeof(DateTime)));
return dt;
}
{
DGToExcel();
}
public void DGToExcel()
{
GridView gvNew = new GridView();
gvNew.DataSource = GetData();
gvNew.DataBind();
System.Web.UI.Control ctl = gvNew;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
//ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private DataTable GetData()
{
DataTable dt = CreateStructure();
for (int i = 0; i < gvMsg.Rows.Count; i++)
{
CheckBox cb = (gvMsg.Rows[i].FindControl("chkSelect")) as CheckBox;
if (cb.Checked)
{
int MsgId = 0;
try
{
MsgId = int.Parse((gvMsg.Rows[i].FindControl("lblId") as Label).Text);
}
catch (Exception)
{
continue;
}
Message msg = MessageManage.GetMessageById(MsgId);
DataRow dr = dt.NewRow();
dr["编号"] = msg.Id;
dr["标题"] = msg.Title;
dr["内容"] = msg.Content;
dr["消息从何处发送"] = this.GetMsgAddress(msg.MsgType).ToString();
dr["意向产品"] = GetProduct(MsgId);
dr["代理人/发信人"] = msg.AgentManName;
dr["代理区域"] = msg.AgentArea;
dr["代理产品类型"] = GetType(msg.AgentType);
dr["电话"] = msg.Phone;
dr["地址"] = msg.Address;
dr["发送时间"] = msg.SendTime;
dt.Rows.Add(dr);
}
}
return dt;
}
private DataTable CreateStructure()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("编号", typeof(int)));
dt.Columns.Add(new DataColumn("标题", typeof(string)));
dt.Columns.Add(new DataColumn("内容", typeof(string)));
dt.Columns.Add(new DataColumn("消息从何处发送", typeof(string)));
dt.Columns.Add(new DataColumn("意向产品", typeof(string)));
dt.Columns.Add(new DataColumn("代理人/发信人", typeof(string)));
dt.Columns.Add(new DataColumn("代理区域", typeof(string)));
dt.Columns.Add(new DataColumn("代理产品类型", typeof(string)));
dt.Columns.Add(new DataColumn("电话", typeof(string)));
dt.Columns.Add(new DataColumn("地址", typeof(string)));
dt.Columns.Add(new DataColumn("发送时间", typeof(DateTime)));
return dt;
}
但是我也遇到了一个问题,就是excel生成的超级链接,是以文本的形式显示出来,哪位大侠遇到这样的问题,请指点迷津.
作者:Cat Qi
出处:http://qixuejia.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://qixuejia.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架