Delete top 3 rows from xls using excel.interop.object library
c#, excel, office-interop
Solution
Remove for loop(for (int i = 1; i <= 3; i++)) in your code it will work.
Your already selecting the range from A1 to A3(Three rows) .........
Excel.Range range = work.get_Range("A1", "A3");
So no need to loop once again for same Range like below.....
//for (int i = 1; i <= 3; i++)
//{
entireRow.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
//}
Problem
I am using excel.interop.object library 9.0 to manipulate my xls file in c# If suppose i have a xls that have 5 sheets. All sheets having some rows. I am using following code to delete top 2 rows from each sheet in my xls ``` Excel.Application excelApp = new Excel.Application(); Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(mstrFilePath, 1, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, null, false); try { Excel.Sheets excelWorkSheet = excelWorkbook.Sheets; foreach (Excel.Worksheet work in excelWorkSheet) { Excel.Range range = work.get_Range("A1", "A3"); Excel.Range entireRow = range.EntireRow; // update for (int i = 1; i <= 3; i++) { entireRow.Delete(Excel.XlDirection.xlUp); } } //excelWorkbook.Close(false, mstrFilePath, null); } catch (Exception ex) { } finally { excelApp.Quit(); } ``` But it is misbehaving deleting 2 rows from sheet 3 and not deleting any rows from any other sheet. what went wrong here ?