XlsxFileResult.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. namespace Admin.NET.Core;
  7. /// <summary>
  8. /// Excel文件ActionResult
  9. /// </summary>
  10. /// <typeparam name="T"></typeparam>
  11. public class XlsxFileResult<T> : XlsxFileResultBase where T : class, new()
  12. {
  13. public string FileDownloadName { get; }
  14. public ICollection<T> Data { get; }
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. /// <param name="data"></param>
  19. /// <param name="fileDownloadName"></param>
  20. public XlsxFileResult(ICollection<T> data, string fileDownloadName = null)
  21. {
  22. FileDownloadName = fileDownloadName;
  23. Data = data;
  24. }
  25. public override async Task ExecuteResultAsync(ActionContext context)
  26. {
  27. var exporter = new ExcelExporter();
  28. var bytes = await exporter.ExportAsByteArray(Data);
  29. var fs = new MemoryStream(bytes);
  30. await DownloadExcelFileAsync(context, fs, FileDownloadName);
  31. }
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. public class XlsxFileResult : XlsxFileResultBase
  37. {
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="stream"></param>
  42. /// <param name="fileDownloadName"></param>
  43. public XlsxFileResult(Stream stream, string fileDownloadName = null)
  44. {
  45. Stream = stream;
  46. FileDownloadName = fileDownloadName;
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="bytes"></param>
  52. /// <param name="fileDownloadName"></param>
  53. public XlsxFileResult(byte[] bytes, string fileDownloadName = null)
  54. {
  55. Stream = new MemoryStream(bytes);
  56. FileDownloadName = fileDownloadName;
  57. }
  58. public Stream Stream { get; protected set; }
  59. public string FileDownloadName { get; protected set; }
  60. public override async Task ExecuteResultAsync(ActionContext context)
  61. {
  62. await DownloadExcelFileAsync(context, Stream, FileDownloadName);
  63. }
  64. }
  65. /// <summary>
  66. /// 基类
  67. /// </summary>
  68. public class XlsxFileResultBase : ActionResult
  69. {
  70. /// <summary>
  71. /// 下载Excel文件
  72. /// </summary>
  73. /// <param name="context"></param>
  74. /// <param name="stream"></param>
  75. /// <param name="downloadFileName"></param>
  76. /// <returns></returns>
  77. protected virtual async Task DownloadExcelFileAsync(ActionContext context, Stream stream, string downloadFileName)
  78. {
  79. var response = context.HttpContext.Response;
  80. response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  81. downloadFileName ??= Guid.NewGuid().ToString("N") + ".xlsx";
  82. if (string.IsNullOrEmpty(Path.GetExtension(downloadFileName))) downloadFileName += ".xlsx";
  83. context.HttpContext.Response.Headers.Append("Content-Disposition", new[] { "attachment; filename=" + HttpUtility.UrlEncode(downloadFileName) });
  84. await stream.CopyToAsync(context.HttpContext.Response.Body);
  85. }
  86. }