【问题标题】:C# NPOI read EXCEL cells from top to bottomC# NPOI 从上到下读取 EXCEL 单元格
【发布时间】:2021-11-22 07:01:30
【问题描述】:

我有从左到右阅读EXCEL表格的顺序

for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
    IRow row = sheet.GetRow(i);
    if (row == null) continue;
    if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
    for (int j = row.FirstCellNum; j < cellCount; j++)
    {
        if (row.GetCell(j) != null)
        {
            if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && 
                !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
            {
                rowList.Add(row.GetCell(j).ToString());
            }
        }
    }
}

我正在从第一张纸中获取所有文本,但我需要从上到下阅读每一列。

这是我尝试过的,但它最终从随机单元格中得到 1 个单词循环...

for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
{
    IRow row = sheet.GetRow(i);
    if (row == null) continue;
    if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
    for (int j = sheet.FirstRowNum; j <= sheet.LastRowNum; j++)
    {
        if (row.GetCell(i) != null)
        {
            if (!string.IsNullOrEmpty(row.GetCell(i).ToString()) && 
                !string.IsNullOrWhiteSpace(row.GetCell(i).ToString()))
            {
                rowList.Add(row.GetCell(i).ToString());
            }
        }
    }                   
}

【问题讨论】:

    标签: c# excel xlsx xls npoi


    【解决方案1】:

    当您从上到下阅读每一列时,您在第二个for 循环中调用row.GetCell(i) 而不是row.GetCell(j)

    for (int i = headerRow.FirstCellNum; i < headerRow.LastCellNum; i++)
    {
        IRow row = sheet.GetRow(i);
        if (row == null) continue;
        if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
        for (int j = sheet.FirstRowNum; j <= sheet.LastRowNum; j++)
        {
            if (row.GetCell(j) != null)
            {
                if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && 
                    !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
                {
                    rowList.Add(row.GetCell(j).ToString());
                }
            }
        }                   
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多