|
|
@ -12,18 +12,11 @@ using Newtonsoft.Json.Serialization;
|
|
|
|
using NPOI.SS.UserModel;
|
|
|
|
using NPOI.SS.UserModel;
|
|
|
|
using NPOI.XSSF.UserModel;
|
|
|
|
using NPOI.XSSF.UserModel;
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using TeacherExt.Entities;
|
|
|
|
using TeacherExt.Entities;
|
|
|
|
using TeacherExt.Models;
|
|
|
|
using TeacherExt.Models;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
using NPOI.SS.Util;
|
|
|
|
|
|
|
|
using NPOI.HSSF.Util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace TeacherExt.Controllers
|
|
|
|
namespace TeacherExt.Controllers
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -381,9 +374,9 @@ namespace TeacherExt.Controllers
|
|
|
|
|
|
|
|
|
|
|
|
private void ExportInternal(ISheet sheet, QueryTeacherModel model)
|
|
|
|
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)
|
|
|
|
var list = this.Query(model)
|
|
|
|
.ToList()
|
|
|
|
.ToList()
|
|
|
|
.Select(o =>
|
|
|
|
.Select(o =>
|
|
|
@ -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(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);
|
|
|
|
row.Order(56).CreateCell(++colIndex).SetStyle(style).SetType(CellType.String).SetCellValue(string.IsNullOrEmpty(teacher.Comment) ? "无" : teacher.Comment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.SetColWidth(3,sheet);
|
|
|
|
NPOIHelper.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;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void EntityToModel(Teacher entity, EditTeacherModel model)
|
|
|
|
private void EntityToModel(Teacher entity, EditTeacherModel model)
|
|
|
|