|
| 1 | +using DevExpress.Spreadsheet; |
| 2 | +using DevExpress.XtraRichEdit; |
| 3 | +using RichEditDocumentFormat = DevExpress.XtraRichEdit.DocumentFormat; |
| 4 | +using RichEditEncryptionSettings = DevExpress.XtraRichEdit.API.Native.EncryptionSettings; |
| 5 | +using SpreadsheetDocumentFormat = DevExpress.Spreadsheet.DocumentFormat; |
| 6 | +using SpreadsheetEncryptionSettings = DevExpress.Spreadsheet.EncryptionSettings; |
| 7 | + |
| 8 | +namespace RichEditOpenAIWebApi.BusinessObjects |
| 9 | +{ |
| 10 | + public static class RichEditHelper |
| 11 | + { |
| 12 | + public static async Task LoadFile(RichEditDocumentServer server, IFormFile file) |
| 13 | + { |
| 14 | + using (var stream = new MemoryStream()) |
| 15 | + { |
| 16 | + await file.CopyToAsync(stream); |
| 17 | + stream.Seek(0, SeekOrigin.Begin); |
| 18 | + server.LoadDocument(stream); |
| 19 | + } |
| 20 | + } |
| 21 | + public static Stream SaveDocument(RichEditDocumentServer server, RichEditFormat outputFormat, RichEditEncryptionSettings? encryptionSettings = null) |
| 22 | + { |
| 23 | + MemoryStream resultStream = new MemoryStream(); |
| 24 | + if (outputFormat == RichEditFormat.Pdf) |
| 25 | + server.ExportToPdf(resultStream); |
| 26 | + else |
| 27 | + { |
| 28 | + RichEditDocumentFormat documentFormat = new RichEditDocumentFormat((int)outputFormat); |
| 29 | + if (documentFormat == RichEditDocumentFormat.Html) |
| 30 | + server.Options.Export.Html.EmbedImages = true; |
| 31 | + if (encryptionSettings == null) |
| 32 | + server.SaveDocument(resultStream, documentFormat); |
| 33 | + else |
| 34 | + server.SaveDocument(resultStream, documentFormat, encryptionSettings); |
| 35 | + } |
| 36 | + resultStream.Seek(0, SeekOrigin.Begin); |
| 37 | + return resultStream; |
| 38 | + } |
| 39 | + public static string GetContentType(RichEditFormat documentFormat) |
| 40 | + { |
| 41 | + switch (documentFormat) |
| 42 | + { |
| 43 | + case RichEditFormat.Doc: |
| 44 | + case RichEditFormat.Dot: |
| 45 | + return "application/msword"; |
| 46 | + case RichEditFormat.Docm: |
| 47 | + case RichEditFormat.Docx: |
| 48 | + case RichEditFormat.Dotm: |
| 49 | + case RichEditFormat.Dotx: |
| 50 | + return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
| 51 | + case RichEditFormat.ePub: |
| 52 | + return "application/epub+zip"; |
| 53 | + case RichEditFormat.Mht: |
| 54 | + case RichEditFormat.Html: |
| 55 | + return "text/html"; |
| 56 | + case RichEditFormat.Odt: |
| 57 | + return "application/vnd.oasis.opendocument.text"; |
| 58 | + case RichEditFormat.Txt: |
| 59 | + return "text/plain"; |
| 60 | + case RichEditFormat.Rtf: |
| 61 | + return "application/rtf"; |
| 62 | + case RichEditFormat.Xml: |
| 63 | + return "application/xml"; |
| 64 | + case RichEditFormat.Pdf: |
| 65 | + return "application/pdf"; |
| 66 | + default: return string.Empty; |
| 67 | + } |
| 68 | + } |
| 69 | + public static async Task<string> FileToBase64String(IFormFile file) |
| 70 | + { |
| 71 | + using (var stream = new MemoryStream()) |
| 72 | + { |
| 73 | + await file.CopyToAsync(stream); |
| 74 | + stream.Seek(0, SeekOrigin.Begin); |
| 75 | + byte[] imageBytes = stream.ToArray(); |
| 76 | + return Convert.ToBase64String(imageBytes); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + public static class SpreadsheetHelper |
| 81 | + { |
| 82 | + |
| 83 | + public static async Task LoadWorkbook(Workbook workbook, IFormFile file) |
| 84 | + { |
| 85 | + using (var stream = new MemoryStream()) |
| 86 | + { |
| 87 | + await file.CopyToAsync(stream); |
| 88 | + stream.Seek(0, SeekOrigin.Begin); |
| 89 | + workbook.LoadDocument(stream); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static Stream SaveDocument(Workbook workbook, SpreadsheetFormat outputFormat, SpreadsheetEncryptionSettings? encryptionSettings = null) |
| 94 | + { |
| 95 | + MemoryStream resultStream = new MemoryStream(); |
| 96 | + |
| 97 | + SpreadsheetDocumentFormat documentFormat = new SpreadsheetDocumentFormat((int)outputFormat); |
| 98 | + if (outputFormat == SpreadsheetFormat.Pdf) |
| 99 | + workbook.ExportToPdf(resultStream); |
| 100 | + else |
| 101 | + { |
| 102 | + |
| 103 | + if (encryptionSettings == null) |
| 104 | + workbook.SaveDocument(resultStream, documentFormat); |
| 105 | + else |
| 106 | + workbook.SaveDocument(resultStream, documentFormat, encryptionSettings); |
| 107 | + } |
| 108 | + |
| 109 | + resultStream.Seek(0, SeekOrigin.Begin); |
| 110 | + return resultStream; |
| 111 | + } |
| 112 | + public static string GetContentType(SpreadsheetFormat documentFormat) |
| 113 | + { |
| 114 | + switch (documentFormat) |
| 115 | + { |
| 116 | + case SpreadsheetFormat.Xls: |
| 117 | + case SpreadsheetFormat.Xlt: |
| 118 | + return "application/msword"; |
| 119 | + case SpreadsheetFormat.Xlsx: |
| 120 | + case SpreadsheetFormat.Xlsb: |
| 121 | + case SpreadsheetFormat.Xlsm: |
| 122 | + case SpreadsheetFormat.Xltm: |
| 123 | + return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
| 124 | + case SpreadsheetFormat.Html: |
| 125 | + return "text/html"; |
| 126 | + case SpreadsheetFormat.XmlSpreadsheet2003: |
| 127 | + return "application/xml"; |
| 128 | + case SpreadsheetFormat.Pdf: |
| 129 | + return "application/pdf"; |
| 130 | + default: return string.Empty; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments