Excel-数据分类导出至多个Sheet NPOI.dll
2015-04-21 14:51
375 查看
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();
}
}
}
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(); } }
}
相关文章推荐
- 使用npoi.dll导出数据到excel
- NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 246人阅读 评论(0) 收藏
- 使用npoi.dll导出数据到excel
- NPOI 通用导出数据到Excel 分类: C# Helper 2014-11-04 16:06 247人阅读 评论(0) 收藏
- NPOI导出EXCEL数据量大,分多个sheet显示数据
- C# NPOI2.1.3 版本数据导入导出到excel
- 【推荐】.NET使用NPOI组件将数据导出Excel
- 【推荐】.NET(webform,MVC)使用NPOI组件将数据导出、导入Excel。 Aspose.Cell 控件报表
- [源码分享]NPOI导出数据到EXCEl(输出文件,web输出下载)
- NET使用NPOI组件将数据导出Excel
- NPOI将数据导出到Excel中
- NPOI导出数据到Excel
- 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续3篇-导出时动态生成多Sheet EXCEL)
- C#添加NPOI.dll导出excel
- NPOI从数据库中调取数据直接导出到EXCEL中
- 使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中
- webapi下使用npoi导出数据到excel,包含自动列宽和自动合并某一列单元格
- 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限
- DataSet数据导出为Excel文档(每个DataTable为一个Sheet)
- npoi实现数据导出Excel