asp.net将gridview导出到excel和word
发布网友
发布时间:2022-04-27 09:08
我来回答
共2个回答
热心网友
时间:2022-04-27 10:37
用数据流导出,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教你。你的串号我已经记下,采纳后我会帮你制作
热心网友
时间:2022-04-27 11:55
更换输出类型 分别导出到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();
我去掉了部分双引号 不知道为什么加上了就提交不全