Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,
I have a workbook having N number of sheets, each sheet is having a template file. Now i need to create a new workbook and copy sheets from existing workbook to newly created one using c#.How can i do that?I am not at all familiar with programming with excel sheets, so please help me sir?
Posted

For programming with EXCEL, you need to add reference of "Microsoft.office.interop.excel" from com tab.
use this[^] link for coding reference
 
Share this answer
 
Hello.

In this topic[^] please look at code in first answer. It doesn't solves your problem, but using there, you can solve it by youreself.

I think, your code shoild be like that:

C#
xlWorkbookFrom = xlApp.Workbooks.Open(fullFilenameFrom, 0, false, 5, "", "",
                    false, XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);
xlWorkbookTo = xlApp.Workbooks.Open(fullFilenameTo, 0, false, 5, "", "",
                    false, XlPlatform.xlWindows, "",
                    true, false, 0, true, false, false);


xlSheetsFrom = xlWorkbookFrom.Sheets as Sheets;
xlSheetsTo = xlWorkbookTo.Sheets as Sheets;

foreach(var sheet in xlSheetsFrom)
{ 
    xlNewSheet = (Worksheet)xlSheetsTo.Add(sheet , Type.Missing, Type.Missing,    Type.Missing);
    xlNewSheet.Name = sheet.Name;
}
xlWorkbookTo .Save();
xlWorkbookTo .Close(Type.Missing,Type.Missing,Type.Missing);
...//etc


But I really don't sure, that thiswill work, I never try.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900