Copy first row in excel workbook to a new excel workbook
Posted
by
user1667414
on Stack Overflow
See other posts from Stack Overflow
or by user1667414
Published on 2012-09-13T03:26:14Z
Indexed on
2012/09/13
3:38 UTC
Read the original article
Hit count: 203
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);
© Stack Overflow or respective owner