Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

587
Visualizações
How to read import excel file with EPPLUS?
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger, ApplicationDbContext context)
    {
        _logger = logger;
        _context = context;
    }

    public IActionResult Index()
    {
        return View();
    }

    ApplicationDbContext _context;
    
    public List<Contact>Import (string fileName)
    {

        var FilePath = $"{Directory.GetCurrentDirectory()}{@""}" + "\\" + fileName;
        FileInfo fileInfo = new FileInfo(FilePath);
        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
        using (ExcelPackage package = new ExcelPackage(fileInfo))
        {
            ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
          
            int rowCount = worksheet.Dimension.End.Row;
            for (int row = 2; row <= rowCount; row++)
            {
                Contact con = new Contact();
             
                    if (row == 1) con.Libelle = worksheet.Cells[row, row].Value.ToString();
                    else if (row == 2) con.Id = (int)worksheet.Cells[row, row].Value;

               
                _context.Add(con);
                _context.SaveChanges(); 
            }
        }
        return _context.Contacts.ToList();
    }

I'm trying to read excel file with EPPLUS but i have one excepton, it is saying that System.NullReferenceException : 'Object reference not set to an instance of an object.' worksheet a été null.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I will suggest You adapt 'defensive programming' paradigm to Your approach in these cases. Specifically meaning do not write code that acts on assuming something else goes well without checking. When you do so I believe the problem will show.

Of cause community cannot help you if the problem is with the excel sheet, unless you can supply it - but I do see some helpfull suggestions as to coding style which i belive can help You solve it Yourself.

Curious to learn what You found if You give it a go :)

From:

public List<Contact>Import (string fileName)
{

    var FilePath = $"{Directory.GetCurrentDirectory()}{@""}" + "\\" + fileName;
    FileInfo fileInfo = new FileInfo(FilePath);
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    using (ExcelPackage package = new ExcelPackage(fileInfo))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();
      
        int rowCount = worksheet.Dimension.End.Row;
        for (int row = 2; row <= rowCount; row++)
        {
            Contact con = new Contact();
         
                if (row == 1) con.Libelle = worksheet.Cells[row, row].Value.ToString();
                else if (row == 2) con.Id = (int)worksheet.Cells[row, row].Value;

           
            _context.Add(con);
            _context.SaveChanges(); 
        }
    }
    return _context.Contacts.ToList();
}

TO:

public List<Contact> Import(string fileName)
{
    //Try not to use uppercase for local variables, to be able to tell them from member properties
    var FilePath = $"{Directory.GetCurrentDirectory()}{@""}" + "\\" + fileName;

    FileInfo fileInfo = new FileInfo(FilePath);
    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
    using (ExcelPackage package = new ExcelPackage(fileInfo))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.FirstOrDefault();

        //Here ok, because a workbook has at least one sheet
        int rowCount = worksheet.Dimension.End.Row;
        for (int row = 2; row <= rowCount; row++)
        {
            try
            {
                Contact con = new Contact();

                var cell = worksheet.Cells[row, row];

                if (row == 1)
                {
                    //Do You know how your package handles in case there is nothing in the cell?
                    con.Libelle = cell.Value?.ToString();
                }
                else if (row == 2)
                {
                    if (int.TryParse(cell.Value, out int parsedResult))
                    {
                        con.Id = parsedResult;
                    }
                    else throw new Exception("Invalid data"); //Your direct cast in source code justifies it not being possible to be an exceptional case 'impossible' or you wouldn't write code presuming it ;)
                }
                _context.Add(con);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                //Let Your debugger help you pinpoint the problem
                System.Diagnostics.Debug.Write($"problem with row {row} :{ex.ToString()}");
            }
        }
    }
    //Are You sure Contacts are initialized, know the implementation of  the _context
    return _context.Contacts?.ToList() ?? null;
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda