/// <summary> /// 设置Excel内容 /// </summary> /// <typeparam name="T">Excel模型实体</typeparam> /// <param name="list">Excel数据集合</param> /// <param name="_excelInfos"></param> private void SetExcelContent <T>(IEnumerable <T> list, Dictionary <PropertyInfo, ExcelInfoAttribute> _excelInfos) { int _rowNum = 1; Dictionary <string, ICellStyle> cellStyleList = new Dictionary <string, ICellStyle>(); foreach (T rowItem in list) { int _rowCell = 0; IRow _rowValue = hssfSheet.CreateRow(_rowNum); foreach (var cellItem in _excelInfos) { object _cellItemValue = cellItem.Key.GetValue(rowItem); ICell _cell = _rowValue.CreateCell(_rowCell); if (!cellStyleList.ContainsKey(cellItem.Value.ExcelStyle.ToString())) { ICellStyle _cellStyle = ExcelStyleMessage.GetCellStyle(hssfWork, cellItem.Value.ExcelStyle); cellStyleList.Add(cellItem.Value.ExcelStyle.ToString(), _cellStyle); } SetCellValue(cellItem, _cellItemValue, _cell); _cell.CellStyle = cellStyleList[cellItem.Value.ExcelStyle.ToString()]; _rowCell++; } _rowNum++; } }
/// <summary> /// 设置Excel行 /// </summary> private void SetExcelTitle(Dictionary <PropertyInfo, ExcelInfoAttribute> excelInfos) { IRow rowTitle = hssfSheet.CreateRow(0); int _cellIndex = 0; foreach (var item in excelInfos) { ICell celltitle = rowTitle.CreateCell(_cellIndex); celltitle.CellStyle = ExcelStyleMessage.GetCellStyle(hssfWork, ExcelStyle.title); celltitle.SetCellValue(item.Value.Name); hssfSheet.SetColumnWidth(_cellIndex, item.Value.Width); _cellIndex++; } }