Create new workbook with no worksheet(s)

Hi,

I want to create a new XL workbook which won't be having any worksheets in it initially. My program is taking care of adding a new worksheet every time which creates a XL file with all my sheets along with 1 blank sheet (Sheet1) by default.

Like in XL programming I can specify worksheet count while creating a new workbook.
For e.g.:

dim objExcel As New Excel.Application

With objExcel
.DisplayAlerts = False
.SheetsInNewWorkbook = 1
End With
Is there any similar way in ASPOSE.Cells for .NET?

Hi,


By default, when you create a new Workbook, Aspose.Cells for .NET also creates one blank worksheet i.e. “Sheet1” in it similar to MS Excel. And, you may add your desired worksheets in the workbook as per your requirements, see the document for reference:
http://docs.aspose.com/display/cellsnet/Managing+Worksheets


Moreover, if you do not use or set valid license, Aspose.Cells also creates an evaluation watermark sheet as well. See the licensing topic:
http://docs.aspose.com/display/cellsnet/Licensing

Also see the sample code below:

Sample code:

C#
//Instantiating a Workbook object
Workbook workbook = new Workbook();

//Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];

//Setting the name of the newly added worksheet
worksheet.Name = “My Worksheet”;

//Saving the Excel file
workbook.Save("e:\\test2\\MyBook.xlsx")//This file contains: "Sheet1" and "My Worksheet" worksheets etc.


VB.NET
'Instantiating a Workbook object

Dim workbook As Workbook = New Workbook()

'Adding a new worksheet to the Workbook object
Dim i As Integer = workbook.Worksheets.Add()

'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(i)

'Setting the name of the newly added worksheet
worksheet.Name = “My Worksheet”

‘Saving the Excel file
workbook.Save(“e:\test2\MyBook.xlsx”)’This file contains: “Sheet1” and “My Worksheet” worksheets etc.

By default, when you create a new Workbook, Aspose.Cells for .NET also creates one blank worksheet i.e. "Sheet1" in it similar to MS Excel

If I don't want this "Sheet1" then? What I mean to say is I only want a workbook with no Sheets in it. Is it possible?

I'm applying proper license, so that's not the issue.

Hi,


If I don’t want this “Sheet1” then? What I mean to say is I only want a workbook with no Sheets in it. Is it possible?”.

How could you do this in MS Excel? I think this is not possible. A new workbook must contain one sheet. I think for your need you may create new Workbook and add your worksheets in it, then you may remove the first worksheet from the workbook (using Aspose.Cells APIs, see the topic/link which I suggested in my previous reply).

Thank you.

Yes, I can do with MS Excel.

dim objExcel As New Excel.Application

With objExcel
.DisplayAlerts = False
.SheetsInNewWorkbook = 0
End With
This way I can specify I don't want any Sheet in new workbook.

Hi,


Please create an Excel file without a single worksheet in it in MS Excel and post the file here, we will check it soon.

Thank you.

Hi Prophetix,


If you have any concerns that you wish to share with us then please create a new thread and provide as much details as you can. We will evaluate your presented scenario to assist you further in that regard.

Amjad, your post illustrates you do not understand the question.


While it may not be possible to save a workbook without a worksheet, it should be possible to create a workbook without any sheets programmatically and then add the sheets desired.

It is surprising that an unfortunate choice like this was made in the Aspose.Cells API and that the VSTO interface actually supports a richer workbook creation API in this case.

Could you explain why a default worksheet is added? Perhaps you have some limitation in your code that requires such a hack to be made?

Thanks…

Hi Owen,

Please note, the current implementation of Aspose.Cells APIs follow Microsoft’s Excel guidelines & specifications (not of VSTO) therefore what is not supported by Excel is not supported with Aspose.Cells APIs either. Regarding your concerns, if you create a blank spreadsheet with Excel application, it adds an empty worksheet, and if you try to remove the only worksheet from the spreadsheet (even before the save operation) Excel pops-up an error as shown in attached snapshot and does not allow you to delete the only worksheet unless you add more worksheets to the spreadsheet. Same is the case with Aspose.Cells’s created workbook. The API will throw exception if the code tries to delete the only worksheet from the workbook regardless of the save operation.

If you can explain why you need a workbook with no worksheets in it, we may be able to provide a workaround for the situation.

Hi Owen,


Adding more to Babar’s comments, when you open a new Blank Workbook in MS Excel manually, it adds three worksheets i.e., Sheet1, Sheet2 and Sheet3 to it automatically (by default). Similarly when you instantiate a new Workbook via Aspose.Cells APIs:
i.e., using the line of code,
Workbook workbook = new Workbook().
it adds Sheet1 to the workbook by default to overcome the error or other consequences.

Well, you may simply remove existing worksheet(s) by adding the following line of code and before adding your desired worksheets in the workbook:
e.g
Sample code:

workbook.Worksheets.Clear();


Let us know if I can be of any further help.

Thank you.