在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
前台代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExportDemo.aspx.cs" Inherits="ExportDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 后台代码: using System; public partial class ExportDemo : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) private void BindGridView() Session["MyData"] = myData; gvPersonList.DataSource = myData; gvPersonList.DataBind(); } DataColumn dcld = new DataColumn("ID",typeof(Int32)); //设置ID列自增 //设置ID列初始值为1 //设置ID列递增步长值为1 //将ID列添加到Table中 data.Columns.Add(new DataColumn("Name",typeof(string))); data.Columns.Add(new DataColumn("Age",typeof(Int32))); data.Columns.Add(new DataColumn("Sex",typeof(bool))); data.Columns.Add(new DataColumn("Married",typeof(bool))); DataRow dataRow = null; Random random = new Random(); //随机生成20条记录 //随机生成姓名 //随机产生介于20至100之间的年龄 dataRow["Age"] = age; //随机设置性别 dataRow["Sex"] = sex; //男性结婚年龄大于22周岁,女性结婚年龄大于20周岁 data.Rows.Add(dataRow); return data; /// <summary> Response.BufferOutput=true; //设定输出的字符集 //假定导出的文件名为FileName.xls Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; //关闭ViewState System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN",true); System.IO.StringWriter stringwriter = new System.IO.StringWriter(cultureInfo); System.Web.UI.HtmlTextWriter textwriter = new System.Web.UI.HtmlTextWriter(stringwriter); gvPersonList.RenderControl(textwriter); //把Html写回浏览器 Response.End(); Response.BufferOutput = true; //设定输出的字符集 //假定输出的文件名为FileName.Doc Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //设置导出文件的格式 //关闭ViewState System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true); System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo); System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter); gvPersonList.RenderControl(textWriter); // //把HTML写回浏览器 Response.End(); //必须重写这个方法,方法体内不需任何操作,不然就会报错! public override void VerifyRenderingInServerForm(Control contorl) |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论