How to delete rows from Excel file by C# code?

Posted on 2006-06-06
Last Modified: 2011-09-20
I need to delete rows from an existing excel file.
I can open the file and read and write, but I also need  to delete rows from it.
I saw fome VB.NET examples but could not modify them to C#.

I use this code to open a file and a worksheet:

oXLMisc = new Excel.Application();
oWBMisc = oXLMisc.Workbooks.Open(miscFileName, 0, false, 5, "", "", true,
                    Excel.XlPlatform.xlWindows, "\t", false, false, 0, 0, 0, 0);
sheetsMisc = oWBMisc.Worksheets;
oSheetMisc = (Excel.Worksheet)sheetsMisc.get_Item(1);

But a statement like one if the two below fails.
oSheetMisc.get_Range("B1", "B300").Delete("Left");
oSheetMisc.Rows.get_Range("B1", "B300").Delete("Left");

I'll appreciate your help.
Question by:pavelmed
    LVL 6

    Expert Comment

    LVL 2

    Accepted Solution

    look if this can help:

    object om = Missing.Value;
    Microsoft.Office.Interop.Excel.Application oXLMisc = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook oWBMisc = oXLMisc.Workbooks.Open(@"d:\sample.xls", om, false, om, om, om, om, om, om, om, om, om, om, om, om);
    Microsoft.Office.Interop.Excel.Worksheet oSheetMisc = (Microsoft.Office.Interop.Excel.Worksheet)oWBMisc.Worksheets[1];
    oSheetMisc.get_Range("A3", "A4").Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftToLeft);
    oWBMisc.Close(true, om, om);

    Author Comment

    I could not use the info from Muhammad's suggestions, but Heidar's code worked well:
    oSheetMisc.get_Range("A3", "A4").Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftToLeft);

    It is just needed to repeat this as many times as there are cells in a row.

    Thank you!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now