/// <summary> /// Determine which of the resolved styles are either to define a time or a date. Stores also the styles into a dictionary /// </summary> /// <param name="styleReaderContainer">Resolved styles from the style reader</param> private void processStyles(StyleReaderContainer styleReaderContainer) { dateStyles = new List <string>(); timeStyles = new List <string>(); for (int i = 0; i < styleReaderContainer.StyleCount; i++) { bool isDate; bool isTime; styleReaderContainer.GetStyle(i, out isDate, out isTime, true); if (isDate) { dateStyles.Add(i.ToString("G", CultureInfo.InvariantCulture)); } if (isTime) { timeStyles.Add(i.ToString("G", CultureInfo.InvariantCulture)); } } }
/// <summary> /// Default constructor /// </summary> public StyleReader() { StyleReaderContainer = new StyleReaderContainer(); }
/// <summary> /// Reads the XLSX file from a file path or a file stream /// </summary> /// <exception cref="Exceptions.IOException"> /// Throws IOException in case of an error /// </exception> public void Read() { try { using (memoryStream = new MemoryStream()) { ZipArchive zf; if (inputStream == null && !string.IsNullOrEmpty(filePath)) { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { fs.CopyTo(memoryStream); } } else if (inputStream != null) { using (inputStream) { inputStream.CopyTo(memoryStream); } } else { throw new IOException("LoadException", "No valid stream or file path was provided to open"); } memoryStream.Position = 0; zf = new ZipArchive(memoryStream, ZipArchiveMode.Read); MemoryStream ms; SharedStringsReader sharedStrings = new SharedStringsReader(); ms = GetEntryStream("xl/sharedStrings.xml", zf); if (ms.Length > 0) // If length == 0, no shared strings are defined (no text in file) { sharedStrings.Read(ms); } StyleReader styleReader = new StyleReader(); ms = GetEntryStream("xl/styles.xml", zf); styleReader.Read(ms); styleReaderContainer = styleReader.StyleReaderContainer; workbook = new WorkbookReader(); ms = GetEntryStream("xl/workbook.xml", zf); workbook.Read(ms); int worksheetIndex = 1; string name; string nameTemplate; WorksheetReader wr; nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml"; name = "xl/worksheets/" + nameTemplate; foreach (KeyValuePair <int, string> definition in workbook.WorksheetDefinitions) { ms = GetEntryStream(name, zf); wr = new WorksheetReader(sharedStrings, nameTemplate, worksheetIndex, styleReaderContainer, importOptions); wr.Read(ms); worksheets.Add(definition.Key, wr); worksheetIndex++; nameTemplate = "sheet" + worksheetIndex.ToString(CultureInfo.InvariantCulture) + ".xml"; name = "xl/worksheets/" + nameTemplate; } } } catch (Exception ex) { throw new IOException("LoadException", "There was an error while reading an XLSX file. Please see the inner exception:", ex); } }
/// <summary> /// Constructor with parameters /// </summary> /// <param name="sharedStrings">SharedStringsReader object</param> /// <param name="name">Worksheet name</param> /// <param name="number">Worksheet number</param> /// <param name="styleReaderContainer">Resolved styles, used to determine dates or times</param> /// <param name="options">Import options to override the automatic approach of the reader. <see cref="ImportOptions"/> for information about import options.</param> public WorksheetReader(SharedStringsReader sharedStrings, string name, int number, StyleReaderContainer styleReaderContainer, ImportOptions options = null) { importOptions = options; Data = new Dictionary <string, Cell>(); Name = name; WorksheetNumber = number; this.sharedStrings = sharedStrings; processStyles(styleReaderContainer); }