?
Solved

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

Posted on 2006-06-06
3
Medium Priority
?
2,486 Views
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");
OR
oSheetMisc.Rows.get_Range("B1", "B300").Delete("Left");

I'll appreciate your help.
0
Comment
Question by:pavelmed
3 Comments
 
LVL 2

Accepted Solution

by:
HeidarV earned 2000 total points
ID: 16849353
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);
oXLMisc.Quit();
0
 

Author Comment

by:pavelmed
ID: 16856999
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!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

850 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