The code is this
Excel.Application appC = new Excel.Application();
appC.Visible = true;
Excel.Workbook bookC = appC.Workbooks.Add(1);
Excel.Worksheet sheetC = bookC.Worksheets.Add();
sheetC.Name = "something";
The command Workbook.Add()
takes one parameter that is supposed to determine how many sheets will be created in the workbook... right?
So why do I get 2 sheets... one named "something" and one named "sheet 2"? What am I doing wrong??
This is the code to create an Excel application object and open a workbook with only ONE sheet and name it as you wish:
I faced the same problem. You need to add a sheet like this:
The parameter to Workbooks.Add does NOT specify the number of sheets.
See the MSDN description of the Add method.
You should probably use the constant xlWBATWorksheet rather than just "1".
[I'm not at Work and don't have Excel handy; it may be that the value of that constant is actually 1, in which case this will make no (functional) difference. The alternative is to set the SheetsInNewWorkbook property before creating the workbook, or simply deleting the unwanted sheets after creating the workbook.]
if you are using vs 2010 it is diffrent you can use the below code to add a work sheet to a work book this i have tried this in VS 2010 this works for me im using excel 2007 work book project template
This code is directly extracted from my projecte please ammed the code as needed hope this will help to solve your problem
thanks