/// <summary> /// Duplicate worksheet and insert the new instance into specified position /// </summary> /// <param name="sheet">worksheet to be duplicated. The worksheet passed here should be /// already added into current workbook.</param> /// <param name="newIndex">position used to insert duplicated new instance</param> /// <param name="newName">New name to be apply</param> /// <returns>instance of duplicated worksheet from specified worksheet</returns> /// <exception cref="WorksheetNotFoundException">when specified worksheet does not belong to /// this workbook.</exception> /// <exception cref="ArgumentOutOfRangeException">when the position used to insert /// duplicated instace of worksheet is out of valid range of this workbook.</exception> public Worksheet CopyWorksheet(Worksheet sheet, int newIndex, string newName = null) { if (sheet.workbook != this) { throw new WorksheetNotFoundException("Specified worksheet does not belong to this workbook."); } if (newIndex < 0 || newIndex > this.worksheets.Count) { throw new ArgumentOutOfRangeException("newIndex"); } var newSheet = sheet.Clone(newName); if (this.WorksheetCreated != null) { this.WorksheetCreated(this, new WorksheetCreatedEventArgs(newSheet)); } InsertWorksheet(newIndex, newSheet); return(newSheet); }