Using the Microsoft Excel 12.0 Object Library (Microsoft.Office.Interop.Excel)
Application app = new Application();
Workbook wb = app.Workbooks.Open("test.xlsx");
Worksheet ws = wb.Sheets["MyTestSheet"];
Range rngSource = ws.UsedRange.Columns["A"];
Range rngTarget = ws.UsedRange.Columns["D"];
rngTarget.Value = rngSource.Value;
rngSource.Value = null;
wb.Save();
app.Application.Quit();
You can do the same with fewer lines of code, but for demo purposes, I wrote it like this. Keep in mind that the above code violates "Never use 2 points when accessing COM objects", which can cause problems with deleting COM objects and leave zombie Excel processes.
source
share