Home » excel » c# – Adding a new column at the start of an excel table in an excel

c# – Adding a new column at the start of an excel table in an excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am wondering how one can insert a new column at the beginning of an excel spreadsheet using C sharp.net. I am using Microsoft.Office.Interop.Excel; I want to use OLEO to do this, but I don’t see that happening. I have been searching google on how to do this for the last two days now. I can’t understand why there are not more tutorials on this?

How to&Answers:

Checkout out the Range.Insert method which you can call on a range of cells to insert entire rows/columns in front of them:

Worksheet sheet = (Worksheet) workBookIn.Sheets[1]; // Worksheet indexes are one based

Range rng = sheet.get_Range("A1", Missing.Value);

rng.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight,
                        XlInsertFormatOrigin.xlFormatFromRightOrBelow);