1

I want to remove empty rows at the end of program exection. In details I am inserting some results after calculation using C# in predefined excel sheets. At the end I need to delete/remove the empty rows programatically. Could any one suggest solution. My code is little big so I am unable to include here. For your understanding I am giving some input and output view of excel. In below Output Excel the rows D and E having empty row I want to remove programatically without mentioning the range.

Input Excel /Pre defined Excel Files

A   1   2   3   4
B
C
D
E

output Excel

A   1   2   3   4
B   ABc cde nAC 123
C   cdf fed x2  123
D
E
7
  • You havent even mentioned what library you use to create your excel file. Commented May 8, 2015 at 7:21
  • I am just inserting a data to predefined template excel and also I using the Microsoft.Office.Interop.Excel reference for importing excel. Commented May 8, 2015 at 7:29
  • Look here: stackoverflow.com/questions/9951188/… Commented May 8, 2015 at 7:31
  • can you knows the row's num of input in runtime? Commented May 8, 2015 at 7:35
  • I don't know the row number, only need to find the empty rows programmatically. Commented May 8, 2015 at 8:07

4 Answers 4

2

You can do it using a Range Object. I assume here that you are using Excel interop.

Let say you have your book open, then set the range then delete it It should look something like this

ApplicationClass excel = new ApplicationClass();
//...

Microsoft.Office.Interop.Excel.Range cel = (Range)excel.Cells[rowIndex, columnIndex];
cel.Delete();

You can also try using:

for(int i = 1; i <=20; i++)
{
   excelRange = (Excel.Range)excelWorkSheet.Cells[i, 1];
   if (!string.IsNullOrEmpty(excelRange.Text.ToString()))
   {
       ((Range)excelWorkSheet.Rows[i]).Delete(excelRange);
   }
}

Check out the link below

https://social.msdn.microsoft.com/Forums/office/en-US/469fdf10-35cc-46b2-a875-7b974deb5659/how-to-delete-all-empty-rows-from-a-excel-sheet-using-microsoftofficeinteropexcel?forum=exceldev

and

https://stackoverflow.com/a/9952004/4373895 Here "Something" is your null value.

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

While including (Range)excel.Cells[rowIndex, columnIndex]; I am facing error, could you help me on this. and in second solution I need to find null value or somestring. Are you trying to say somestring as null?
I tried the social msdn, but when i am trying this below lines I am getting this error "No overload for method 'CountA' takes '1' arguments" if (application.WorksheetFunction.CountA(exlwooksheet.Rows[i]) == 0) (exlwooksheet.Rows[i] as Microsoft.Office.Interop.Excel.Range).Delete();
VikrantMore Can you look and help me on this issue?
1

You can remove empty rows in Excel file using the following code

 xlApp = new Microsoft.Office.Interop.Excel.Application();
 Microsoft.Office.Interop.Excel.Workbook excelWorkbook = xlApp.Workbooks.Open(fileName);
 Microsoft.Office.Interop.Excel._Worksheet sheet = excelWorkbook.Sheets[1];
 var LastRow = sheet.UsedRange.Rows.Count;
 LastRow = LastRow + sheet.UsedRange.Row - 1;
 for (int i = 1; i <= LastRow; i++)
 {
   if (application.WorksheetFunction.CountA(sheet.Rows[i]) == 0)
       (sheet.Rows[i] as Microsoft.Office.Interop.Excel.Range).Delete();
 }

Comments

0

The below code works perfectly and creates a new blank row for a specified row and column number :

Excel.Range rng = (Excel.Range)xlWorkSheet1.Cells[RowNumber, ColumnNumber];
                            Excel.Range Row1 = rng.EntireRow;
                            Row1.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false);

Comments

0

Extension methods that deletes empty rows and columns from an Excel worksheet.

/// <summary>
/// Deletes empty rows and columns from the end of the given worksheet
/// </summary>
public static void Trim(this Excel.Worksheet worksheet)
{
    worksheet.TrimColumns();
    worksheet.TrimRows();
}

/// <summary>
/// Deletes empty rows from the end of the given worksheet
/// </summary>
public static void TrimRows(this Excel.Worksheet worksheet)
{
    Excel.Range range = worksheet.UsedRange;
    while(worksheet.Application.WorksheetFunction.CountA(range.Rows[range.Rows.Count]) == 0)
        (range.Rows[range.Rows.Count] as Excel.Range).Delete();
}

/// <summary>
/// Deletes empty columns from the end of the given worksheet
/// </summary>
public static void TrimColumns(this Excel.Worksheet worksheet)
{
    Excel.Range range = worksheet.UsedRange;
    while(worksheet.Application.WorksheetFunction.CountA(range.Columns[range.Columns.Count]) == 0)
        (range.Columns[range.Columns.Count] as Excel.Range).Delete();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.