c# web gridview导出到excel

    技术2022-07-03  170

    1)新建web应用程序

    2)添加类StudentDemo.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace WebApplication18 { public class StudentDemo { public StudentDemo(int _studentID, string _studentName, int _classID) { this.studentID = _studentID; this.studentName = _studentName; this.classID = _classID; } private int studentID;//学生编号 public int StudentID { get { return studentID; } set { studentID = value; } } private string studentName;//学生姓名 public string StudentName { get { return studentName; } set { studentName = value; } } private int classID;//班级编号 public int ClassID { get { return classID; } set { classID = value; } } } }

    2)添加web窗体 Default.aspx

    1.前台

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication18._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="gvwStudents" runat="server"> </asp:GridView> <asp:Button ID="Button1" runat="server" Text="保存到excel" οnclick="btnExcel_Click" /> </div> </form> </body> </html>

    2.后台

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication18 { public partial class _Default : System.Web.UI.Page { private List<StudentDemo> allStudents;//所有学生 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SettingAllStudents(); BindStudents(); } } /// <summary> /// 绑定所有用户 /// </summary> private void BindStudents() { gvwStudents.DataSource = allStudents; gvwStudents.DataBind(); } /// <summary> /// gridview导出到excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnExcel_Click(object sender, EventArgs e) { //Excel Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.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); gvwStudents.RenderControl(oHtmlTextWriter);//gvwUsers是Gridview的ID名称 Response.Write(oStringWriter.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } /// <summary> /// 设置所有学生 /// </summary> private void SettingAllStudents() { allStudents = new List<StudentDemo>(); for (int i = 1; i <= 9; i++)//一共有9个学生,i表示学生的编号 { int j = 0;//学生所在编辑的编号 int k = 3;//每个班有3个人 if (i % k == 0) { j = i / k; } else { j = i / k + 1; } StudentDemo item = new StudentDemo(i, "张三" + i, j); allStudents.Add(item); } } } }

     

    说明:如果下载下来的是乱码,请尝试gb2312


    最新回复(0)