c# web gridview excel로 내보내기

4517 단어
1) 새 웹 응용 프로그램
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) 웹 창 추가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" onclick="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를 시도하십시오

좋은 웹페이지 즐겨찾기