|
|
@@ -100,42 +100,68 @@ public static class CommonUtil
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 导出模板Excel
|
|
|
/// </summary>
|
|
|
/// <param name="fileName"></param>
|
|
|
- /// <param name="fileDto"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static async Task<IActionResult> ExportExcelTemplate(string fileName, dynamic fileDto)
|
|
|
+ public static async Task<IActionResult> ExportExcelTemplate<T>(string fileName) where T : class, new()
|
|
|
{
|
|
|
fileName = $"{fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
|
|
|
-
|
|
|
IImporter importer = new ExcelImporter();
|
|
|
- MethodInfo generateTemplateMethod = importer.GetType().GetMethod("GenerateTemplate");
|
|
|
- MethodInfo closedGenerateTemplateMethod = generateTemplateMethod.MakeGenericMethod(fileDto.GetType());
|
|
|
- var res = await (Task<ExportFileInfo>)closedGenerateTemplateMethod.Invoke(importer, new object[] { Path.Combine(App.WebHostEnvironment.WebRootPath, fileName) });
|
|
|
-
|
|
|
+ var res = await importer.GenerateTemplate<T>(Path.Combine(App.WebHostEnvironment.WebRootPath, fileName));
|
|
|
return new FileStreamResult(new FileStream(res.FileName, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 导出模板Excel
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="fileName"></param>
|
|
|
+ /// <param name="fileDto"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static async Task<IActionResult> ExportExcelTemplate(string fileName, dynamic fileDto)
|
|
|
+ {
|
|
|
+ MethodInfo generateTemplateMethod = typeof(CommonUtil).GetMethods().FirstOrDefault(p => p.Name == "ExportExcelTemplate" && p.IsGenericMethodDefinition);
|
|
|
+ MethodInfo closedGenerateTemplateMethod = generateTemplateMethod.MakeGenericMethod(fileDto.GetType());
|
|
|
+ return await (Task<IActionResult>)closedGenerateTemplateMethod.Invoke(null, new object[] { fileName });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 导入数据Excel
|
|
|
/// </summary>
|
|
|
+ /// <typeparam name="T"></typeparam>
|
|
|
/// <param name="file"></param>
|
|
|
- /// <param name="dataDto"></param>
|
|
|
/// <returns></returns>
|
|
|
- public static async Task<dynamic> ImportExcelData([Required] IFormFile file, dynamic dataDto)
|
|
|
+ public static async Task<ICollection<T>> ImportExcelData<T>([Required] IFormFile file) where T : class, new()
|
|
|
{
|
|
|
var newFile = await App.GetRequiredService<SysFileService>().UploadFile(file, "");
|
|
|
- var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, newFile.FilePath, newFile.Id.ToString(), newFile.Suffix);
|
|
|
+ var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, newFile.FilePath, newFile.Id.ToString() + newFile.Suffix);
|
|
|
|
|
|
IImporter importer = new ExcelImporter();
|
|
|
- MethodInfo importMethod = importer.GetType().GetMethod("Import");
|
|
|
- MethodInfo closedImportMethod = importMethod.MakeGenericMethod(dataDto.GetType());
|
|
|
- var res = await (Task<dynamic>)closedImportMethod.Invoke(importer, new object[] { filePath });
|
|
|
+ var res = await importer.Import<T>(filePath);
|
|
|
if (res == null || res.Exception != null)
|
|
|
throw Oops.Oh("导入异常:" + res.Exception);
|
|
|
-
|
|
|
return res.Data;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 导入数据Excel
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="file"></param>
|
|
|
+ /// <param name="dataDto"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static async Task<dynamic> ImportExcelData([Required] IFormFile file, dynamic dataDto)
|
|
|
+ {
|
|
|
+ MethodInfo importMethod = typeof(CommonUtil).GetMethods().FirstOrDefault(p => p.Name == "ImportExcelData" && p.IsGenericMethodDefinition);
|
|
|
+ MethodInfo closedImportMethod = importMethod.MakeGenericMethod(dataDto.GetType());
|
|
|
+ var task = (Task)closedImportMethod.Invoke(null, new object[] { file });
|
|
|
+ await task;
|
|
|
+ return task.GetType().GetProperty("Result").GetValue(task);
|
|
|
+ }
|
|
|
}
|