using System; using System.IO; using System.Data; using System.Linq; using System.Text; using ExcelDataReader; using System.Threading; using System.Threading.Tasks; using xAiApi.Interfaces.Extractors; namespace xAiApi.Providers.Extractors { public class XExcelFileContentExtractor : IXExcelFileContentExtractor { /// /// Supported MIME Types ... /// private static readonly string[] SupportedMimeTypes = [ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xlsx "application/vnd.ms-excel" // .xls ]; /// /// Check if this extractor supports the specified MIME type ... /// public bool CanExtract(string mimeType) { // return SupportedMimeTypes.Contains( mimeType?.ToLowerInvariant() ?? string.Empty ); } /// /// Extract text content from file stream ... /// public async Task ExtractAsync( Stream fileStream, string mimeType, CancellationToken cancellationToken = default ) { // Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // using var memoryStream = new MemoryStream(); await fileStream.CopyToAsync(memoryStream, cancellationToken); memoryStream.Position = 0; // var stringBuilder = new StringBuilder(); // using (var reader = ExcelReaderFactory.CreateReader(memoryStream)) { // var result = reader.AsDataSet(); foreach (DataTable table in result.Tables) { // stringBuilder.AppendLine($"--- Sheet: {table.TableName} ---"); // var headers = new string[table.Columns.Count]; for (int i = 0; i < table.Columns.Count; i++) { headers[i] = table.Columns[i].ColumnName; } stringBuilder.AppendLine(string.Join(" | ", headers)); stringBuilder.AppendLine(new string('-', 50)); // foreach (DataRow row in table.Rows) { // var rowValues = new string[row.ItemArray.Length]; for (int i = 0; i < row.ItemArray.Length; i++) { rowValues[i] = row.ItemArray[i]?.ToString()?.Trim() ?? string.Empty; } stringBuilder.AppendLine(string.Join(" | ", rowValues)); } stringBuilder.AppendLine(); } } // return stringBuilder.ToString(); } } }