Base solution for your next web application
Open Closed

Upload Excel #9682


User avatar
0
A4amen created

Prerequisites

Please answer the following questions before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • What is your product version? 9.1
  • What is your product type (Angular or MVC)? Angular or MVC
  • What is product framework type (.net framework or .net core)?

I am trying to replicate the user excel upload for uploading contacts. I have successfully done every on the angular app and create all the service in the backend but for some reason the background task is not excecuting After investigation I realized this class is preventing the task from running

`` public class ContactsListExcelDataReader : NpoiExcelImporterBase<ImportContactsDto>, IContactListExcelDataReader { private readonly ILocalizationSource _localizationSource;

    public ContactsListExcelDataReader(ILocalizationManager localizationManager)
    {
        _localizationSource = localizationManager.GetSource(CoreConsts.LocalizationSourceName);
    }

    public List&lt;ImportContactsDto&gt; GetContactsFromExcel(byte[] fileBytes)
    {
        return ProcessExcelFile(fileBytes, ProcessExcelRow);
    }

    private ImportContactsDto ProcessExcelRow(ISheet worksheet, int row)
    {
        if (IsRowEmpty(worksheet, row))
        {
            return null;
        }

        var exceptionMessage = new StringBuilder();
        var contact = new ImportContactsDto();

        try
        {
            contact.FirstName = GetRequiredValueFromRowOrNull(worksheet, row, 0, nameof(contact.FirstName), exceptionMessage);
            contact.LastName = GetRequiredValueFromRowOrNull(worksheet, row, 1, nameof(contact.LastName), exceptionMessage);
           // contact.Gender =  (Gender)Enum.Parse(typeof(Gender), GetRequiredValueFromRowOrNull(worksheet, row, 2, nameof(contact.Gender), exceptionMessage));
            worksheet.GetRow(row).Cells[3].SetCellType(CellType.String);
           // contact.DateOfBirth = DateTime.Parse(GetRequiredValueFromRowOrNull(worksheet, row, 3, nameof(contact.DateOfBirth), exceptionMessage));
           // contact.CountryCode = (Country) Enum.Parse(typeof(Country), GetRequiredValueFromRowOrNull(worksheet, row, 4, "Country", exceptionMessage));
            worksheet.GetRow(row).Cells[5].SetCellType(CellType.String);
            contact.MobileNumber = worksheet.GetRow(row).Cells[5]?.StringCellValue;
            contact.Email = GetRequiredValueFromRowOrNull(worksheet, row, 5, nameof(contact.Email), exceptionMessage);
            contact.Address = GetRequiredValueFromRowOrNull(worksheet, row, 6, nameof(contact.Address), exceptionMessage);
            contact.Tag = GetRequiredValueFromRowOrNull(worksheet, row, 7, nameof(contact.Tag), exceptionMessage);
            contact.Note = GetRequiredValueFromRowOrNull(worksheet, row, 8, nameof(contact.Note), exceptionMessage);
            //contact.Tag = GetAssignedRoleNamesFromRow(worksheet, row, 6);
        }
        catch (System.Exception exception)
        {
            contact.Exception = exception.Message;
        }

        return contact;
    }

    private string GetRequiredValueFromRowOrNull(ISheet worksheet, int row, int column, string columnName, StringBuilder exceptionMessage)
    {
        var cellValue = worksheet.GetRow(row).Cells[column].StringCellValue;
        if (cellValue != null && !string.IsNullOrWhiteSpace(cellValue))
        {
            return cellValue;
        }

        exceptionMessage.Append(GetLocalizedExceptionMessagePart(columnName));
        return null;
    }

    private string[] GetAssignedRoleNamesFromRow(ISheet worksheet, int row, int column)
    {
        var cellValue = worksheet.GetRow(row).Cells[column].StringCellValue;
        if (cellValue == null || string.IsNullOrWhiteSpace(cellValue))
        {
            return new string[0];
        }

        return cellValue.ToString().Split(':').Where(s => !string.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray();
    }

    private string GetLocalizedExceptionMessagePart(string parameter)
    {
        return _localizationSource.GetString("{0}IsInvalid", _localizationSource.GetString(parameter)) + "; ";
    }

    private bool IsRowEmpty(ISheet worksheet, int row)
    {
        var cell = worksheet.GetRow(row)?.Cells.FirstOrDefault();
        return cell == null || string.IsNullOrWhiteSpace(cell.StringCellValue);
    }
}
``

Kindly help me fix this.


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @A4amen

    Do you get any exception or is it just nothing happens ?

  • User Avatar
    0
    A4amen created

    It just does not happen.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @A4amen

    It will be hard to detect problem this way. Is it possible to share your project via email with [email protected] ?

    Thanks,