feat:构建还原
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Module.Excel
|
||||
{
|
||||
public class ExcelManager
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
namespace Yi.Framework.Module.Excel
|
||||
{
|
||||
public class OemClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 导出excel
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="entityList"></param>
|
||||
/// <param name="filePath"></param>
|
||||
public void Export<T>(List<T> entityList, string filePath)
|
||||
{
|
||||
var properties = typeof(T).GetProperties().Where(x => x.GetCustomAttribute<DisplayNameAttribute>() is not null).Where(x => x.GetGetMethod().IsPublic).ToList();
|
||||
// 创建工作簿
|
||||
IWorkbook workbook = new XSSFWorkbook();
|
||||
// 创建工作表
|
||||
ISheet sheet = workbook.CreateSheet("sheet");
|
||||
|
||||
|
||||
// 写入表头
|
||||
IRow headerRow = sheet.CreateRow(0);
|
||||
for (int j = 0; j < properties.Count(); j++)
|
||||
{
|
||||
headerRow.CreateCell(j).SetCellValue(properties[j].GetCustomAttribute<DisplayNameAttribute>()!.DisplayName);
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
for (int i = 0; i < entityList.Count(); i++)
|
||||
{
|
||||
var currentEntity = entityList[i];
|
||||
IRow dataRow = sheet.CreateRow(i + 1);
|
||||
for (int j = 0; j < properties.Count(); j++)
|
||||
{
|
||||
var currentPropertiy = properties[j];
|
||||
|
||||
//只处理简单类型
|
||||
dataRow.CreateCell(j).SetCellValue(JsonSerializer.Serialize(currentPropertiy.GetValue(currentEntity)).TrimStart("\"".ToCharArray()).TrimEnd("\"".ToCharArray()));
|
||||
}
|
||||
}
|
||||
|
||||
// 保存文件
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
|
||||
{
|
||||
workbook.Write(fs);
|
||||
}
|
||||
|
||||
workbook.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 导入excel
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public List<T> Import<T>(string filePath) where T : new()
|
||||
{
|
||||
List<T> result = new();
|
||||
|
||||
Dictionary<int, PropertyInfo> propHas = new Dictionary<int, PropertyInfo>();
|
||||
var properties = typeof(T).GetProperties().Where(x => x.GetCustomAttribute<DisplayNameAttribute>() is not null).Where(x => x.GetGetMethod().IsPublic).ToList();
|
||||
|
||||
|
||||
// 创建文件流
|
||||
using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
// 创建工作簿
|
||||
IWorkbook workbook = new XSSFWorkbook(fileStream);
|
||||
|
||||
// 选择第一个工作表
|
||||
ISheet sheet = workbook.GetSheetAt(0);
|
||||
|
||||
//获取表头
|
||||
IRow headerRow = sheet.GetRow(0);
|
||||
|
||||
|
||||
for (int col = 0; col < headerRow.LastCellNum; col++)
|
||||
{
|
||||
// 获取单元格的值
|
||||
ICell cell = headerRow.GetCell(col);
|
||||
var property = properties.Where(x => x.GetCustomAttribute<DisplayNameAttribute>().DisplayName == cell.StringCellValue).FirstOrDefault();
|
||||
if (property is not null && !propHas.Values.Contains(property))
|
||||
{
|
||||
propHas[col] = property;
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历行
|
||||
for (int row = 1; row <= sheet.LastRowNum; row++)
|
||||
{
|
||||
//一行一个对象
|
||||
|
||||
|
||||
IRow currentRow = sheet.GetRow(row);
|
||||
var currentResult = new T();
|
||||
// 遍历列
|
||||
for (int col = 0; col < currentRow.LastCellNum; col++)
|
||||
{
|
||||
// 获取单元格的值
|
||||
ICell cell = currentRow.GetCell(col);
|
||||
string? cellValue = cell.ToString();
|
||||
object value = cellValue.TrimStart("\"".ToCharArray()).TrimEnd("\"".ToCharArray());
|
||||
|
||||
value = Convert.ChangeType(cellValue, propHas[col].PropertyType);
|
||||
|
||||
|
||||
propHas[col].SetValue(currentResult, value);
|
||||
|
||||
}
|
||||
|
||||
result.Add(currentResult);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.23" />
|
||||
<PackageReference Include="CSRedisCore" Version="3.8.670" />
|
||||
<PackageReference Include="NPOI" Version="2.6.2" />
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
|
||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
|
||||
|
||||
@@ -37,7 +37,7 @@ public class Startup : AppStartup
|
||||
services.AddSchedule(options =>
|
||||
{
|
||||
// 注册作业,并配置作业触发器
|
||||
//options.AddJob<TestJob>(Triggers.Period(10000));
|
||||
options.AddJob<SystemDataJob>(Triggers.Period(10000));
|
||||
//options.AddJob<SystemDataJob>(Triggers.Period(10000));
|
||||
options.AddJob<SystemDataJob>(Triggers.Cron("0 0 0,12 ? * ?", CronStringFormat.WithSeconds)); // 表示每天凌晨与12点
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user