天天看點

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