asp.net将gridview导出到excel和word
如何将gridview导出到excel和word,gridview控件和导出excel按钮不在同一个页面,并且数据集是多表查询,包含了gridview中不显示的字段,导出...
如何将gridview导出到excel和word,gridview控件和导出excel按钮不在同一个页面,并且数据集是多表查询,包含了gridview中不显示的字段,导出excel只要求导出gridview显示的字段,而且导出的数据在excel表头中字段为数据库中的英文,如何把excel的表头设置为gridview中的中文显示,望高手指点,多谢!
展开
2个回答
展开全部
用数据流导出,Gridview什么样子,导出来就是什么样子。
ex:
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("ZH-CN", 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);
Response.Write(oStringWriter.ToString());
Response.End();
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
ex:
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("ZH-CN", 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);
Response.Write(oStringWriter.ToString());
Response.End();
我可以帮助你,你先设置我最佳答案后,我百度Hii教你。你的串号我已经记下,采纳后我会帮你制作
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
更换输出类型 分别导出到word 和 excel中
//以下三行可选,如果没有的话导出的只是当前页数据,没有其他页数据
gvTrip.AllowPaging = false;
gvTrip.AllowSorting = false;
gvTripDataBind(); //重新绑定gridview.
//移除标识列,操作列
gvTrip.Columns.RemoveAt(0);
gvTrip.Columns.RemoveAt(0);
gvTrip.Columns.RemoveAt(5);
gvTrip.Columns.RemoveAt(7);
///为gvPerson绑定--任务描述列、说明列、建议列、和备注列,因为这些列都使用超
//长字符串截取的方法。构造出差原因列
BoundField columnTripReason = new BoundField();
columnTripReason.HeaderText =出差原因;
columnTripReason.DataField =TripReason;
////加入出差原因列
gvTrip.Columns.Insert(5, columnTripReason);
//构造备注列
BoundField columnRemark = new BoundField();
columnRemark.HeaderText = 备注;
columnRemark.DataField = Remark;
//加入备注列
gvTrip.Columns.Insert(8, columnRemark);
//重新绑定gvPerson
gvTripDataBind();
Response.Charset = GB2312;
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader(Content-Disposition, attachment;filename= + HttpUtility.UrlEncode(人员流动(出差)信息表.xls, System.Text.Encoding.UTF8).ToString());
Response.ContentType = application/ms-excel;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvTrip.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
我去掉了部分双引号 不知道为什么加上了就提交不全
//以下三行可选,如果没有的话导出的只是当前页数据,没有其他页数据
gvTrip.AllowPaging = false;
gvTrip.AllowSorting = false;
gvTripDataBind(); //重新绑定gridview.
//移除标识列,操作列
gvTrip.Columns.RemoveAt(0);
gvTrip.Columns.RemoveAt(0);
gvTrip.Columns.RemoveAt(5);
gvTrip.Columns.RemoveAt(7);
///为gvPerson绑定--任务描述列、说明列、建议列、和备注列,因为这些列都使用超
//长字符串截取的方法。构造出差原因列
BoundField columnTripReason = new BoundField();
columnTripReason.HeaderText =出差原因;
columnTripReason.DataField =TripReason;
////加入出差原因列
gvTrip.Columns.Insert(5, columnTripReason);
//构造备注列
BoundField columnRemark = new BoundField();
columnRemark.HeaderText = 备注;
columnRemark.DataField = Remark;
//加入备注列
gvTrip.Columns.Insert(8, columnRemark);
//重新绑定gvPerson
gvTripDataBind();
Response.Charset = GB2312;
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader(Content-Disposition, attachment;filename= + HttpUtility.UrlEncode(人员流动(出差)信息表.xls, System.Text.Encoding.UTF8).ToString());
Response.ContentType = application/ms-excel;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gvTrip.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
我去掉了部分双引号 不知道为什么加上了就提交不全
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询