Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having Issue while reading a excel file with .xls extension #1294

Closed
Sivaprasadnelli opened this issue Mar 14, 2024 Discussed in #1112 · 4 comments
Closed

Having Issue while reading a excel file with .xls extension #1294

Sivaprasadnelli opened this issue Mar 14, 2024 Discussed in #1112 · 4 comments
Labels

Comments

@Sivaprasadnelli
Copy link

Hello team,

I have a query to ask. We are using NPOI Library to read .xls and .xlsx files in our .Net Core application. While we are uploading a .xls file and there is one empty cell in a single row but while reading the rows it is not considering the empty cell even though we are trying to check for the null or empty values. Could you let know is it expecting behavior?

// Iterate through rows and cells to read data from Excel file
for (int i = 0; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
// Check if all cells in the row are empty or blank using TrueForAll
if (Enumerable.Range(0, row.Cells.Count).All(index => row.Cells[index].CellType == CellType.Blank))
{
continue; // Skip the row if all cells are empty
}
ICell cell = null;
try
{
foreach (ICell currentCell in row.Cells)
{
if (string.IsNullOrEmpty(currentCell.ToString().Trim()))
{
throw new InvalidDataException(INVALID_DATA_UPLOADED);
}
}
lines.Add(row.Cells.Select(cell => cell.ToString()).ToArray());
}
catch (Exception ex)
{
throw new Exception();
}
}
}

@Sivaprasadnelli
Copy link
Author

Can Someone please help me here. It is bit urgent for us.

@Bykiev
Copy link
Collaborator

Bykiev commented Mar 14, 2024

In your sample I don't see the check for null or blank cell

currentCell == null || currentCell.CellType == CellType.Blank

@Sivaprasadnelli
Copy link
Author

Sivaprasadnelli commented Mar 14, 2024 via email

@Bykiev
Copy link
Collaborator

Bykiev commented Mar 14, 2024

I believe your code should be looking like this:

// Iterate through rows and cells to read data from Excel file
for (int i = 0; i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row != null)
{
     // Check if all cells in the row are empty or blank using TrueForAll
     if (Enumerable.Range(0, row.Cells.Count).All(index => row.Cells[index].CellType == CellType.Blank))
     {
          continue; // Skip the row if all cells are empty
     }
    
     for (int i = 0; i < row.Cells.Count; i++) // possibly you should use fixed columns number
     {
          var currentCell = row.GetCell(i);

           if (currentCell == null || currentCell.CellType == CellType.Blank)
                throw new InvalidDataException(INVALID_DATA_UPLOADED);
      }
     lines.Add(row.Cells.Select(cell => cell.ToString()).ToArray());
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants