您的位置:首页 > 其它

Excel-数据分类导出至多个Sheet NPOI.dll

2015-04-21 14:51 344 查看
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();

}

}

[code]    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();
        string filename = "Exp_" + DateTime.Now.ToString("yyyyMMddhhmmss");

        //临时存放路径
        string filePath = Server.MapPath("~/upload/" + filename);

        Workbook hssfworkbook = new HSSFWorkbook();
        Sheet sheetNO = hssfworkbook.CreateSheet("未打卡");
        Sheet sheetOLD = hssfworkbook.CreateSheet("迟到");
        Sheet sheetOK = hssfworkbook.CreateSheet("正常");

        int RowNO = 0;
        int RowOLD = 0;
        int RowOK = 0;

        for (int i = 0; i < uList.Count; i++)
        {
            switch (uList[i].TypeID)
            {
                case 1:
                    RowNO++;
                    Row rowNo = sheetNO.CreateRow(RowNO);
                    rowNo.CreateCell(0).SetCellValue(uList[i].TypeID);
                    rowNo.CreateCell(1).SetCellValue(uList[i].UserName);
                    break;
                case 2:
                    RowOLD++;
                    Row rowOLD = sheetOLD.CreateRow(RowOLD);
                    rowOLD.CreateCell(0).SetCellValue(uList[i].TypeID);
                    rowOLD.CreateCell(1).SetCellValue(uList[i].UserName);
                    break;
                default:
                    RowOK++;
                    Row rowOK = sheetOK.CreateRow(RowOK);
                    rowOK.CreateCell(0).SetCellValue(uList[i].TypeID);
                    rowOK.CreateCell(1).SetCellValue(uList[i].UserName);
                    break;
            }

        }

        // 写入到客户端 
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        hssfworkbook.Write(ms);
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", filename));
        Response.BinaryWrite(ms.ToArray());
        hssfworkbook = null;
        ms.Close();

    }
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: