Former-commit-id: fdcfd31cf44ce9f175f64698cddbe4741fe40741
Former-commit-id: b1def7400cb3f37fd1e6aef8c886864282dfd0ac
1.0
wanggang 5 years ago
parent f11bf1f191
commit aaad9c9936

@ -381,7 +381,7 @@ namespace TeacherExt.Controllers
private void ExportInternal(ISheet sheet, QueryTeacherModel model) private void ExportInternal(ISheet sheet, QueryTeacherModel model)
{ {
var style = this.CreateStyle(sheet); var style = this.CreateStyle(sheet,fontName:"宋体",fontSize:11);
var headers = CreateHader(sheet, model); var headers = CreateHader(sheet, model);
var list = this.Query(model) var list = this.Query(model)
@ -541,34 +541,12 @@ 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);
} }
for (int i = 0; i < rowIndex; i++) this.SetColWidth(3,sheet);
{
sheet.AutoSizeColumn(i);
var row = sheet.GetRow(i);
if (row != null)
{
for (int j = 0; j < colIndex; j++)
{
var cell = sheet.GetRow(i).GetCell(j);
if (cell != null)
{
var length = cell.ToString().Split('\n').Select(o=> Encoding.UTF8.GetBytes(o).Count()).Max()*256;
if (length > sheet.GetColumnWidthInPixels(j))
{
if (!cell.IsMergedCell)
{
sheet.SetColumnWidth(j, length);
}
}
}
}
}
}
} }
private List<string> CreateHader(ISheet sheet, QueryTeacherModel model) private List<string> CreateHader(ISheet sheet, QueryTeacherModel model)
{ {
var style = this.CreateStyle(sheet, HSSFColor.PaleBlue.Index, 11,HSSFColor.White.Index,true,"黑体"); 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 result = new List<string>();
var headers = typeof(EditTeacherModel).GetProperties().Select(o => o.GetCustomAttribute<ExcelHeaderAttribute>()).Where(o => o != null).ToList(); var headers = typeof(EditTeacherModel).GetProperties().Select(o => o.GetCustomAttribute<ExcelHeaderAttribute>()).Where(o => o != null).ToList();
var colIndex = 0; var colIndex = 0;
@ -655,6 +633,46 @@ namespace TeacherExt.Controllers
return result; 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())* 256;
if (length > sheet.GetColumnWidth(j))
{
sheet.SetColumnWidth(j, length);
}
}
}
}
}
}
private CellRangeAddress GetRegion(ISheet sheet,ICell cell) private CellRangeAddress GetRegion(ISheet sheet,ICell cell)
{ {
CellRangeAddress result = null; CellRangeAddress result = null;
@ -688,10 +706,9 @@ namespace TeacherExt.Controllers
} }
} }
private ICellStyle CreateStyle(ISheet sheet,short? backgroundColor=null,double? fontSize=null,short? fontColor=null,bool isBold=false,string fontName=null) 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(); var style = sheet.Workbook.CreateCellStyle();
style.WrapText = true;
if(backgroundColor.HasValue) if(backgroundColor.HasValue)
{ {
style.FillPattern = FillPattern.SolidForeground; style.FillPattern = FillPattern.SolidForeground;
@ -711,6 +728,10 @@ namespace TeacherExt.Controllers
{ {
font.Color = fontColor.Value; font.Color = fontColor.Value;
} }
if(textWrap.HasValue)
{
style.WrapText = textWrap.Value;
}
style.SetFont(font); style.SetFont(font);
style.Alignment = HorizontalAlignment.Center; style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center; style.VerticalAlignment = VerticalAlignment.Center;

Loading…
Cancel
Save