/// <summary> /// Safely rename the worksheet with the given name. If the sheet already exists a number will be added after the name. /// </summary> public static void Rename(this _Worksheet sheet, string name, int number = 0) { try { // Try to rename the sheet with the new name. If the number is not 0, add it to the sheet name. // The number is automatically incremented if a sheet with a similar name already exists. sheet.Name = number == 0 ? name : name + " " + number; } catch (COMException) { // If renaming the sheet failed because of a duplicate name, try again with an incremented number. sheet.Rename(name, ++number); } }
/// <summary> /// Print the categories to the <see cref="_Worksheet"/>. /// </summary> /// <param name="title">The category title.</param> private void PrintCategories(string title) { // Create a new sheet for the chart. if (_sheet == null) { _sheet = WorksheetHelper.NewWorksheet(title); } else { _sheet.Rename(Regex.Replace(_sheet.Name, @"[^A-Za-z]+", "") + " & " + title); } // Write the title to the sheet. _sheet.Cells[_row++, _column] = title; // Write the categories to she sheet. foreach (var valuesArray in _valuesArray) { _sheet.Cells[_row++, _column] = valuesArray.Name; } }