|
@@ -7,6 +7,8 @@
|
|
|
// 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
|
|
// 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
|
|
|
// 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
|
|
// 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
|
|
|
|
|
|
|
|
|
|
+using Magicodes.ExporterAndImporter.Core;
|
|
|
|
|
+using Magicodes.ExporterAndImporter.Excel;
|
|
|
using System.Xml;
|
|
using System.Xml;
|
|
|
using System.Xml.Linq;
|
|
using System.Xml.Linq;
|
|
|
using System.Xml.Serialization;
|
|
using System.Xml.Serialization;
|
|
@@ -90,4 +92,43 @@ public static class CommonUtil
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /// <summary>
|
|
|
|
|
+ /// 导出模板Excel
|
|
|
|
|
+ /// </summary>
|
|
|
|
|
+ /// <param name="fileName"></param>
|
|
|
|
|
+ /// <param name="fileDto"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ public static async Task<IActionResult> ExportExcelTemplate(string fileName, dynamic fileDto)
|
|
|
|
|
+ {
|
|
|
|
|
+ fileName = $"{fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
|
|
|
|
|
+
|
|
|
|
|
+ IImporter importer = new ExcelImporter();
|
|
|
|
|
+ MethodInfo generateTemplateMethod = typeof(ExcelImporter).GetMethod("GenerateTemplate");
|
|
|
|
|
+ MethodInfo closedGenerateTemplateMethod = generateTemplateMethod.MakeGenericMethod(fileDto.GetType());
|
|
|
|
|
+ var res = await (Task<dynamic>)closedGenerateTemplateMethod.Invoke(importer, new object[] { 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="file"></param>
|
|
|
|
|
+ /// <param name="dataDto"></param>
|
|
|
|
|
+ /// <returns></returns>
|
|
|
|
|
+ public static async Task<dynamic> ImportExcelData([Required] IFormFile file, dynamic dataDto)
|
|
|
|
|
+ {
|
|
|
|
|
+ var newFile = await App.GetRequiredService<SysFileService>().UploadFile(file, "");
|
|
|
|
|
+ var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, newFile.FilePath, newFile.Name);
|
|
|
|
|
+
|
|
|
|
|
+ IImporter importer = new ExcelImporter();
|
|
|
|
|
+ MethodInfo importMethod = typeof(ExcelImporter).GetMethod("Import");
|
|
|
|
|
+ MethodInfo closedImportMethod = importMethod.MakeGenericMethod(dataDto.GetType());
|
|
|
|
|
+ var res = await (Task<dynamic>)closedImportMethod.Invoke(importer, new object[] { filePath });
|
|
|
|
|
+ if (res == null || res.Exception != null)
|
|
|
|
|
+ throw Oops.Oh("导入异常:" + res.Exception);
|
|
|
|
|
+
|
|
|
|
|
+ return res.Data;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|