CommonUtil.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using System.Xml;
  10. using System.Xml.Linq;
  11. using System.Xml.Serialization;
  12. namespace Admin.NET.Core;
  13. /// <summary>
  14. /// 通用工具类
  15. /// </summary>
  16. public static class CommonUtil
  17. {
  18. /// <summary>
  19. /// 生成百分数
  20. /// </summary>
  21. /// <param name="PassCount"></param>
  22. /// <param name="allCount"></param>
  23. /// <returns></returns>
  24. public static string ExecPercent(decimal PassCount, decimal allCount)
  25. {
  26. string res = "";
  27. if (allCount > 0)
  28. {
  29. var value = (double)Math.Round(PassCount / allCount * 100, 1);
  30. if (value < 0)
  31. res = Math.Round(value + 5 / Math.Pow(10, 0 + 1), 0, MidpointRounding.AwayFromZero).ToString();
  32. else
  33. res = Math.Round(value, 0, MidpointRounding.AwayFromZero).ToString();
  34. }
  35. if (res == "") res = "0";
  36. return res + "%";
  37. }
  38. /// <summary>
  39. /// 获取服务地址
  40. /// </summary>
  41. /// <returns></returns>
  42. public static string GetLocalhost()
  43. {
  44. return $"{App.HttpContext.Request.Scheme}://{App.HttpContext.Request.Host.Value}";
  45. }
  46. /// <summary>
  47. /// 对象序列化XML
  48. /// </summary>
  49. /// <typeparam name="T"></typeparam>
  50. /// <param name="obj"></param>
  51. /// <returns></returns>
  52. public static string SerializeObjectToXml<T>(T obj)
  53. {
  54. if (obj == null) return string.Empty;
  55. var xs = new XmlSerializer(obj.GetType());
  56. var stream = new MemoryStream();
  57. var setting = new XmlWriterSettings
  58. {
  59. Encoding = new UTF8Encoding(false), // 不包含BOM
  60. Indent = true // 设置格式化缩进
  61. };
  62. using (var writer = XmlWriter.Create(stream, setting))
  63. {
  64. var ns = new XmlSerializerNamespaces();
  65. ns.Add("", ""); // 去除默认命名空间
  66. xs.Serialize(writer, obj, ns);
  67. }
  68. return Encoding.UTF8.GetString(stream.ToArray());
  69. }
  70. /// <summary>
  71. /// 字符串转XML格式
  72. /// </summary>
  73. /// <param name="xmlStr"></param>
  74. /// <returns></returns>
  75. public static XElement SerializeStringToXml(string xmlStr)
  76. {
  77. try
  78. {
  79. return XElement.Parse(xmlStr);
  80. }
  81. catch
  82. {
  83. return null;
  84. }
  85. }
  86. /// <summary>
  87. /// 导出模板Excel
  88. /// </summary>
  89. /// <param name="fileName"></param>
  90. /// <param name="fileDto"></param>
  91. /// <returns></returns>
  92. public static async Task<IActionResult> ExportExcelTemplate(string fileName, dynamic fileDto)
  93. {
  94. fileName = $"{fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
  95. IImporter importer = new ExcelImporter();
  96. MethodInfo generateTemplateMethod = importer.GetType().GetMethod("GenerateTemplate");
  97. MethodInfo closedGenerateTemplateMethod = generateTemplateMethod.MakeGenericMethod(fileDto.GetType());
  98. var res = await (Task<dynamic>)closedGenerateTemplateMethod.Invoke(importer, new object[] { Path.Combine(App.WebHostEnvironment.WebRootPath, fileName) });
  99. return new FileStreamResult(new FileStream(res.FileName, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
  100. }
  101. /// <summary>
  102. /// 导入数据Excel
  103. /// </summary>
  104. /// <param name="file"></param>
  105. /// <param name="dataDto"></param>
  106. /// <returns></returns>
  107. public static async Task<dynamic> ImportExcelData([Required] IFormFile file, dynamic dataDto)
  108. {
  109. var newFile = await App.GetRequiredService<SysFileService>().UploadFile(file, "");
  110. var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, newFile.FilePath, newFile.Name);
  111. IImporter importer = new ExcelImporter();
  112. MethodInfo importMethod = importer.GetType().GetMethod("Import");
  113. MethodInfo closedImportMethod = importMethod.MakeGenericMethod(dataDto.GetType());
  114. var res = await (Task<dynamic>)closedImportMethod.Invoke(importer, new object[] { filePath });
  115. if (res == null || res.Exception != null)
  116. throw Oops.Oh("导入异常:" + res.Exception);
  117. return res.Data;
  118. }
  119. }