c# web gridview导出到excel

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值