128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using ExcelDataReader;
|
|
using System.Threading;
|
|
using xAiModels.Models;
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract content from stream as Rich Result ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileExtractionResult> ExtractRichAsync(
|
|
Stream fileStream,
|
|
string fileName,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var content = await ExtractAsync(
|
|
mimeType: mimeType,
|
|
fileStream: fileStream,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
var result = new XFileExtractionResult
|
|
{
|
|
Text = content,
|
|
FileName = fileName,
|
|
MimeType = mimeType
|
|
};
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |