/// <summary> /// Initializes a new instance of the <b>ExcelParsedFile</b> class. /// </summary> /// <param name="filename"></param> /// <param name="sheetNumber"></param> /// <param name="allowEmpty"></param> /// <exception cref="DuplicateNameException">If multiple columns found with name \.</exception> /// <exception cref="InvalidDataException">If no columns was found in the specified file. The first row of the file must contain column names! /// or if no content was found in the specified file.</exception> private ExcelParsedFile(string filename, int sheetNumber, bool allowEmpty) { IWorkbook b = Factory.GetWorkbook(filename); IWorksheet sheet = b.Worksheets[sheetNumber]; _sheetName = sheet.Name; int r = 0; int c = 0; _columnNames = new List <string>(); while (!string.IsNullOrEmpty(sheet.Cells[r, c].Text)) { string key = sheet.Cells[r, c].Text.ToLower(); if (ordinalHash.ContainsKey(key)) { throw new DuplicateNameException("Multiple columns found with name \"" + sheet.Cells[r, c].Text + "\""); } ordinalHash.Add(key, c); _columnNames.Add(sheet.Cells[r, c].Text); c++; } ColumnCount = _columnNames.Count; if (ColumnCount == 0 && !allowEmpty) { throw new InvalidDataException("No columns was found in the specified file. The first row of the file must contain column names!"); } _content = new List <FileRow>(); if (ColumnCount > 0) { r++; while (!IsRowEmpty(sheet, r, ColumnCount)) { FileRow data = new FileRow(this, r + 1); // r + 1, because this property is used to display the number of row in Excel, not the index _content.Add(data); for (int i = 0; i < ColumnCount; i++) { /// 2009-03-18, Jakob Adolfsson /// Changed from sheet.Cells[ r, i ].Text to sheet.Cells[ r, i ].Entry, since the Text-property only returns the value displayed in Excel, not the actual value entered. /// The downside of this is that a formula will not be evaluated, but will be returned as the entered string value. /// /// 2009-06-08, Jakob Adolfsson /// Added a check if the entry starts with an equals sign. /// If so, the Text-property is read to get the evaluated forumula string entry = sheet.Cells[r, i].Entry; if (!string.IsNullOrEmpty(entry) && entry.StartsWith("=")) { entry = sheet.Cells[r, i].Text; } data.Add(entry); } r++; } } RowCount = _content.Count; if (RowCount == 0 && !allowEmpty) { throw new InvalidDataException("No content was found in the specified file."); } }