/// <summary> /// Default constructor /// </summary> /// <param name="file">Stream with source data</param> public ExcelDataReader(Stream file) { m_file = file; // new BufferedStream(file); m_hdr = XlsHeader.ReadHeader(m_file); XlsRootDirectory dir = new XlsRootDirectory(m_hdr); XlsDirectoryEntry workbookEntry = dir.FindEntry("Workbook"); if (workbookEntry == null) workbookEntry = dir.FindEntry("Book"); if (workbookEntry == null) throw new FileNotFoundException("Oops! Neither stream 'Workbook' nor 'Book' was found in file"); if (workbookEntry.EntryType != STGTY.STGTY_STREAM) throw new FormatException("Oops! Workbook directory entry is not a Stream"); m_stream = new XlsBiffStream(m_hdr, workbookEntry.StreamFirstSector); ReadWorkbookGlobals(); GC.Collect(); m_workbookData = new DataSet(); for (int i = 0; i < m_sheets.Count; i++) if (ReadWorksheet(m_sheets[i])) m_workbookData.Tables.Add(m_sheets[i].Data); m_globals.SST = null; m_globals = null; m_sheets = null; m_stream = null; m_hdr = null; GC.Collect(); }
public XlsStream(XlsHeader hdr, uint startSector) { m_file = hdr.FileStream; m_fat = hdr.FAT; m_hdr = hdr; m_startSector = startSector; }
/// <summary> /// Creates Root Directory catalog from XlsHeader /// </summary> /// <param name="hdr">XlsHeader object</param> public XlsRootDirectory(XlsHeader hdr) { m_hdr = hdr; XlsStream stream = new XlsStream(hdr, hdr.RootDirectoryEntryStart); byte[] array = stream.ReadStream(); byte[] tmp; XlsDirectoryEntry entry; List<XlsDirectoryEntry> entries = new List<XlsDirectoryEntry>(); for (int i = 0; i < array.Length; i += XlsDirectoryEntry.Length) { tmp = new byte[XlsDirectoryEntry.Length]; Buffer.BlockCopy(array, i, tmp, 0, tmp.Length); entries.Add(new XlsDirectoryEntry(tmp)); } m_entries = entries; for (int i = 0; i < entries.Count; i++) { entry = entries[i]; if (m_root == null && entry.EntryType == STGTY.STGTY_ROOT) m_root = entry; if (entry.ChildSid != (uint)FATMARKERS.FAT_FreeSpace) entry.Child = entries[(int)entry.ChildSid]; if (entry.LeftSiblingSid != (uint)FATMARKERS.FAT_FreeSpace) entry.LeftSibling = entries[(int)entry.LeftSiblingSid]; if (entry.RightSiblingSid != (uint)FATMARKERS.FAT_FreeSpace) entry.RightSibling = entries[(int)entry.RightSiblingSid]; } }
/// <summary> /// Reads Excel header from Stream /// </summary> /// <param name="file">Stream with Excel file</param> /// <returns>XlsHeader representing specified file</returns> public static XlsHeader ReadHeader(Stream file) { XlsHeader hdr = new XlsHeader(file); lock (file) { file.Seek(0, SeekOrigin.Begin); file.Read(hdr.m_bytes, 0, 512); } if (!hdr.IsSignatureValid) throw new InvalidHeaderException("Invalid file signature"); if (hdr.ByteOrder != 0xFFFE) throw new FormatException("Invalid byte order specified"); return hdr; }
/// <summary> /// Constructs FAT table from list of sectors /// </summary> /// <param name="hdr">XlsHeader</param> /// <param name="sectors">Sectors list</param> public XlsFat(XlsHeader hdr, List<uint> sectors) { m_hdr = hdr; m_sectors_for_fat = sectors.Count; uint sector = 0, prevSector = 0; int sectorSize = hdr.SectorSize; byte[] buff = new byte[sectorSize]; Stream file = hdr.FileStream; using (MemoryStream ms = new MemoryStream(sectorSize * m_sectors_for_fat)) { lock (file) { for (int i = 0; i < sectors.Count; i++) { sector = sectors[i]; if (prevSector == 0 || (sector - prevSector) != 1) file.Seek((sector + 1) * sectorSize, SeekOrigin.Begin); prevSector = sector; file.Read(buff, 0, sectorSize); ms.Write(buff, 0, sectorSize); } } ms.Seek(0, SeekOrigin.Begin); BinaryReader rd = new BinaryReader(ms); m_sectors = (int)ms.Length / 4; m_fat = new List<uint>(m_sectors); for (int i = 0; i < m_sectors; i++) m_fat.Add(rd.ReadUInt32()); rd.Close(); ms.Close(); } }
public XlsBiffStream(XlsHeader hdr, uint streamStart) : base(hdr, streamStart) { bytes = base.ReadStream(); m_size = bytes.Length; m_offset = 0; }