亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

EPPlus:在 Excel 中查找整行是否為空

EPPlus:在 Excel 中查找整行是否為空

C#
慕絲7291255 2021-12-25 18:37:52
我在我的 .net core web api 中使用 EPPlus 庫。在上述方法中,我想驗證他上傳的 excel。我想知道我的整行是否為空。我有以下代碼: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 會給我。是否可以檢查整行并在為空時繼續下一行。
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

您可以for在行級別的循環中設置 bool 。然后循環所有單元格并在單元格不為空時更改布爾值。


//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>";

    }

}


查看完整回答
反對 回復 2021-12-25
?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您可以使用 linq 檢查行單元格范圍值:


var startRow = 1;

var endRow = 1;

var columnStart = 1;

var columnEnd = worksheet.Cells.End.Column;


var cellRange = worksheet.Cells[startRow, columnStart , endRow, columnEnd];


var isRowEmpty = cellRange.All(c => c.Value == null)


查看完整回答
反對 回復 2021-12-25
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

如果您不知道要檢查的列數,則可以利用以下事實:Worksheet.Cells集合僅包含實際具有值的單元格的條目:


[TestMethod]

public void EmptyRowsTest()

{

    //Throw in some data

    var datatable = new DataTable("tblData");

    datatable.Columns.AddRange(new[] { new DataColumn("Col1", typeof(int)), new DataColumn("Col2", typeof(int)), new DataColumn("Col3", typeof(object)) });


    //Only fille every other row

    for (var i = 0; i < 10; i++)

    {

        var row = datatable.NewRow();

        if (i % 2 > 0)

        {

            row[0] = i;

            row[1] = i * 10;

            row[2] = Path.GetRandomFileName();

        }

        datatable.Rows.Add(row);

    }


    //Create a test file

    var existingFile = new FileInfo(@"c:\temp\EmptyRowsTest.xlsx");

    if (existingFile.Exists)

        existingFile.Delete();


    using (var pck = new ExcelPackage(existingFile))

    {

        var worksheet = pck.Workbook.Worksheets.Add("Sheet1");

        worksheet.Cells.LoadFromDataTable(datatable, true);


        pck.Save();

    }


    //Load from file

    using (var pck = new ExcelPackage(existingFile))

    {

        var worksheet = pck.Workbook.Worksheets["Sheet1"];


        //Cells only contains references to cells with actual data

        var cells = worksheet.Cells;

        var rowIndicies = cells

            .Select(c => c.Start.Row)

            .Distinct()

            .ToList();


        //Skip the header row which was added by LoadFromDataTable

        for (var i = 1; i <= 10; i++)

            Console.WriteLine($"Row {i} is empty: {rowIndicies.Contains(i)}");

    }

}

在輸出中給出這個(第 0 行是列標題):


Row 1 is empty: True

Row 2 is empty: False

Row 3 is empty: True

Row 4 is empty: False

Row 5 is empty: True

Row 6 is empty: False

Row 7 is empty: True

Row 8 is empty: False

Row 9 is empty: True

Row 10 is empty: False


查看完整回答
反對 回復 2021-12-25
  • 3 回答
  • 0 關注
  • 434 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號