Epplus删除特定行中的所有行 [英] Epplus delete all rows from specific row

查看:124
本文介绍了Epplus删除特定行中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式从特定(空)行中删除所有后续行?我尝试了Cyclus

It is possible to somehow delete all following rows from specific (empty) row ? I tried for cyclus

            for (int rowNum = 1; rowNum <= worksheet.Dimension.End.Row; rowNum++)
            {
                var rowCells = from cell in worksheet.Cells
                               where (cell.Start.Row == rowNum)
                               select cell;

                if (!rowCells.Any(cell => cell.Value != null))
                {
                    worksheet.DeleteRow(rowNum);
                }

            }

但是在excel中需要几分钟是数百万个空行。

but it takes minutes if in excel are millions of empty rows.

Epplus提供此方法 worksheet.DeleteRow(int rowFrom,int rows)但我不知道所有其他空行的计数。

Epplus offer this method worksheet.DeleteRow(int rowFrom, int rows) but i do not know the count of all additional empty rows.

在下面的示例中,我需要删除所有12+行,但是问题是我不知道特定行,空行开始的地方。

In following example i need to delete all rows 12+ but the problem is that i do not know the specific row, where the empty rows begin.

替代方法可以找到最后一个非空行并删除具有该范围的所有内容,这会更快,但是表中还有一个空行的问题。

The alternative aproach can be finding last non empty row and delete everything with the range, which will be faster, but there is another issue with empty row inside the table.

ws.DeleteRow(lastFilledTableRow, workSheet.Dimension.End.Row - tableRowsCount,true);

在此示例中,问题是红色行,但也许我会告诉用户这种出色的功能格式无效,请规避问题。

In this example the problem is the red row but maybe i will tell the users that this kind of excel format is invalid and circumvent the problem.

推荐答案

我知道它很旧,但是我找不到任何解决方案,因此将其作为我自己的解决方案。
它正在检查最后一行是否为空,如果是,它将删除它,直到找到非空行。 (非空意味着这里:该行中的所有列都有一定的值)

I know that it is old but I could not find any solution so made one my by own. It is checking the last row if it is empty and if yes it deletes it and doing this until finds non-empty row. (non-empty means here: all columns in this row have some value)

worksheet.TrimLastEmptyRows();

public static void TrimLastEmptyRows(this ExcelWorksheet worksheet)
    {
        while (worksheet.IsLastRowEmpty())
            worksheet.DeleteRow(worksheet.Dimension.End.Row);
    }

public static bool IsLastRowEmpty(this ExcelWorksheet worksheet)
    {
        var empties = new List<bool>();

        for (int i = 1; i <= worksheet.Dimension.End.Column; i++)
        {
            var rowEmpty = worksheet.Cells[worksheet.Dimension.End.Row, i].Value == null ? true : false;
            empties.Add(rowEmpty);
        }

        return empties.All(e => e);
    }

这篇关于Epplus删除特定行中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆