last ...
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Supported MIME Types ...
|
||||
/// </summary>
|
||||
private static readonly string[] SupportedMimeTypes =
|
||||
[
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xlsx
|
||||
"application/vnd.ms-excel" // .xls
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Check if this extractor supports the specified MIME type ...
|
||||
/// </summary>
|
||||
public bool CanExtract(string mimeType)
|
||||
{
|
||||
//
|
||||
return SupportedMimeTypes.Contains(
|
||||
mimeType?.ToLowerInvariant() ?? string.Empty
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract text content from file stream ...
|
||||
/// </summary>
|
||||
public async Task<string> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user