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
richard smithPosted Mar 20, 2013, 8:47 AM
richard smithPosted Mar 19, 2013, 11:38 PM
(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.)
Sharon WhitePosted Mar 19, 2013, 11:18 PM
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 smithPosted Mar 19, 2013, 11:25 AM
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 WhitePosted Mar 19, 2013, 3:00 AM
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 smithPosted Mar 18, 2013, 2:36 PM
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
Imtiyaz AnsariPosted Mar 18, 2013, 2:17 PM
namespace add
Microsoft.Office.Interop.Excel.dll
all gridview data create Excel File ......
try