Copy first row in excel workbook to a new excel workbook
- by user1667414
How do I get the first row in an excel workbook & save it to a new excel workbook using .net c#? I dont know the amount of columns so need to get entire row. This what I have but the new workbook is blank (no row copied)
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file);
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRangeHeader = xlWorksheet.get_Range("A1", "A1").EntireRow;
Excel.Workbook xlWorkbookNew = xlApp.Workbooks.Add();
Excel._Worksheet xlWorksheetNew = xlWorkbookNew.Sheets[1];
xlWorksheetNew.get_Range("A1", "A1").EntireRow.Value = xlRangeHeader;
xlWorkbook.Close(false);
xlWorkbookNew.SaveAs(Path.Combine(sDestination, Path.GetFileName(file)), fileFormat);
xlWorkbookNew.Close(true);