重构NPOI逻辑无关代码

Former-commit-id: 93418d59831a207851066f8a1b223bcb6d0da60d
Former-commit-id: f65fd3398aa3f564d721a1e4bf891ab460e63770
1.0
wanggang 5 years ago
parent c51806a851
commit 7a467da9af

@ -12,18 +12,11 @@ using Newtonsoft.Json.Serialization;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using TeacherExt.Entities;
using TeacherExt.Models;
using System.Reflection;
using NPOI.SS.Util;
using NPOI.HSSF.Util;
namespace TeacherExt.Controllers
{
@ -90,7 +83,7 @@ namespace TeacherExt.Controllers
[HttpPost]
[Route("/")]
public IActionResult Index(QueryTeacherModel model,bool isPost)
public IActionResult Index(QueryTeacherModel model, bool isPost)
{
return this.Index(model);
}
@ -381,9 +374,9 @@ namespace TeacherExt.Controllers
private void ExportInternal(ISheet sheet, QueryTeacherModel model)
{
var style = this.CreateStyle(sheet,fontName:"宋体",fontSize:11);
var style = NPOIHelper.CreateStyle(sheet, fontName: "宋体", fontSize: 11);
var headers = CreateHader(sheet, model);
var headers = NPOIHelper.CreateHader(sheet, typeof(EditTeacherModel), model.Headers);
var list = this.Query(model)
.ToList()
.Select(o =>
@ -409,14 +402,14 @@ namespace TeacherExt.Controllers
colIndex = -1;
var row = sheet.CreateRow(rowIndex);
teacher.DisplayOrder = rowNumber;
row.Order(00).CreateCell(++colIndex).SetStyle(style).SetType(CellType.Numeric).SetCellValue(rowNumber);
// this.IsExport(teacher);
// this.IsExport(teacher);
row.Order(01).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.Organ);
row.Order(02).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.RealName);
row.Order(03).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Sex)?"无":teacher.Sex);
row.Order(03).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Sex) ? "无" : teacher.Sex);
row.Order(04).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.Birthday.HasValue ? teacher.Birthday.Value.ToString("yyyy-MM") : "无");
if(teacher.Age.HasValue)
if (teacher.Age.HasValue)
{
row.Order(05).CreateCell(++colIndex).SetStyle(style).SetType(CellType.Numeric).SetCellValue(teacher.Age.Value);
}
@ -442,9 +435,9 @@ namespace TeacherExt.Controllers
row.Order(13).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue("无");
row.Order(14).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue("无");
}
row.Order(15).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Politics)?"无":teacher.Politics);
row.Order(15).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Politics) ? "无" : teacher.Politics);
row.Order(16).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.JoinPartyDate.HasValue ? teacher.JoinPartyDate.Value.ToString("yyyy-MM") : "无");
row.Order(17).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.MaxTitle)?"无":teacher.MaxTitle);
row.Order(17).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.MaxTitle) ? "无" : teacher.MaxTitle);
row.Order(18).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.MaxTitleStart.HasValue ? teacher.MaxTitleStart.Value.ToString("yyyy-MM") : "无");
if (teacher.IsJobAsMaxTitle.HasValue)
{
@ -459,8 +452,8 @@ namespace TeacherExt.Controllers
else
{
row.Order(19).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue("无");
row.Order(20).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.JobAsNotMaxTitleReason)?"无":teacher.JobAsNotMaxTitleReason);
row.Order(21).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.NotMaxTitle)?"无":teacher.NotMaxTitle);
row.Order(20).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.JobAsNotMaxTitleReason) ? "无" : teacher.JobAsNotMaxTitleReason);
row.Order(21).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.NotMaxTitle) ? "无" : teacher.NotMaxTitle);
row.Order(22).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.NotMaxTitleStart.HasValue ? teacher.NotMaxTitleStart.Value.ToString("yyyy-MM") : "无");
row.Order(23).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(teacher.JobAsNotMaxTitleDate.HasValue ? teacher.JobAsNotMaxTitleDate.Value.ToString("yyyy-MM") : "无");
}
@ -541,205 +534,7 @@ namespace TeacherExt.Controllers
row.Order(55).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.CurrentAddressStreat) ? "无" : teacher.CurrentAddressStreat);
row.Order(56).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Comment) ? "无" : teacher.Comment);
}
this.SetColWidth(3,sheet);
}
private List<string> CreateHader(ISheet sheet, QueryTeacherModel model)
{
var style = this.CreateStyle(sheet,backgroundColor: HSSFColor.PaleBlue.Index,fontSize: 11,fontColor: HSSFColor.White.Index,isBold: true,fontName: "黑体",textWrap:true);
var result = new List<string>();
var headers = typeof(EditTeacherModel).GetProperties().Select(o => o.GetCustomAttribute<ExcelHeaderAttribute>()).Where(o => o != null).ToList();
var colIndex = 0;
foreach (var item in headers)
{
if (!model.Headers.Any() || model.Headers.Contains(item.Header))
{
result.Add(item.Header);
int i = 0;
foreach (var head in item.Headers)
{
for (int j = i; j < i + head.Value; j++)
{
var row = (sheet.GetRow(j) ?? sheet.CreateRow(j));
var cell = (row.GetCell(colIndex) ?? row.CreateCell(colIndex));
cell.SetStyle(style);//.SetCellValue(head.Key);
if (j == i)
{
cell.SetCellValue(head.Key);
}
}
if (head.Value > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i + head.Value - 1, colIndex, colIndex));
sheet.GetRow(i).GetCell(colIndex).SetStyle(style);
}
i = i + head.Value;
}
colIndex += 1;
}
}
for (int i = 0; i < sheet.LastRowNum; i++)
{
var row = sheet.GetRow(i);
for (int j = 0; j < row.LastCellNum; j++)
{
var cell = row.GetCell(j);
var cells = new List<ICell>() { cell };
if (cell != null && cell.StringCellValue != "")
{
var col = 1;
if (j + 1 < row.LastCellNum)
{
for (int k = j + 1; k < row.LastCellNum; k++)
{
var next = row.GetCell(k);
if (next == null || next.StringCellValue != cell.StringCellValue)
{
break;
}
next.SetCellValue("");
cells.Add(next);
col++;
}
}
if (col > 1)
{
if (!cell.IsMergedCell)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i, j, j + col - 1));
}
else
{
var currentRegion = this.GetRegion(sheet, cell);
var rowEnd = currentRegion.LastRow;
foreach (var rcell in cells)
{
var region = this.GetRegion(sheet, rcell);
if (region != null)
{
RemoveRegion(sheet, region);
}
}
sheet.AddMergedRegion(new CellRangeAddress(i, rowEnd, j, j + col - 1));
}
Console.WriteLine($"@@@@@:{i},{i},{j},{j + col - 1}");
}
j = j + col - 1;
}
//Console.WriteLine($">>>>>>>>>>>>>{i}x{j}:{cell?.StringCellValue}");
}
//break;
}
return result;
}
private void SetColWidth(int lastHeadNum, ISheet sheet)
{
for (int i = lastHeadNum; i < sheet.LastRowNum; i++)
{
var row = sheet.GetRow(i);
if (row != null)
{
for (int j = 0; j < row.LastCellNum; j++)
{
var cell = sheet.GetRow(i).GetCell(j);
if (cell != null)
{
if (i == lastHeadNum && string.IsNullOrEmpty(cell.StringCellValue))
{
for (int k = 0; k < lastHeadNum+1; k++)
{
if (i - k >= 0)
{
var tempCell = sheet.GetRow(i - k).GetCell(j);
if (tempCell.StringCellValue != "")
{
cell = tempCell;
break;
}
}
}
}
var length = ((cell.ToString().Split('\n').Select(o => Encoding.UTF8.GetBytes(o).Count()).Max())+1)* 256;
if (length > sheet.GetColumnWidth(j))
{
sheet.SetColumnWidth(j, length);
}
}
}
}
}
}
private CellRangeAddress GetRegion(ISheet sheet,ICell cell)
{
CellRangeAddress result = null;
if(cell.IsMergedCell)
{
foreach (var region in sheet.MergedRegions)
{
if (cell.RowIndex >= region.FirstRow && cell.RowIndex <= region.LastRow && cell.ColumnIndex >= region.FirstColumn && cell.ColumnIndex <= region.LastColumn)
{
result = region;
break;
}
}
}
return result;
}
private void RemoveRegion(ISheet sheet, CellRangeAddress region)
{
for (int i = 0; i < sheet.NumMergedRegions; i++)
{
var item = sheet.GetMergedRegion(i);
if(item.FirstRow==region.FirstRow
&&item.LastRow==region.LastRow
&&item.FirstColumn==region.FirstColumn
&&item.LastColumn==region.LastColumn)
{
sheet.RemoveMergedRegion(i);
break;
}
}
}
private ICellStyle CreateStyle(ISheet sheet,short? backgroundColor=null,double? fontSize=null,short? fontColor=null,bool isBold=false,string fontName=null,bool? textWrap=null)
{
var style = sheet.Workbook.CreateCellStyle();
if(backgroundColor.HasValue)
{
style.FillPattern = FillPattern.SolidForeground;
style.FillForegroundColor = backgroundColor.Value;
}
var font = sheet.Workbook.CreateFont();
if(!string.IsNullOrEmpty(fontName))
{
font.FontName = fontName;
}
font.IsBold = isBold;
if(fontSize.HasValue)
{
font.FontHeightInPoints = fontSize.Value;
}
if(fontColor.HasValue)
{
font.Color = fontColor.Value;
}
if(textWrap.HasValue)
{
style.WrapText = textWrap.Value;
}
style.SetFont(font);
style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center;
style.BorderTop = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
return style;
NPOIHelper.SetColWidth(3, sheet);
}
private void EntityToModel(Teacher entity, EditTeacherModel model)
@ -755,7 +550,7 @@ namespace TeacherExt.Controllers
if (entity.JobStart.HasValue)
{
model.JobAgeYear = DateTime.Now.Year - entity.JobStart.Value.Year;
model.JobAgeMonth = model.JobAgeYear*12 + Math.Abs((entity.JobStart.Value.Month - DateTime.Now.Month));
model.JobAgeMonth = model.JobAgeYear * 12 + Math.Abs((entity.JobStart.Value.Month - DateTime.Now.Month));
}
}
}

@ -1,24 +0,0 @@
using NPOI.SS.UserModel;
namespace TeacherExt.Controllers
{
public static class NPOIExt
{
public static ICell SetType(this ICell cell, CellType type)
{
cell.SetCellType(type);
return cell;
}
public static ICell SetStyle(this ICell cell, ICellStyle style)
{
cell.CellStyle = style;
return cell;
}
public static IRow Order(this IRow row,int order)
{
return row;
}
}
}

@ -0,0 +1,225 @@
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
namespace TeacherExt.Controllers
{
public static class NPOIHelper
{
public static ICell SetType(this ICell cell, CellType type)
{
cell.SetCellType(type);
return cell;
}
public static ICell SetStyle(this ICell cell, ICellStyle style)
{
cell.CellStyle = style;
return cell;
}
public static IRow Order(this IRow row, int order)
{
return row;
}
public static ICellStyle CreateStyle(ISheet sheet, short? backgroundColor = null, double? fontSize = null, short? fontColor = null, bool isBold = false, string fontName = null, bool? textWrap = null)
{
var style = sheet.Workbook.CreateCellStyle();
if (backgroundColor.HasValue)
{
style.FillPattern = FillPattern.SolidForeground;
style.FillForegroundColor = backgroundColor.Value;
}
var font = sheet.Workbook.CreateFont();
if (!string.IsNullOrEmpty(fontName))
{
font.FontName = fontName;
}
font.IsBold = isBold;
if (fontSize.HasValue)
{
font.FontHeightInPoints = fontSize.Value;
}
if (fontColor.HasValue)
{
font.Color = fontColor.Value;
}
if (textWrap.HasValue)
{
style.WrapText = textWrap.Value;
}
style.SetFont(font);
style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center;
style.BorderTop = BorderStyle.Thin;
style.BorderRight = BorderStyle.Thin;
style.BorderBottom = BorderStyle.Thin;
style.BorderLeft = BorderStyle.Thin;
return style;
}
public static CellRangeAddress GetRegion(ISheet sheet, ICell cell)
{
CellRangeAddress result = null;
if (cell.IsMergedCell)
{
foreach (var region in sheet.MergedRegions)
{
if (cell.RowIndex >= region.FirstRow && cell.RowIndex <= region.LastRow && cell.ColumnIndex >= region.FirstColumn && cell.ColumnIndex <= region.LastColumn)
{
result = region;
break;
}
}
}
return result;
}
public static void RemoveRegion(ISheet sheet, CellRangeAddress region)
{
for (int i = 0; i < sheet.NumMergedRegions; i++)
{
var item = sheet.GetMergedRegion(i);
if (item.FirstRow == region.FirstRow
&& item.LastRow == region.LastRow
&& item.FirstColumn == region.FirstColumn
&& item.LastColumn == region.LastColumn)
{
sheet.RemoveMergedRegion(i);
break;
}
}
}
public static void SetColWidth(int lastHeadNum, ISheet sheet)
{
for (int i = lastHeadNum; i < sheet.LastRowNum; i++)
{
var row = sheet.GetRow(i);
if (row != null)
{
for (int j = 0; j < row.LastCellNum; j++)
{
var cell = sheet.GetRow(i).GetCell(j);
if (cell != null)
{
if (i == lastHeadNum && string.IsNullOrEmpty(cell.StringCellValue))
{
for (int k = 0; k < lastHeadNum + 1; k++)
{
if (i - k >= 0)
{
var tempCell = sheet.GetRow(i - k).GetCell(j);
if (tempCell.StringCellValue != "")
{
cell = tempCell;
break;
}
}
}
}
var length = ((cell.ToString().Split('\n').Select(o => Encoding.UTF8.GetBytes(o).Count()).Max()) + 1) * 256;
if (length > sheet.GetColumnWidth(j))
{
sheet.SetColumnWidth(j, length);
}
}
}
}
}
}
public static List<string> CreateHader(ISheet sheet, Type modelType, List<string> includeHeaders)
{
var style = NPOIHelper.CreateStyle(sheet, backgroundColor: HSSFColor.PaleBlue.Index, fontSize: 11, fontColor: HSSFColor.White.Index, isBold: true, fontName: "黑体", textWrap: true);
var result = new List<string>();
var headers = modelType.GetProperties().Select(o => o.GetCustomAttribute<ExcelHeaderAttribute>()).Where(o => o != null).ToList();
var colIndex = 0;
foreach (var item in headers)
{
if (!includeHeaders.Any() || includeHeaders.Contains(item.Header))
{
result.Add(item.Header);
int i = 0;
foreach (var head in item.Headers)
{
for (int j = i; j < i + head.Value; j++)
{
var row = (sheet.GetRow(j) ?? sheet.CreateRow(j));
var cell = (row.GetCell(colIndex) ?? row.CreateCell(colIndex));
cell.SetStyle(style);
if (j == i)
{
cell.SetCellValue(head.Key);
}
}
if (head.Value > 1)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i + head.Value - 1, colIndex, colIndex));
sheet.GetRow(i).GetCell(colIndex).SetStyle(style);
}
i = i + head.Value;
}
colIndex += 1;
}
}
for (int i = 0; i < sheet.LastRowNum; i++)
{
var row = sheet.GetRow(i);
for (int j = 0; j < row.LastCellNum; j++)
{
var cell = row.GetCell(j);
var cells = new List<ICell>() { cell };
if (cell != null && cell.StringCellValue != "")
{
var col = 1;
if (j + 1 < row.LastCellNum)
{
for (int k = j + 1; k < row.LastCellNum; k++)
{
var next = row.GetCell(k);
if (next == null || next.StringCellValue != cell.StringCellValue)
{
break;
}
next.SetCellValue("");
cells.Add(next);
col++;
}
}
if (col > 1)
{
if (!cell.IsMergedCell)
{
sheet.AddMergedRegion(new CellRangeAddress(i, i, j, j + col - 1));
}
else
{
var currentRegion = NPOIHelper.GetRegion(sheet, cell);
var rowEnd = currentRegion.LastRow;
foreach (var rcell in cells)
{
var region = NPOIHelper.GetRegion(sheet, rcell);
if (region != null)
{
NPOIHelper.RemoveRegion(sheet, region);
}
}
sheet.AddMergedRegion(new CellRangeAddress(i, rowEnd, j, j + col - 1));
}
}
j = j + col - 1;
}
}
}
return result;
}
}
}
Loading…
Cancel
Save