EPPlus:查找整个行在Excel中是否为空 [英] EPPlus: Find if the entire row is empty in Excel

查看:497
本文介绍了EPPlus:查找整个行在Excel中是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在.net核心Web API中使用EPPlus库。在上述方法中,我要验证他是否上传了excel。我想确定我的整个行是否为空。我有以下代码:

I am using EPPlus library in my .net core web api. In the said method I want to validate he uploaded excel. I want to find out if my entire row is empty. I have the following code:

using (ExcelPackage package = new ExcelPackage(file.OpenReadStream()))
{
    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
    int rowCount = worksheet.Dimension.End.Row;
    int colCount = worksheet.Dimension.End.Column;

    //loop through rows and columns
    for (int row = 1; row <= rowCount; row++)
    {
        for (int col = 1; col <= ColCount; col++)
        {
            var rowValue = worksheet.Cells[row, col].Value;
            //Want to find here if the entire row is empty
        }
    }
}

rowValue会给我特定单元格是否为空。

rowValue above would give me if the particular cell if empty or not. Is it possible to check for the entire row and proceed to next row if empty.

推荐答案

您可以在 for 在行级循环。然后循环所有单元格,并在单元格不为空时更改布尔值。

You can set a bool in the for loop at row level. Then loop all the cells and change the bool when a cell is not empty.

//loop through rows and columns
for (int row = 1; row <= rowCount; row++)
{
    //create a bool
    bool RowIsEmpty = true;

    for (int col = 1; col <= colCount; col++)
    {
        //check if the cell is empty or not
        if (worksheet.Cells[row, col].Value != null)
        {
            RowIsEmpty = false;
        }
    }

    //display result
    if (RowIsEmpty)
    {
        Label1.Text += "Row " + row + " is empty.<br>";
    }
}

这篇关于EPPlus:查找整个行在Excel中是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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