共享一个MVC通过NPOI导出excel的通用方法

时间:2023-03-09 23:59:25
共享一个MVC通过NPOI导出excel的通用方法
 public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, params string[] excelPropertyNames)
{
NPOI.SS.UserModel.IWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("Sheet1");
NPOI.SS.UserModel.IRow row;
NPOI.SS.UserModel.ICell cell;
NPOI.SS.UserModel.ICellStyle cellStyle; int rowNum = ;
if (!string.IsNullOrEmpty(title))
{
#region 标题
#region 标题样式
cellStyle = workbook.CreateCellStyle();
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直居中有问题
NPOI.SS.UserModel.IFont font = workbook.CreateFont();
font.FontHeightInPoints = ;
cellStyle.SetFont(font);
#endregion
row = sheet.CreateRow(rowNum);
cell = row.CreateCell(, NPOI.SS.UserModel.CellType.String);
cell.SetCellValue(title);
cell.CellStyle = cellStyle;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , excelPropertyNames.Length > ? excelPropertyNames.Length - : ));
rowNum++;
#endregion
} if (objList.Count > )
{
Type type = objList[].GetType();
if (type != null)
{
System.Reflection.PropertyInfo[] properties = type.GetProperties();
if (properties.Length > )
{
#region 表头
#region 表头样式
cellStyle = workbook.CreateCellStyle();
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
#endregion
if (excelPropertyNames.Length > )
{
row = sheet.CreateRow(rowNum);
int count = ;
for (int m = ; m < properties.Length; m++)
{
if (excelPropertyNames.Contains(properties[m].Name))
{
cell = row.CreateCell(count, NPOI.SS.UserModel.CellType.String);
string displayName = GetDisplayNameByPropertyName(properties[m].Name);
cell.SetCellValue(displayName == null ? "" : displayName);
cell.CellStyle = cellStyle;
count++;
}
}
rowNum++;
}
#endregion #region 表体
if (excelPropertyNames.Length > )
{
for (int i = ; i < objList.Count; i++)
{
row = sheet.CreateRow(i + rowNum);
int count = ;
for (int j = ; j < properties.Length; j++)
{
if (excelPropertyNames.Contains(properties[j].Name))
{
cell = row.CreateCell(count);
object obj = properties[j].GetValue(objList[i]);
cell.SetCellValue(obj == null ? "" : obj.ToString());
cell.CellStyle = cellStyle;
count++;
}
}
}
}
#endregion
}
}
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
workbook.Write(ms);
return ms;
} public static string GetDisplayNameByPropertyName(string propertyName)
{
string result = null;
foreach (KeyValuePair<string,string> dic in NameDictionary())
{
if (dic.Key == propertyName)
{
result = dic.Value;
}
continue;
}
return result;
} public static Dictionary<string, string> NameDictionary()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("AdminID", "编号"); dic.Add("AdminName", "用户名"); dic.Add("AdminMobile", "手机号"); dic.Add("RealName", "真实姓名"); return dic;
}

调用很简单

 public ActionResult Test()
{
int totalCount;
List<AdminModel> adminModelList = adminBLL.GetPageList(, , out totalCount);
if (adminModelList == null)
{
adminModelList = new List<AdminModel>();
}
return File(ExcelHelper.ExportExcel<AdminModel>("表头", adminModelList, "AdminID", "AdminName", "AdminMobile", "RealName").ToArray(), "application/vnd.ms-excel", "工作簿.xls");
}