NPOI _导出exl(简单应用)
Posted leolzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NPOI _导出exl(简单应用)相关的知识,希望对你有一定的参考价值。
1. 导出exl表格,创建表格导出到客户端
public static MemoryStream Export_Table<T>(List<T> datalist) { MemoryStream ms = new MemoryStream(); var members = typeof(T).GetProperties(); var workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet(); NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0); int order = 1; foreach (var meber in members)//初始化标题 { string titlevalue = ""; var name = meber.GetCustomAttributes(typeof(TableAttribulate), false); if (name.Length == 0) continue; var pro = name[0] as TableAttribulate; if (pro == null) continue; titlevalue = pro.CName; var cell = headerRow.CreateCell(order); cell.SetCellValue(titlevalue); if(pro.Weight==0) sheet.AutoSizeColumn(order,true); else { sheet.SetColumnWidth(order,pro.Weight); } cell.CellStyle = GetStyle(workbook); cell.CellStyle.Alignment = pro.HorizontalAlignment; var cellfont = workbook.CreateFont(); cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold; cell.CellStyle.SetFont(cellfont); order++; } int rowIndex = 1; foreach (var row in datalist) { NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex); int colIndex = 0; var cellindex = dataRow.CreateCell(colIndex); cellindex.SetCellValue(rowIndex); cellindex.CellStyle = GetStyle(workbook); var cellfont = workbook.CreateFont(); cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal; cellfont.Color = NPOI.HSSF.Util.HSSFColor.LightBlue.Index; cellindex.CellStyle.SetFont(cellfont); colIndex++; foreach (var meber in members) { var name = meber.GetCustomAttributes(typeof(TableAttribulate), false); if (name.Length == 0) continue; var pro = name[0] as TableAttribulate; if (pro == null) continue; if (pro.Weight == 0) sheet.AutoSizeColumn(colIndex, true); var cell = dataRow.CreateCell(colIndex); var mebervalue = meber.GetValue(row); cell.SetCellValue(mebervalue == null ? "" : mebervalue.ToString()); cell.CellStyle = GetStyle(workbook); cell.CellStyle.Alignment = pro.HorizontalAlignment; colIndex++; } rowIndex++; } workbook.Write(ms); ms.Seek(0, SeekOrigin.Begin); return ms;// }
2.代码总的特性
public class TableAttribulate:Attribute { public TableAttribulate(string name, int weight = 0, NPOI.SS.UserModel.HorizontalAlignment hoalign = NPOI.SS.UserModel.HorizontalAlignment.Center) { CName = name; Weight = weight; HorizontalAlignment = hoalign; } public string CName { get; set; } public int Weight { get; set; } public NPOI.SS.UserModel.HorizontalAlignment HorizontalAlignment { get; set; }//对齐方式 }
3.函数
public static NPOI.SS.UserModel.ICellStyle GetStyle(NPOI.HSSF.UserModel.HSSFWorkbook workbook) { var cs = workbook.CreateCellStyle(); cs.DataFormat = NPOI.HSSF.UserModel.HSSFDataFormat.GetBuiltinFormat("@"); cs.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cs.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cs.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cs.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cs.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cs.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; var cellfont = workbook.CreateFont(); cellfont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Normal; cs.SetFont(cellfont); return cs; }
以上是关于NPOI _导出exl(简单应用)的主要内容,如果未能解决你的问题,请参考以下文章