1

I am trying to write output to an Excel workbook using ExcelLibrary. I want this workbook to contain only one sheet but I can't find any way to either create a workbook with only one sheet or remove the extra sheets after creation. All of the examples I have found do not work.

Any advice is appreciated.

Regards.

Kevin
  • 1,252
  • 6
  • 32
  • 50

1 Answers1

1

The Worksheets are available to you via the Worksheets property, which is simply a generic list of Worksheet objects - i.e. List<Worksheet>.

You can treat this list like any generic List<T> and use Clear(), Remove(), etc. to manipulate the list of worksheets.

Workbook workbook = new Workbook();
workbook.Worksheets.Clear(); // remove all worksheets

Worksheet mySheet = new Worksheet();
workbook.Worksheets.Add(mySheet); // add a new worksheet
workbook.Worksheets.Remove(mySheet); // remove a particular worksheet
workbook.Worksheets.RemoveAt(4); // remove a worksheet at specific index
KP.
  • 13,218
  • 3
  • 40
  • 60
  • Thanks. I tried `workbook.Worksheets.Clear();` but I still end up with extra worksheets. Also, when I try to delete them by index I always throw an index out of bounds exception -- which is weird since it should be one or the other. – Kevin Jun 25 '12 at 20:38
  • My apologies. I was adding the worksheet in the wrong spot. Your example is perfectly correct. Thank you. – Kevin Jun 25 '12 at 20:46
  • @Kevin You're welcome. Glad to help. May I suggest you mark my answer as correct? Thanks. – KP. Jun 26 '12 at 13:48