Commit 1b74a3e2 authored by 黄奎's avatar 黄奎

11111

parent c22c784d
...@@ -19,4 +19,10 @@ ...@@ -19,4 +19,10 @@
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" /> <PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="Aspose.Cells">
<HintPath>..\lib\Aspose.Cells.dll</HintPath>
</Reference>
</ItemGroup>
</Project> </Project>
using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
namespace Edu.Common.Plugin
{
/// <summary>
/// Aspose.Cell帮助类
/// </summary>
public class AsposeCellHelper
{
/// <summary>
/// 模板导出
/// </summary>
/// <param name="list">模板数据</param>
/// <param name="isTempLate">是否模板导出</param>
/// <param name="tempLatePath">模板文件路劲</param>
public static byte[] ToExcelExtend(List<ExcelDataSource> list, bool isTempLate = false, string tempLatePath = "")
{
Workbook workbook = null;
Worksheet sheet;
if (isTempLate && !string.IsNullOrEmpty(tempLatePath.Trim()))
{
using (FileStream file = new FileStream(tempLatePath, FileMode.Open, FileAccess.Read))
{
//将文件流中模板加载到工作簿对象中
workbook = new Workbook(file);
}
sheet = workbook.Worksheets[0];
}
else
{
workbook = new Workbook();
sheet = workbook.Worksheets[0];
}
int rowIndex = 0;
Cells cells = sheet.Cells;
//生成行
foreach (var item in list)
{
int columnsIndex = 0;
//设置行高
if (item.ColumnHight > 0)
{
cells.SetRowHeight(rowIndex, item.ColumnHight);
}
else
{
cells.SetRowHeight(rowIndex, 25);
}
//生成列
foreach (var subItem in item.ExcelRows)
{
Style style = cells[rowIndex, columnsIndex].GetStyle();
//自动换行
style.IsTextWrapped = true;
//设置单元宽度
if (subItem.CellWidth > 0)
{
cells.SetColumnWidth(columnsIndex, subItem.CellWidth);
}
string cellValue = subItem.Value ?? "";
//设置单元格背景颜色
switch (subItem.BgColorEnum)
{
case ColorEnum.BrightGreen:
style.ForegroundColor = Color.DodgerBlue;
//fCellStyle.FillPattern = FillPattern.SolidForeground;
break;
case ColorEnum.DarkBlue:
style.ForegroundColor = Color.DarkBlue;
//fCellStyle.FillPattern = FillPattern.SolidForeground;
break;
case ColorEnum.Red:
style.ForegroundColor = Color.Red;
//fCellStyle.FillPattern = FillPattern.SolidForeground;
break;
}
////字体大小
//if (subItem.FontHeight > 0)
//{
// style.Font.Size = subItem.FontHeight * 20;
//}
//else
//{
// style.Font.Size = 11 * 20;
//}
//字体
if (!string.IsNullOrWhiteSpace(subItem.FontName))
{
style.Font.Name = subItem.FontName;
}
else
{
style.Font.Name = "宋体";
}
if (subItem.FontSize > 0)
{
style.Font.Size = subItem.FontSize;
}
// 设置字体颜色
switch (subItem.FontColorEnum)
{
case ColorEnum.Red:
style.Font.Color = Color.Red;
break;
case ColorEnum.Green:
style.Font.Color = Color.Green;
break;
case ColorEnum.DarkBlue:
style.Font.Color = Color.DarkBlue;
break;
default:
style.Font.Color = Color.Black;
break;
}
//字体加粗
if (subItem.IsBold)
{
style.Font.IsBold = subItem.IsBold;
}
//水平对齐方式
style.HorizontalAlignment = subItem.HAlignmentEnum switch
{
HAlignmentEnum.CENTER => TextAlignmentType.Center,
HAlignmentEnum.LEFT => TextAlignmentType.Left,
HAlignmentEnum.RIGHT => TextAlignmentType.Right,
_ => TextAlignmentType.Center,
};
//垂直对齐方式
style.VerticalAlignment = subItem.VAlignmentEnum switch
{
VAlignmentEnum.CENTER => TextAlignmentType.Center,
VAlignmentEnum.TOP => TextAlignmentType.Top,
VAlignmentEnum.BOTTOM => TextAlignmentType.Bottom,
_ => TextAlignmentType.Center,
};
if (!subItem.IsSetBorder)
{
style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
}
else
{
style.SetBorder(BorderType.BottomBorder, CellBorderType.None, Color.Black);
style.SetBorder(BorderType.LeftBorder, CellBorderType.None, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.None, Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.None, Color.Black);
}
cells[rowIndex, columnsIndex].SetStyle(style);
cells[rowIndex, columnsIndex].PutValue(cellValue);
columnsIndex++;
}
rowIndex++;
}
using MemoryStream ms = workbook.SaveToStream();
ms.Flush();
ms.Position = 0;
return ms.ToArray();
}
}
}
...@@ -1179,11 +1179,11 @@ namespace Edu.WebApi.Controllers.Course ...@@ -1179,11 +1179,11 @@ namespace Edu.WebApi.Controllers.Course
{ {
ExcelRows = new List<ExcelColumn>() ExcelRows = new List<ExcelColumn>()
}; };
firstRow.ExcelRows.Add(new ExcelColumn(value: "报名课程")); firstRow.ExcelRows.Add(new ExcelColumn(value: "报名课程") { CellWidth=15});
foreach (var item in list) foreach (var item in list)
{ {
JObject jobj = JObject.Parse(Common.Plugin.JsonHelper.Serialize(item)); JObject jobj = JObject.Parse(Common.Plugin.JsonHelper.Serialize(item));
firstRow.ExcelRows.Add(new ExcelColumn(value: jobj.GetStringValue("CourseName"))); firstRow.ExcelRows.Add(new ExcelColumn(value: jobj.GetStringValue("CourseName")) { CellWidth = 15 });
} }
slist.Add(firstRow); slist.Add(firstRow);
...@@ -1222,6 +1222,7 @@ namespace Edu.WebApi.Controllers.Course ...@@ -1222,6 +1222,7 @@ namespace Edu.WebApi.Controllers.Course
{ {
var item = JObject.Parse(JsonHelper.Serialize(list[j])); var item = JObject.Parse(JsonHelper.Serialize(list[j]));
var ChapterList = Common.Plugin.JsonHelper.DeserializeObject<List<object>>(item.GetStringValue("ChapterList")); var ChapterList = Common.Plugin.JsonHelper.DeserializeObject<List<object>>(item.GetStringValue("ChapterList"));
string str = "";
if (ChapterList != null && i <= ChapterList.Count - 1) if (ChapterList != null && i <= ChapterList.Count - 1)
{ {
var sObj = JObject.Parse(ChapterList[i].ToString()); ; var sObj = JObject.Parse(ChapterList[i].ToString()); ;
...@@ -1229,21 +1230,23 @@ namespace Edu.WebApi.Controllers.Course ...@@ -1229,21 +1230,23 @@ namespace Edu.WebApi.Controllers.Course
int State = sObj.GetInt("State"); int State = sObj.GetInt("State");
if (State == 4) if (State == 4)
{ {
dataRow.ExcelRows.Add(new ExcelColumn(value: chapterName) { } ); str = "【缺课】\r\n" + chapterName;
dataRow.ExcelRows.Add(new ExcelColumn(value: str) { FontColorEnum = ColorEnum.Red });
} }
else else
{ {
dataRow.ExcelRows.Add(new ExcelColumn(value: "【已上课】" + chapterName)); str = "【已上课】\r\n" + chapterName;
dataRow.ExcelRows.Add(new ExcelColumn(value: str) { FontColorEnum = ColorEnum.DarkBlue });
} }
} }
else else
{ {
dataRow.ExcelRows.Add(new ExcelColumn(value: "")); dataRow.ExcelRows.Add(new ExcelColumn(value: str) );
} }
}; };
slist.Add(dataRow); slist.Add(dataRow);
} }
var byteData = ExcelTempLateHelper.ToExcelExtend(slist); var byteData = AsposeCellHelper.ToExcelExtend(slist);
return File(byteData, "application/octet-stream", excelName); return File(byteData, "application/octet-stream", excelName);
} }
catch (Exception ex) catch (Exception ex)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment