NPOI大数据量多个sheet导出源码(原)
2015-08-31 18:07
441 查看
代码如下:
#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=10000) { bool bResult = false; //在内存中生成一个Excel文件: HSSFWorkbook book = new HSSFWorkbook(); if (objList != null && objList.Count > 0) { double sheetCount = Math.Ceiling((double)objList.Count / listCount); for (int i = 0; i < sheetCount; i++) { ISheet sheet = null; sheet = book.CreateSheet("sheet" + i); sheet.DefaultRowHeight = 20 * 10; List<T> list = new List<T>(); list = objList.Skip<T>(listCount * i).Take<T>(listCount).ToList(); int rowIndex = 0; int StartColIndex = 0; int colIndex = StartColIndex; //创建表头样式 ICellStyle style = book.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center; style.WrapText = true; IFont font = book.CreateFont(); font.FontHeightInPoints = 16; font.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; font.FontName = "简体中文"; style.SetFont(font);//HEAD 样式 Type myType = null; myType = objList[0].GetType(); //根据反射从传递进来的属性名信息得到要显示的属性 List<PropertyInfo> myPro = new List<PropertyInfo>(); PropertyInfo[] properties = myType.GetProperties(); #region 定义表头 int m = 0; if (columnInfo != null) { var rowheader = sheet.CreateRow(0); rowheader.Height = rowheader.Height = 20 * 20; 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 == 0) { return bResult; } foreach (T obj in list) { int n = 0; if (sheet != null) { rowIndex++; var sheetrow = sheet.CreateRow(rowIndex); sheetrow.Height = sheetrow.Height = 20 * 20; foreach (PropertyInfo p in myPro) { dynamic val = p.GetValue(obj, null) ?? ""; string valtype = val.GetType().ToString(); if (valtype.ToLower().IndexOf("decimal", StringComparison.Ordinal) > -1) { val = Convert.ToDouble(val); } else if (valtype.ToLower().IndexOf("datetime", StringComparison.Ordinal) > -1) { 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 { //在工作薄中建立工作表 HSSFSheet sheet = book.CreateSheet() as HSSFSheet; sheet.SetColumnWidth(0, 30 * 256); if (sheet != null) sheet.CreateRow(0).CreateCell(0).SetCellValue("暂无数据!"); } 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/ms-excel"; using (MemoryStream ms = new MemoryStream()) { book.Write(ms); rs.BinaryWrite(ms.ToArray()); ms.Flush(); } } catch (SystemException ex) { LogHelper.Write(ex); } catch (ApplicationException ex) { LogHelper.Write(ex); } return bResult; } #endregion
相关文章推荐
- 云计算关键技术
- LeetCode之Contains Duplicate II
- 什么情况下用copy、assign、retain、strong...
- 车型识别“A Large-Scale Car Dataset for Fine-Grained Categorization and Verification”
- 刷一题Leetcode:Factorial Trailing Zeroes
- LeetCode之Contains Duplicate
- hdfs源码分析第一弹
- 物联网方案分析的几个哲学命题
- 物联网支撑平台
- OC之非ARC环境下循环retain问题
- HDU 2476 String painter(区间dp)
- c的Main函数参数解析
- poj 1273 Drainage Ditches
- ESP8266学习笔记8:ESP8266接入机智云
- leecode 每日解题思路 127-Factorial Trailing Zeroes
- (null): Linker command failed with exit code 1 (use -v to see invocation)
- 为什么要内存对齐 Data alignment: Straighten up and fly right
- INSTALL_FAILED_CONFLICTING_PROVIDER错误解决方法
- 将 paired count 和unpaired count 相加
- 物联网发展