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

fix issue #436 skipped cell issue #437

Merged
merged 1 commit into from
Dec 8, 2020
Merged

Conversation

tony1223
Copy link
Contributor

As title, this PR fixed #436 issue.

@tony1223
Copy link
Contributor Author

tony1223 commented Oct 12, 2020

Test Case. The generated excel should have first and second empty cell.

var workbook = new SXSSFWorkbook();
ISheet worksheet = workbook.CreateSheet("Sheet");
var maxRow = 1000;
for (int rownum = 0; rownum < maxRow; rownum++)
{
    IRow row = ((SXSSFSheet) worksheet).CreateRow(rownum);
    for (int celnum = 2; celnum < 34; celnum++)
    {
        ICell Cell = row.CreateCell(celnum);
        Cell.SetCellValue("Cell: Row-" + rownum + ";CellNo:" + celnum);
    }
}
FileStream sw = File.Open("test.xlsx", FileMode.Create);
workbook.Write(sw);
sw.Close();

@tonyqus tonyqus added this to the NPOI 2.5.3 milestone Oct 20, 2020
@Warkanlock
Copy link

Hey guys, when this will be available for use? I was having the issue as #436 using SXSSF

@canceriens
Copy link

Hi

I am having the exact same issue but I am using XSSFWorkbook object and I am reading from the Excel (XLSX) file. Empty cells get skipped and the remaining cells shift towards the left.

When I use HSSFWorkbook to read data from Excel (XLS) file that works perfectly fine.

Please let me know whether this fix would also work when reading data from excel files and if yes when is this going to be released?

Many Thanks,

@kweaver32
Copy link

I'm also experiencing this issue when using SXSSF. My current use case requires me to use SXSSF due to large file requirements and out of memory exceptions.

@tony1223
Copy link
Contributor Author

You could create every cell (including empty cell) when you writing for workaround.

@tony1223
Copy link
Contributor Author

tony1223 commented Oct 22, 2020

@canceriens would you mind to provide the sample code for reading / the sample xlsx file, and I could check if its covered by the fix .

It's hard to image the test case for me, do you used row.GetEnumerator() ?
If yes,after the fix you could use row.AllCellsIterator() for all cells (including empty one).

But if you read cell by get cell, it shouldn't be a issue. Need more details to dig into this issue.

@tonyqus
Copy link
Member

tonyqus commented Oct 22, 2020

@canceriens I don't think your issue is same as what fixed in this PR. You should use GetCell to get the cell object instead of iterating Cells. This is a common mistake for NPOI newbies. And I don't think this PR is a good place to discuss your issue since it's a bit misleading.

@tonyqus tonyqus modified the milestones: NPOI 2.5.3, NPOI 2.5.2 Oct 24, 2020
@tonyqus tonyqus modified the milestones: NPOI 2.5.2, NPOI 2.5.3 Nov 25, 2020
@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.3 Dec 8, 2020
@tonyqus tonyqus merged commit e4b277a into nissl-lab:master Dec 8, 2020
@ReneRam
Copy link

ReneRam commented Nov 17, 2023

Is this issue solved? I'm experiencing the same with NPOI 2.6.2. I managed to fix the first occurrence found but in the second occurrence, row 85 of attached excel file still get cell shifting in 'IRow row = sheet.GetRow(i);'. Following is a snippet of my code:

` if (xlsFile.Type == ".xlsx")
{
using (var stream = new FileStream(xlsFile.Path, FileMode.Open))
{
stream.Position = 0;
XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
sheet = xssWorkbook.GetSheetAt(0);
IRow headerRow = sheet.GetRow(0);
int cellCount = headerRow.LastCellNum;
// loop cells in header row
for (int j = 0; j < cellCount; j++)
{
ICell cell = headerRow.GetCell(j);
if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
{
dt.Columns.Add(cell.ToString());
}
}
// loop cells in data row
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
IRow row = sheet.GetRow(i);
if (row == null) continue;

                    rowList.Clear(); // clear the existing rowList

                    for (int j = row.FirstCellNum; j < cellCount; j++)
                    {
                        ICell c = row.GetCell(j);

                        if (c != null)
                        {
                            switch (c.CellType)
                            {
                                case NPOI.SS.UserModel.CellType.Numeric:
                                    if (DateUtil.IsCellDateFormatted(c))
                                    {
                                        rowList.Add(c.DateCellValue.ToString());
                                    }
                                    else
                                    {
                                        rowList.Add(c.ToString());
                                    }
                                    break;

                                default:
                                    rowList.Add(c.ToString().Replace("\"", "'").TrimEnd());
                                    break;
                            }
                        }
                        else
                        {
                            // Add an empty string or null to rowList when the cell is empty
                            rowList.Add(string.Empty); // or rowList.Add(null);
                        }
                    }

                    if (rowList.Count > 0)
                        dt.Rows.Add(rowList.ToArray());
                }
            }
        }`

or even a hint/workaround on how to solve this.

Thanks

brogi.xlsx

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

Successfully merging this pull request may close these issues.

Empty cells are skipped while using SXSSF objects
7 participants