ExportsHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FastReport.Export;
  5. using FastReport.Export.Html;
  6. using FastReport.Export.Image;
  7. using System.Reflection;
  8. using System.Diagnostics.CodeAnalysis;
  9. #if !OPENSOURCE
  10. using FastReport.Export.Csv;
  11. using FastReport.Export.OoXML;
  12. using FastReport.Export.Pdf;
  13. using FastReport.Export.BIFF8;
  14. using FastReport.Export.RichText;
  15. using FastReport.Export.LaTeX;
  16. using FastReport.Export.Zpl;
  17. using FastReport.Export.Xml;
  18. using FastReport.Export.Mht;
  19. using FastReport.Export.Hpgl;
  20. using FastReport.Export.Odf;
  21. using FastReport.Export.Dbf;
  22. using FastReport.Export.Text;
  23. using FastReport.Export.XAML;
  24. using FastReport.Export.Svg;
  25. using FastReport.Export.Ppml;
  26. using FastReport.Export.PS;
  27. using FastReport.Export.Json;
  28. using FastReport.Export.Dxf;
  29. using FastReport.Export.Email;
  30. #endif
  31. namespace FastReport.Web
  32. {
  33. internal static class ExportsHelper
  34. {
  35. static readonly ExportInfo[] ExportsCollection;
  36. static ExportsHelper()
  37. {
  38. ExportsCollection = new ExportInfo[]
  39. {
  40. #if !OPENSOURCE
  41. new ExportInfo("latex", Exports.LaTeX, typeof(LaTeXExport), false),
  42. new ExportInfo("xls", Exports.Excel97, typeof(Excel2003Document), false),
  43. new ExportInfo("zpl", Exports.Zpl, typeof(ZplExport), false),
  44. new ExportInfo("hpgl", Exports.Hpgl, typeof(HpglExport), false),
  45. new ExportInfo("pdf", Exports.Pdf, typeof(PDFExport), true),
  46. new ExportInfo("rtf", Exports.Rtf, typeof(RTFExport), true),
  47. new ExportInfo("mht", Exports.Mht, typeof(MHTExport), false),
  48. new ExportInfo("xml", Exports.XmlExcel, typeof(XMLExport), true),
  49. new ExportInfo("docx", Exports.Word2007, typeof(Word2007Export), true),
  50. new ExportInfo("xlsx", Exports.Excel2007, typeof(Excel2007Export), true),
  51. new ExportInfo("pptx", Exports.PowerPoint2007, typeof(PowerPoint2007Export), true),
  52. new ExportInfo("ods", Exports.Ods, typeof(ODSExport), true),
  53. new ExportInfo("odt", Exports.Odt, typeof(ODTExport), true),
  54. new ExportInfo("xps", Exports.Xps, typeof(XPSExport), false),
  55. new ExportInfo("csv", Exports.Csv, typeof(CSVExport), false),
  56. new ExportInfo("dbf", Exports.Dbf, typeof(DBFExport), false),
  57. new ExportInfo("txt", Exports.Text, typeof(TextExport), false),
  58. new ExportInfo("xaml", Exports.Xaml, typeof(XAMLExport), false),
  59. new ExportInfo("svg", Exports.Svg, typeof(SVGExport), true),
  60. new ExportInfo("ppml", Exports.Ppml, typeof(PPMLExport), false),
  61. new ExportInfo("ps", Exports.PS, typeof(PSExport), false),
  62. new ExportInfo("json", Exports.Json, typeof(JsonExport), false),
  63. new ExportInfo("dxf", Exports.Dxf, typeof(DxfExport), false),
  64. new ExportInfo("email", Exports.Email, typeof(EmailExport), true),
  65. #endif
  66. new ExportInfo("html", Exports.HTML, typeof(HTMLExport), true),
  67. //new ExportInfo("png", Exports.Image, typeof(ImageExport), true),
  68. //new ExportInfo("jpeg", Exports.Image, typeof(ImageExport), true),
  69. //new ExportInfo("gif", Exports.Image, typeof(ImageExport), true),
  70. //new ExportInfo("tiff", Exports.Image, typeof(ImageExport), true),
  71. //new ExportInfo("bmp", Exports.Image, typeof(ImageExport), true),
  72. //new ExportInfo("metafile", Exports.Image, typeof(ImageExport), true),
  73. new ExportInfo("fpx", Exports.Prepared, null, false),
  74. };
  75. }
  76. internal static Exports GetExportFromExt(string extension)
  77. {
  78. var exportProperty = GetInfoFromExt(extension);
  79. return exportProperty.Export;
  80. }
  81. internal static string GetExtFromExportType(Exports type)
  82. {
  83. var exportProperty = GetInfoFromExport(type);
  84. return exportProperty.Extension;
  85. }
  86. internal static ExportBase GetExport(Exports type)
  87. {
  88. var exportProperty = GetInfoFromExport(type);
  89. return (ExportBase)Activator.CreateInstance(exportProperty.ExportType);
  90. }
  91. internal static ExportBase GetExport(string extension)
  92. {
  93. var exportProperty = GetInfoFromExt(extension);
  94. return (ExportBase)Activator.CreateInstance(exportProperty.ExportType);
  95. }
  96. internal static string GetLocalization(ToolbarLocalization localization, Exports type)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. #if BLAZOR && !WASM
  101. internal static string GetHref(WebReport webReport)
  102. {
  103. string href = webReport.template_export_url(GetExtFromExportType(webReport.ExportType));
  104. return href;
  105. }
  106. #endif
  107. internal static ExportInfo GetInfoFromExt(string ext)
  108. {
  109. var exportProperty = ExportsCollection
  110. .FirstOrDefault(export => export.Extension == ext);
  111. if (exportProperty.Extension == null)
  112. throw new Exception("Unknown export extension");
  113. return exportProperty;
  114. }
  115. internal static ExportInfo GetInfoFromExport(Exports type)
  116. {
  117. var exportProperty = ExportsCollection
  118. .FirstOrDefault(export => export.Export == type);
  119. if (exportProperty.Extension == null)
  120. throw new Exception("Unknown export type");
  121. return exportProperty;
  122. }
  123. internal readonly struct ExportInfo
  124. {
  125. public readonly string Extension;
  126. public readonly Exports Export;
  127. [DynamicallyAccessedMembers(LinkerFlags.ExportTypeMembers)]
  128. public readonly Type ExportType;
  129. public readonly bool HaveSettings;
  130. public PropertyInfo[] Properties
  131. => ExportType?.GetProperties(BindingFlags.Public | BindingFlags.Instance)
  132. .Where(property => property.CanWrite)
  133. .ToArray();
  134. public ExportBase CreateExport()
  135. {
  136. if(Export != Exports.Prepared)
  137. return (ExportBase)Activator.CreateInstance(ExportType);
  138. else
  139. return null;
  140. }
  141. public ExportInfo(string extension, Exports export, [DynamicallyAccessedMembers(LinkerFlags.ExportTypeMembers)] Type exportType, bool haveSettings)
  142. {
  143. Extension = extension;
  144. ExportType = exportType;
  145. Export = export;
  146. HaveSettings = haveSettings;
  147. }
  148. }
  149. }
  150. }