Updating
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Serilog;
|
||||
using DocumentFormat.OpenXml.Packaging;
|
||||
using DocumentFormat.OpenXml.Spreadsheet;
|
||||
using LCU.Library.Internal.Exceptions;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Responses;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Requests;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.ExcelServices;
|
||||
|
||||
public class ExcelService : IExcelService
|
||||
{
|
||||
public ExcelDataResponse ExtractExcelWorkBookData(ExcelDataRequest excelDataRequest)
|
||||
{
|
||||
var excelDataResponse = new ExcelDataResponse();
|
||||
try
|
||||
{
|
||||
var fileBytes = Convert.FromBase64String(excelDataRequest.FileContent);
|
||||
using var memoryStream = new MemoryStream(fileBytes);
|
||||
|
||||
using SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(memoryStream, false);
|
||||
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
|
||||
Workbook workbook = workbookPart.Workbook;
|
||||
foreach (Sheet sheet in workbook.GetFirstChild<Sheets>().Elements<Sheet>())
|
||||
{
|
||||
SheetRequest sheetRequest = null;
|
||||
if (excelDataRequest.SkipSheetForExtraction(sheet.Name, out sheetRequest))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var sheetResponse = new SheetResponse(sheet.Name);
|
||||
var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
|
||||
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().FirstOrDefault();
|
||||
if (sheetData == null)
|
||||
{
|
||||
Log.Information($"Sheet named {sheet.Name} does not have any sheet data.");
|
||||
continue;
|
||||
}
|
||||
|
||||
sheetResponse = ExtractRowData(sheetData, sheetRequest, sheetResponse);
|
||||
excelDataResponse.WorkBook.SheetResponses.Add(sheetResponse);
|
||||
}
|
||||
|
||||
return excelDataResponse;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
var message = $"Failure during ExtractExcelWorkBookData. Exception Message: {exception.Message}";
|
||||
Log.Error(message, exception);
|
||||
throw new ServiceException(message);
|
||||
}
|
||||
}
|
||||
|
||||
private SheetResponse ExtractRowData(SheetData sheetData, SheetRequest sheetRequest, SheetResponse sheetResponse)
|
||||
{
|
||||
foreach (var row in sheetData.Elements<Row>())
|
||||
{
|
||||
if (!int.TryParse(row.RowIndex, out int rowIndex))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sheetRequest.SkipRowForExtraction(rowIndex))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
sheetResponse = ExtractCellData(row, rowIndex, sheetRequest, sheetResponse);
|
||||
}
|
||||
|
||||
return sheetResponse;
|
||||
}
|
||||
|
||||
private SheetResponse ExtractCellData(Row row, int rowIndex, SheetRequest sheetRequest, SheetResponse sheetResponse)
|
||||
{
|
||||
foreach (var cell in row.Elements<Cell>())
|
||||
{
|
||||
var columnName = GetColumnName(cell.CellReference);
|
||||
if (sheetRequest.SkipColumnForExtraction(columnName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(cell is null || cell.CellValue is null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var cellResponse = new CellResponse()
|
||||
{
|
||||
Row = rowIndex,
|
||||
ColumnName = columnName,
|
||||
Value = cell.CellValue.InnerText,
|
||||
Formula = cell.CellFormula is null ? string.Empty : cell.CellFormula.InnerText
|
||||
};
|
||||
sheetResponse.CellResponses.Add(cellResponse);
|
||||
}
|
||||
|
||||
return sheetResponse;
|
||||
}
|
||||
|
||||
private static string GetColumnName(string cellReference)
|
||||
{
|
||||
string columnName = "";
|
||||
foreach (char character in cellReference)
|
||||
{
|
||||
if (!char.IsLetter(character))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
columnName += character;
|
||||
}
|
||||
return columnName;
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Requests;
|
||||
using LCU.Library.Internal.FileOperationsModels.Models.ExcelModels.Responses;
|
||||
|
||||
namespace LCU.Api.Internal.FileOperations.Services.ExcelServices;
|
||||
|
||||
public interface IExcelService
|
||||
{
|
||||
ExcelDataResponse ExtractExcelWorkBookData(ExcelDataRequest excelDataRequest);
|
||||
}
|
||||
Reference in New Issue
Block a user