Reputation: 46222
I currently have the following code to add a blank worksheet to a workbook using ClosedXML. Wondering if there is an easier way without specifying the name of the worksheet as in "Sheet1" even though "Sheet1" is named that way by default when you open Excel.
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add("Sheet1");
MemoryStream fs = new MemoryStream();
wb.SaveAs(fs);
fs.Position = 0;
return fs;
}
Upvotes: 0
Views: 2891
Reputation: 1126
Since you are creating a new workbook, I would use:
wb.Worksheets.Add("Sheet" + wb.Worksheets.Count+1);
This way no matter what the user's default setting for the number of initial worksheets is, you will always be adding a uniquely named worksheet. If you were accessing an existing workbook, you would have to check for duplicate worksheet names, but with a new workbook, your only concern is the default number of worksheets the user has chosen.
Upvotes: 1