Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Find empty cells and write them inside with ClosedXml

I have this problem, I have installed ClosedXml:

I have an Excel file already created and populated, now I should find the blank line below the already populated one and write some data

Example:

[A, 1] = name; [B, 1] = surname;

the next line will be empty and I will pass some variables to populate the cells going to the right.

OpenFileDialog FileExcel = new OpenFileDialog();

        if (FileExcel.ShowDialog() == DialogResult.OK)
        {
            try
            {
                var sr = new StreamReader(FileExcel.FileName);
            }
            catch (SecurityException ex)
            {
                MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                $"Details:\n\n{ex.StackTrace}");
            }
        }


using (var excelWorkbook = new XLWorkbook(FileExcel.FileName))
        {
            var nonEmptyDataRows = excelWorkbook.Worksheet(Convert.ToInt32(comboBox1.SelectedItem)).RowsUsed();

            foreach (var dataRow in nonEmptyDataRows)
            {
                //for row number check
                if (dataRow.RowNumber() >= 1 && dataRow.RowNumber() <= 100)
                {

                }
            }
        }
like image 933
Mrpit Avatar asked Mar 12 '26 12:03

Mrpit


1 Answers

Use row.Cells(false) instead of row.Cells(). It does not skip over unused cells. Then you can simply check of cell.Value() is empty

like image 81
dev Joshi Avatar answered Mar 15 '26 02:03

dev Joshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!