1. 程式人生 > >asp.net匯出Excel生成多個Sheet

asp.net匯出Excel生成多個Sheet

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace PCExpExcel
{
    public partial class ExpExcel : System.Web.UI.Page

    {

        //定義一個使用者實體類

        public class User
        {
            public string UserName { get; set; }
            public int TypeID { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gv_list.DataSource = GetUserList();
                gv_list.DataBind();
            }
        }

        //定義使用者列表資料集,type=1為未打卡,type=2為遲到,type=3為正常
        private static List<User> GetUserList()
        {
            List<User> uList = new List<User>();
            uList.Add(new User() { TypeID = 2, UserName = "董明珠" });
            uList.Add(new User() { TypeID = 2, UserName = "雷軍" });
            uList.Add(new User() { TypeID = 3, UserName = "馬雲" });
            uList.Add(new User() { TypeID = 3, UserName = "馬化騰" });
            uList.Add(new User() { TypeID = 2, UserName = "喬布斯" });
            uList.Add(new User() { TypeID = 1, UserName = "習近平" });
            uList.Add(new User() { TypeID = 1, UserName = "李克強" });
            return uList;
        }

        //點選匯出按鈕
        protected void btnExp_Click(object sender, EventArgs e)

        {

            //獲取使用者資料集

            List<User> uList = GetUserList();

            //定義匯出檔名,格式為:Exp_+年月日時分秒

            string filename = "Exp_" + DateTime.Now.ToString("yyyyMMddhhmmss");


            //臨時存放路徑
            string filePath = Server.MapPath("~/upload/" + filename);

            //建立WorkBook物件

            Workbook hssfworkbook = new HSSFWorkbook();

            //建立第一個Sheet,命名為“未打卡”

            Sheet sheetNO = hssfworkbook.CreateSheet("未打卡");

            //建立第二個Sheet,命名為“遲到”

            Sheet sheetOLD = hssfworkbook.CreateSheet("遲到");

            //建立第三個Sheet,命名為“正常”

            Sheet sheetOK = hssfworkbook.CreateSheet("正常");

            //定義迴圈索引
            int RowNO = 0;//未打卡
            int RowOLD = 0;//遲到
            int RowOK = 0;//正常

            //開始迴圈,往Sheet中新增資料
            for (int i = 0; i < uList.Count; i++)

            {

                //用switch篩選資料的型別

                switch (uList[i].TypeID)
                {
                    case 1://未打卡,typeid=1,新增到第一張Sheet表
                        RowNO++;
                        Row rowNo = sheetNO.CreateRow(RowNO);//建立Sheet中的行
                        rowNo.CreateCell(0).SetCellValue(uList[i].TypeID);//第一列=TypeID
                        rowNo.CreateCell(1).SetCellValue(uList[i].UserName);//第二列=UserName
                        break;
                    case 2://遲到,typeid=2,新增到第二張Sheet表
                        RowOLD++;
                        Row rowOLD = sheetOLD.CreateRow(RowOLD);//建立Sheet中的行
                        rowOLD.CreateCell(0).SetCellValue(uList[i].TypeID);//建立Sheet中的行
                        rowOLD.CreateCell(1).SetCellValue(uList[i].UserName);//第二列=UserName
                        break;
                    default://正常,typeid=3,新增到第三張Sheet表
                        RowOK++;
                        Row rowOK = sheetOK.CreateRow(RowOK);//建立Sheet中的行
                        rowOK.CreateCell(0).SetCellValue(uList[i].TypeID);//建立Sheet中的行
                        rowOK.CreateCell(1).SetCellValue(uList[i].UserName);//第二列=UserName
                        break;
                }


            }


            // 寫入到客戶端 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();//建立Stream物件
            hssfworkbook.Write(ms);//將建立的WorkBook物件加入Stream
            Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", filename));//設定檔案屬性,檔名
            Response.BinaryWrite(ms.ToArray());//寫入檔案流
            hssfworkbook = null;//清空WorkBook
            ms.Close();//關閉Stream物件


        }
    }
}