I am running the below code attempting to insert 3 rows in Excel, but it gives me the debug error of:
COMException was unhandled
private static _Application docExcel;
public static void AddThreeRows()
{
long i;
for(i = (int)(docExcel.Cells[docExcel.Rows.Count, "e"].End(XlDirection.XxlUp).Row); i >= 3; i--)
{
string e1 = docExcel.Cells[i, "E"].Value;
if (!string.IsNullOrEmpty(e1.ToUpper()))
{
string em1 = docExcel.Cells[i - 1, "e"].Value;
if (e1 != em1)
{
docExcel.Rows.Insert(Excel.XlInsertShiftDirection.xlShiftDown);
}
}
}
}
What I am trying to do here is each time there is a different value in "E" add 3 rows....so for example if Column E reads:
Joseph
Joseph
Brian
Brian
Brian
Brian
Brian
Mark
Mark
Mark
etc
Loading

Imtiyaz AnsariRank #1,031 · 1.9K reputationPosted Mar 18, 2013, 10:17 AM EDT
namespace add
Microsoft.Office.Interop.Excel.dll
all gridview data create Excel File ......
try
richard smithRank #8,268 · 225 reputationPosted Mar 18, 2013, 10:36 AM EDT
I am wanting to search cell E and each time there is a seperate value in cell E...add 3 spaces between them. So again it would look like this:
Jorge Pasada
Jorge Pasada
....Space 1....
....Space 2....
....Space 3....
George Jones
George Jones
....Space 1....
....Space 2....
....Space 3....
Mitch Smith
Mitch Smith
Sharon White50 reputationPosted Mar 18, 2013, 11:00 PM EDT
Do you want to insert whole rows or just cells of column E?
If whole rows, the following code may be a little helpful.
//Load excel
Workbook book = new Workbook();
book.LoadFromFile("test.xls");
Worksheet sheet = book.Worksheets[0];
//Insert first 3 rows after the second row.
sheet.InsertRow(3, 3);
//Because the first rows has been inserted, so insert the next 3 rows after the eighth row which is the original fifth row.
sheet.InsertRow(8, 3);
And the above code is based on a .NET Excel Library.
Hope Helpful.
richard smithRank #8,268 · 225 reputationPosted Mar 19, 2013, 7:25 AM EDT
Microsoft.Office.Interop.Excel.Worksheet doesnot contain a definition for 'InsertRow' and no extension method 'InsertRow' acceping a 1st argument of type 'Microsoft.Office.Interop.Excel.Worksheet' could be found
Sharon White50 reputationPosted Mar 19, 2013, 7:18 PM EDT
I am sorry for tell you that I don't use Microsoft.Office.Interop but another library to realize this function more more easily. So you cannot find InsertRow method if you use Microsoft.Office.Interop instead of my library dll.
richard smithRank #8,268 · 225 reputationPosted Mar 19, 2013, 7:38 PM EDT
(My chrome keeps telling me it is unable to open the link you provided in your initial post, if it would I am sure I would know what you are referencing.)
richard smithRank #8,268 · 225 reputationPosted Mar 20, 2013, 4:47 AM EDT