I need help to copy some data from cells in one excel workbook to another excel workbook. I am having trouble specifying the c# code to look for data in a specified column that isn't the first column. The excel worksheet contains data about employees details such as name, number, department and email. I want the code to search the column and for the data that matches a certain position e.g. trainee instead of permanent. The code should be able to copy the people who are trainees and paste in another specified workbook.
I have tried to implement an if statement and if the cell contains the string 'trainee' it will paste the data into another workbook, this was successful only when the column was the first, which in the actual spreadsheet it wasn't. I have searched all over the internet but can't find a conclusive tutorial on how to manipulate excel with c# in a console app.
using Excel = Microsoft.Office.Interop.Excel;
class Program
{
static void Main(string[] args)
{
var excelapp = new Excel.Application();
excelapp.Workbooks.Add();
string path =
@"C:\Users\....xlsx";
Excel.Workbook workbook = excelapp.Workbooks.Open(path);
Excel.Worksheet workSheet = workbook.Worksheets.get_Item(1);
var source =
workSheet.Range["h3:H10"].Insert(Excel.XlInsertShiftDirection.xlShiftDown);
Excel.Range dest = workSheet.Range["F10"];
workbook.SaveAs("Book1.xlsx");
}
}
This current code will only copy the entire spreadsheet and paste into a new workbook. I only want the relevant data.