NPOI导出Excel生成多个sheet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
using System.Reflection; namespace WebApplication1.Controllers
{
public class Info
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
List<Info> list = new List<Info>(); for (int i = ; i < ; i++)
{
Info info = new Info();
info.Id = i + ;
info.Name = "张三" + (i + );
list.Add(info);
}
Dictionary<string, string> columnInfo = new Dictionary<string, string>();
columnInfo.Add("Id","序号");
columnInfo.Add("Name", "姓名");
byte[] btyBytes=null;
var a = ExportExcelTest<Info>(list, @"F://111.xlsx", ref btyBytes, columnInfo, ); return View();
} #region NPOI大数据量多个sheet导出 /// <summary>
/// 大数据量多个sheet导出
/// </summary>
/// <typeparam name="T">数据源实体类</typeparam>
/// <param name="objList">数据源</param>
/// <param name="fileName">文件名称</param>
/// <param name="btyBytes">导出数据流</param>
/// <param name="columnInfo">显示列对应数据字典</param>
/// <param name="listCount">每个sheet包含数据条数</param>
/// <returns></returns>
public static bool ExportExcelTest<T>(List<T> objList, string fileName, ref byte[] btyBytes,
Dictionary<string, string> columnInfo = null, int listCount = )
{
bool bResult = false;
//在内存中生成一个Excel文件:
XSSFWorkbook book = new XSSFWorkbook();
if (objList != null && objList.Count > )
{
double sheetCount = Math.Ceiling((double)objList.Count / listCount);
for (int i = ; i < sheetCount; i++)
{
ISheet sheet = null;
sheet = book.CreateSheet("sheet" + i);
sheet.DefaultRowHeight = * ;
List<T> list = new List<T>();
list = objList.Skip<T>(listCount * i).Take<T>(listCount).ToList(); int rowIndex = ;
int StartColIndex = ;
int colIndex = StartColIndex; //创建表头样式
ICellStyle style = book.CreateCellStyle();
style.Alignment = HorizontalAlignment.CENTER;
style.WrapText = true;
IFont font = book.CreateFont();
font.FontHeightInPoints = ;
font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.BOLD;
font.FontName = "简体中文";
style.SetFont(font);//HEAD 样式 Type myType = null;
myType = objList[].GetType();
//根据反射从传递进来的属性名信息得到要显示的属性
List<PropertyInfo> myPro = new List<PropertyInfo>();
PropertyInfo[] properties = myType.GetProperties(); #region 定义表头
int m = ;
if (columnInfo != null)
{
var rowheader = sheet.CreateRow();
rowheader.Height = rowheader.Height = * ;
foreach (string cName in columnInfo.Keys)
{
PropertyInfo p = myType.GetProperty(cName);
if (p != null)
{
myPro.Add(p);
rowheader.CreateCell(m).SetCellValue(columnInfo[cName]);
m++;
}
}
}
#endregion
#region 定义表体并赋值
//如果没有找到可用的属性则结束
if (myPro.Count == ) { return bResult; }
foreach (T obj in list)
{
int n = ;
if (sheet != null)
{
rowIndex++;
var sheetrow = sheet.CreateRow(rowIndex);
sheetrow.Height = sheetrow.Height = * ;
foreach (PropertyInfo p in myPro)
{
dynamic val = p.GetValue(obj, null) ?? "";
string valtype = val.GetType().ToString();
if (valtype.ToLower().IndexOf("decimal", StringComparison.Ordinal) > -)
{
val = Convert.ToDouble(val);
}
else if (valtype.ToLower().IndexOf("datetime", StringComparison.Ordinal) > -)
{
val = val.ToString("yyyy-MM-dd HH:mm:ss");
if (val.Equals("0001-01-01 00:00:00"))
{
val = "";
}
}
sheetrow.CreateCell(n).SetCellValue(val);
n++;
}
} }
#endregion
}
}
else
{
//在工作薄中建立工作表
XSSFSheet sheet = book.CreateSheet() as XSSFSheet;
sheet.SetColumnWidth(, * );
if (sheet != null) sheet.CreateRow().CreateCell().SetCellValue("暂无数据!");
} var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
book.Write(fs);
//try
//{
// HttpResponse rs = System.Web.HttpContext.Current.Response;
// rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// rs.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
// rs.ContentType = "application/vnd.ms-excel";
// //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet // using (MemoryStream ms = new MemoryStream())
// {
// book.Write(ms);
// btyBytes = ms.GetBuffer();
// rs.AddHeader("Content-Length", btyBytes.Length.ToString());
// rs.BinaryWrite(ms.GetBuffer());
// ms.Flush();
// }
//}
//catch (SystemException ex)
//{
// throw ex;
//}
//catch (ApplicationException ex)
//{
// throw ex;
//}
return bResult;
} #endregion
}
}

NPOI导出Excel生成多个sheet的更多相关文章
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
- NPOI导出Excel(含有超过65335的处理情况)
NPOI导出Excel的网上有很多,正好自己遇到就学习并总结了一下: 首先说明几点: 1.Excel2003及一下:后缀xls,单个sheet最大行数为65335 Excel2007 单个sheet ...
- 分享使用NPOI导出Excel树状结构的数据,如部门用户菜单权限
大家都知道使用NPOI导出Excel格式数据 很简单,网上一搜,到处都有示例代码. 因为工作的关系,经常会有处理各种数据库数据的场景,其中处理Excel 数据导出,以备客户人员确认数据,场景很常见. ...
- NPOI导出Excel表功能实现(多个工作簿)(备用)
Excel生成操作类: 代码 using System; using System.Collections.Generic; using System.Text; using System.IO; u ...
- NPOI导出Excel示例
摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...
- 使用NPOI导出Excel文件
使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- NPOI导出EXCEL 打印设置分页及打印标题
在用NPOI导出EXCEL的时候设置分页,在网上有查到用sheet1.SetRowBreak(i)方法,但一直都没有起到作用.经过研究是要设置 sheet1.FitToPage = false; 而 ...
随机推荐
- 2019-9-2-Visual-Studio-自定义项目模板
title author date CreateTime categories Visual Studio 自定义项目模板 lindexi 2019-09-02 12:57:38 +0800 2018 ...
- 2018-11-2-win10-uwp-通过-win2d-画出笔迹
title author date CreateTime categories win10 uwp 通过 win2d 画出笔迹 lindexi 2018-11-2 20:11:0 +0800 2018 ...
- 28款GitHub最流行的开源机器学习项目,推荐GitHub上10 个开源深度学习框架
20 个顶尖的 Python 机器学习开源项目 机器学习 2015-06-08 22:44:30 发布 您的评价: 0.0 收藏 1收藏 我们在Github上的贡献者和提交者之中检查了用Python语 ...
- javascript异步编程 Async/await
Async/await Async/await 在学习他之前应当补充一定的 promise 知识 它是一种与 promise 相配合的特殊语法,目前被认为是异步编程的终级解决方案 值得我们每一个人学习 ...
- javax.el.PropertyNotFoundException: Property 'XXX' not found on type java.lang.String
遇到的问题: 在使用idea开发Java Web时,调用SSM框架出现了如下错误: 但是我的类中已经定义了geter和seter方法,如下: 而Jsp中的调用代码是通过EL实现,也导入了相应的包.如下 ...
- ABP运行Login failed for user 'IIS APPPOOL XXXXX Reason: Could not find a login matching the name provided问题解决
我们在ABP官网上面生成解决方案后,编译完成,将数据库连接字符串中的Ip改成自己的测试数据库Ip直接在Vs里面调试运行没有任何问题. 发布之后到文件夹后运行,就报如下图异常. VS里面可以跑起来,单独 ...
- AbstactFactory模式
AbstractFactory模式就是用来解决这类问题的:要创建一组相关或者相互依赖的对象. AbstractFactory模式关键就是将这一组对象的创建封装到一个用于创建对象的类(ConcreteF ...
- 重置Rhapsody超级管理员administrator的密码
Rhapsody的安装信息说明 rhapsody 默认初始安装的用户名为 Administrator 密码为 rhapsody 配置文件rhapsody.properties位于位于{安装目录}\Rh ...
- XAMPP下MYSQL中文乱码问题的解决
XAMPP下MYSQL中文乱码问题的解决 现象描述: 安装完成XAMMP后,内置有MySQL数据库. 新建好自己的数据库后通过hibernate往表里面添加一些中文信息时全部乱码变成“??”. 问题解 ...
- SpringBoot入门简易教程
使用SpringBoot来开发一个简单的restful api网关功能,目标:实现对SpringBoot的简单入门. 1. 创建SpringBoot项目 可以通过spring官网(https://st ...