We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Not able to delete all worksheet

Thread ID:

Created:

Updated:

Platform:

Replies:

99432 May 20,2011 12:43 AM UTC May 20,2011 03:57 AM UTC ASP.NET Web Forms (Classic) 1
loading
Tags: XlsIO
shrimpy
Asked On May 20, 2011 12:43 AM UTC

Hi All,

I am try to create a empty workbook with only one worksheet.

what i did is:

for (int i = 0; i < this._workBook.Worksheets.Count; i++)
{
this._workBook.Worksheets.Remove(i);
}


this._workSheet = this._workBook.Worksheets.Create("My Demo Worksheet");
this._workSheet.Activate();

when i save the file, why there will be two worksheets?

Thanks



Lokesh R [Syncfusion]
Replied On May 20, 2011 03:57 AM UTC

Hi Shrimpy,

Thank you very much for your interest in Syncfusion products.

As you have reported in your last update the code snippet will result with two worksheets in the output file. Because the code will remove from the starting index 0, so when the 0th index is removed, sheet in the 1st index will be moved to the 0th index but the code will remove from the 1st index [But actually you are removing the 2nd index sheet ]. I hope the input workbook has 5 sheets the code will remove in the below way

0 1 2 3 4
0 1 2 3 [When i==0]
0 1 2 [When i==1]
0 1 [When i==2]
I have modified the code snippet and placed below, please try the below code snippet at your side and let me know if this helps you.

[C#]
// Instantiate the spreadsheet creation engine.
ExcelEngine excelEngine = new ExcelEngine();

// Instantiate the Excel application object.
IApplication application = excelEngine.Excel;

string file = @"..\..\Book1.xlsx";

//Open the workbook
IWorkbook workbook = application.Workbooks.Open(file, ExcelOpenType.Automatic);

//Delete worksheets from workbook
for (int i = workbook.Worksheets.Count - 1; i > 0; i--)
workbook.Worksheets.Remove(i);

//Save the workbook
workbook.SaveAs("SampleOutput_NoSheetIndex.xlsx");

Please let me know if you need any clarifications.

Thanks,
Lokesh


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;