namespace com.azkoss.excellite { using System; using System.Collections; using System.Reflection; /// ///Collection of worksheets (ExcelWorksheet). /// /// public sealed class ExcelWorksheetCollection : IEnumerable { // Methods internal ExcelWorksheetCollection(ExcelFile parent) { this.sheetIndexes = new ArrayList(); this.parent = parent; this.worksheetArray = new ArrayList(); } /// ///Adds an empty worksheet to the end of the collection. /// ///Worksheet name. ///Newly created worksheet. /// ///If this is the first worksheet added to the collection the ///ActiveWorksheet is set to this worksheet. /// ///Thrown if worksheet name is not unique. public ExcelWorksheet Add(string worksheetName) { return this.InsertInternal(worksheetName, this.worksheetArray.Count); } /// ///Adds a copy of an existing worksheet to the end of the collection. /// ///Name of new worksheet. ///Source worksheet. ///Newly created worksheet. /// ///If this is the first worksheet added to the collection the ///ActiveWorksheet is set to this worksheet. /// ///Thrown if worksheet name is not unique. public ExcelWorksheet AddCopy(string destinationWorksheetName, ExcelWorksheet sourceWorksheet) { return this.InsertCopyInternal(destinationWorksheetName, this.worksheetArray.Count, sourceWorksheet); } internal ushort AddSheetReference(string sheet) { ushort num1 = this.GetSheetIndex(sheet); if (!this.sheetIndexes.Contains(num1)) { this.sheetIndexes.Add(num1); return (ushort) (this.sheetIndexes.Count - 1); } for (int num2 = 0; num2 < this.sheetIndexes.Count; num2++) { ushort num3 = (ushort) this.sheetIndexes[num2]; if (num1 == num3) { return (ushort) num2; } } return 0; } internal void DeleteInternal(ExcelWorksheet ws) { if (this.activeWorksheet == ws) { this.activeWorksheet = null; } ushort num1 = this.GetSheetIndex(ws.Name); for (int num2 = 0; num2 < this.sheetIndexes.Count; num2++) { ushort num3 = (ushort) this.sheetIndexes[num2]; if (num3 > num1) { this.sheetIndexes[num2] = (ushort) (num3 - 1); } } this.worksheetArray.Remove(ws); } internal void ExceptionIfNotUnique(string worksheetName) { foreach (ExcelWorksheet worksheet1 in this.worksheetArray) { if (worksheet1.Name == worksheetName) { throw new ArgumentException("Provided worksheet name is not unique.", "worksheetName"); } } } internal int GetActiveWorksheetIndex() { if (this.Count == 0) { throw new Exception("Workbook must contain at least one worksheet. Use ExcelFile.Worksheets.Add() method to create a new worksheet."); } for (int num1 = 0; num1 < this.Count; num1++) { if (this[num1] == this.activeWorksheet) { return num1; } } throw new Exception("Internal: Can't find ActiveWorksheet."); } /// ///Returns an enumerator for the ///ExcelWorksheetCollection. /// public IEnumerator GetEnumerator() { return this.worksheetArray.GetEnumerator(); } internal ushort GetSheetIndex(string sheet) { ushort num1 = 0; foreach (ExcelWorksheet worksheet1 in this.worksheetArray) { if (worksheet1.Name == sheet) { return num1; } num1 = (ushort) (num1 + 1); } return 0; } internal int IndexOf(ExcelWorksheet ws) { return this.worksheetArray.IndexOf(ws); } internal ExcelWorksheet InsertCopyInternal(string destinationWorksheetName, int position, ExcelWorksheet sourceWorksheet) { this.ExceptionIfNotUnique(destinationWorksheetName); ExcelWorksheet worksheet1 = new ExcelWorksheet(destinationWorksheetName, this, sourceWorksheet); this.worksheetArray.Insert(position, worksheet1); if (sourceWorksheet.ParentExcelFile != worksheet1.ParentExcelFile) { worksheet1.ParentExcelFile.CopyDrawings(sourceWorksheet.ParentExcelFile); } return worksheet1; } internal ExcelWorksheet InsertInternal(string worksheetName, int position) { this.ExceptionIfNotUnique(worksheetName); ExcelWorksheet worksheet1 = new ExcelWorksheet(worksheetName, this); this.worksheetArray.Insert(position, worksheet1); return worksheet1; } // Properties /// ///Gets or sets active worksheet. /// /// ///Active worksheet is the one selected when file is opened with Microsoft Excel. By default active worksheet ///is the first one added with Add method. /// public ExcelWorksheet ActiveWorksheet { get { if ((this.activeWorksheet == null) && (this.worksheetArray.Count > 0)) { this.activeWorksheet = this[0]; } return this.activeWorksheet; } set { this.activeWorksheet = value; if (this.GetActiveWorksheetIndex() >= 5) { this.activeWorksheet = this[0]; } } } /// ///Gets the number of elements contained in the ///ExcelWorksheetCollection. /// public int Count { get { return this.worksheetArray.Count; } } /// ///Gets the worksheet with the specified name. /// ///The name of the worksheet. public ExcelWorksheet this[string name] { get { foreach (ExcelWorksheet worksheet1 in this.worksheetArray) { if (worksheet1.Name == name) { return worksheet1; } } throw new ArgumentOutOfRangeException("name", name, "No worksheet with specified name."); } } ///Gets the worksheet with the specified index or name. /// ///Gets the worksheet with the specified index. /// ///The zero-based index of the worksheet. public ExcelWorksheet this[int index] { get { return (ExcelWorksheet) this.worksheetArray[index]; } } internal ExcelFile Parent { get { return this.parent; } } /// ///Gets the sheet indexes. /// ///The sheet indexes. internal ushort[] SheetIndexes { get { return (ushort[]) this.sheetIndexes.ToArray(typeof(ushort)); } } /// ///Gets the sheet names. /// ///The sheet names. internal string[] SheetNames { get { string[] textArray1 = new string[this.worksheetArray.Count]; int num1 = 0; foreach (ExcelWorksheet worksheet1 in this) { textArray1[num1++] = worksheet1.Name; } return textArray1; } } // Fields private ExcelWorksheet activeWorksheet; private ExcelFile parent; private ArrayList sheetIndexes; private ArrayList worksheetArray; } }