public XlsStream(XlsHeader hdr, uint startSector) { m_fileStream = hdr.FileStream; m_fat = hdr.FAT; m_hdr = hdr; m_startSector = startSector; }
/// <summary> /// Constructor /// </summary> /// <param name="bytes">byte array representing current object</param> /// <param name="header"></param> public XlsDirectoryEntry(byte[] bytes, XlsHeader header) { if (bytes.Length < Length) throw new Excel.Exceptions.BiffRecordException(Errors.ErrorDirectoryEntryArray); m_bytes = bytes; m_header = header; }
public XlsBiffStream(XlsHeader hdr, uint streamStart, bool isMini, XlsRootDirectory rootDir) : base(hdr, streamStart, isMini, rootDir) { bytes = base.ReadStream(); m_size = bytes.Length; m_offset = 0; }
/// <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; }
/// <summary> /// Creates Root Directory catalog from XlsHeader /// </summary> /// <param name="hdr">XlsHeader object</param> public XlsRootDirectory(XlsHeader 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> /// Creates Root Directory catalog from XlsHeader /// </summary> /// <param name="hdr">XlsHeader object</param> public XlsRootDirectory(XlsHeader hdr) { XlsStream stream = new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null); 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, hdr)); } m_entries = entries; for (int i = 0; i < entries.Count; i++) { entry = entries[i]; //Console.WriteLine("Directory Entry:{0} type:{1}, firstsector:{2}, streamSize:{3}, isEntryMiniStream:{4}", entry.EntryName, entry.EntryType.ToString(), entry.StreamFirstSector, entry.StreamSize, entry.IsEntryMiniStream); 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]; } stream.CalculateMiniFat(this); }
public XlsDirectoryEntry(byte[] bytes, XlsHeader header) { if (bytes.Length < 0x80) { throw new BiffRecordException("Directory Entry error: Array is too small."); } this.m_bytes = bytes; this.m_header = header; }
/// <summary> /// Constructor /// </summary> /// <param name="bytes">byte array representing current object</param> /// <param name="header"></param> public XlsDirectoryEntry(byte[] bytes, XlsHeader header) { if (bytes.Length < Length) { throw new Excel.Exceptions.BiffRecordException(Errors.ErrorDirectoryEntryArray); } m_bytes = bytes; m_header = header; }
public XlsStream(XlsHeader hdr, uint startSector, bool isMini, XlsRootDirectory rootDir) { m_fileStream = hdr.FileStream; m_fat = hdr.FAT; m_hdr = hdr; m_startSector = startSector; m_isMini = isMini; m_rootDir = rootDir; CalculateMiniFat(rootDir); }
/// <summary> /// Constructs FAT table from list of sectors /// </summary> /// <param name="hdr">XlsHeader</param> /// <param name="sectors">Sectors list</param> /// <param name="sizeOfSector"></param> /// <param name="isMini"></param> /// <param name="rootDir"></param> public XlsFat(XlsHeader hdr, List <uint> sectors, int sizeOfSector, bool isMini, XlsRootDirectory rootDir) { m_isMini = isMini; m_rootDir = rootDir; m_hdr = hdr; m_sectors_for_fat = sectors.Count; sizeOfSector = hdr.SectorSize; uint sector = 0, prevSector = 0; //calc offset of stream . If mini stream then find mini stream container stream //long offset = 0; //if (rootDir != null) // offset = isMini ? (hdr.MiniFatFirstSector + 1) * hdr.SectorSize : 0; byte[] buff = new byte[sizeOfSector]; Stream file = hdr.FileStream; using (MemoryStream ms = new MemoryStream(sizeOfSector * 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) * sizeOfSector, SeekOrigin.Begin); } prevSector = sector; file.Read(buff, 0, sizeOfSector); ms.Write(buff, 0, sizeOfSector); } } 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 static XlsHeader ReadHeader(Stream file) { XlsHeader header = new XlsHeader(file); lock (file) { file.Seek(0L, SeekOrigin.Begin); file.Read(header.m_bytes, 0, 0x200); } if (!header.IsSignatureValid) { throw new HeaderException("Error: Invalid file signature."); } if (header.ByteOrder != 0xfffe) { throw new FormatException("Error: Invalid byte order specified in header."); } return(header); }
/// <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 HeaderException(Errors.ErrorHeaderSignature); } if (hdr.ByteOrder != 0xFFFE) { throw new FormatException(Errors.ErrorHeaderOrder); } return(hdr); }
public XlsFat(XlsHeader hdr, List <uint> sectors, int sizeOfSector, bool isMini, XlsRootDirectory rootDir) { this.m_isMini = isMini; this.m_rootDir = rootDir; this.m_hdr = hdr; this.m_sectors_for_fat = sectors.Count; sizeOfSector = hdr.SectorSize; uint num = 0; uint num2 = 0; byte[] buffer = new byte[sizeOfSector]; Stream fileStream = hdr.FileStream; using (MemoryStream stream2 = new MemoryStream(sizeOfSector * this.m_sectors_for_fat)) { lock (fileStream) { for (int j = 0; j < sectors.Count; j++) { num = sectors[j]; if ((num2 == 0) || ((num - num2) != 1)) { fileStream.Seek((num + 1) * sizeOfSector, SeekOrigin.Begin); } num2 = num; fileStream.Read(buffer, 0, sizeOfSector); stream2.Write(buffer, 0, sizeOfSector); } } stream2.Seek(0L, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(stream2); this.m_sectors = ((int)stream2.Length) / 4; this.m_fat = new List <uint>(this.m_sectors); for (int i = 0; i < this.m_sectors; i++) { this.m_fat.Add(reader.ReadUInt32()); } reader.Close(); stream2.Close(); } }
/// <summary> /// Creates Root Directory catalog from XlsHeader /// </summary> /// <param name="hdr">XlsHeader object</param> public XlsRootDirectory(XlsHeader hdr) { XlsStream stream = new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null); 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, hdr)); } m_entries = entries; for (int i = 0; i < entries.Count; i++) { entry = entries[i]; //Console.WriteLine("Directory Entry:{0} type:{1}, firstsector:{2}, streamSize:{3}, isEntryMiniStream:{4}", entry.EntryName, entry.EntryType.ToString(), entry.StreamFirstSector, entry.StreamSize, entry.IsEntryMiniStream); 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]; } } stream.CalculateMiniFat(this); }
/// <summary> /// Constructs FAT table from list of sectors /// </summary> /// <param name="hdr">XlsHeader</param> /// <param name="sectors">Sectors list</param> /// <param name="sizeOfSector"></param> /// <param name="isMini"></param> /// <param name="rootDir"></param> public XlsFat(XlsHeader hdr, List<uint> sectors, int sizeOfSector, bool isMini, XlsRootDirectory rootDir) { m_isMini = isMini; m_rootDir = rootDir; m_hdr = hdr; m_sectors_for_fat = sectors.Count; sizeOfSector = hdr.SectorSize; uint sector = 0, prevSector = 0; //calc offset of stream . If mini stream then find mini stream container stream //long offset = 0; //if (rootDir != null) // offset = isMini ? (hdr.MiniFatFirstSector + 1) * hdr.SectorSize : 0; byte[] buff = new byte[sizeOfSector]; Stream file = hdr.FileStream; using (MemoryStream ms = new MemoryStream(sizeOfSector * 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) * sizeOfSector, SeekOrigin.Begin); prevSector = sector; file.Read(buff, 0, sizeOfSector); ms.Write(buff, 0, sizeOfSector); } } 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(); } }
private void Dispose(bool disposing) { // Check to see if Dispose has already been called. if (!this.disposed) { if (disposing) { if (m_workbookData != null) m_workbookData.Dispose(); if (m_sheets != null) m_sheets.Clear(); } m_workbookData = null; m_sheets = null; m_stream = null; m_globals = null; m_encoding = null; m_hdr = null; disposed = true; } }
/// <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(); } }
/// <summary> /// Creates Root Directory catalog from XlsHeader /// </summary> /// <param name="hdr">XlsHeader object</param> public XlsRootDirectory(XlsHeader hdr) { XlsStream stream = new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null); 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, hdr)); } 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]; } } stream.CalculateMiniFat(this); }
public XlsRootDirectory(XlsHeader hdr) { XlsStream stream = new XlsStream(hdr, hdr.RootDirectoryEntryStart, false, null); byte[] src = stream.ReadStream(); List <XlsDirectoryEntry> list = new List <XlsDirectoryEntry>(); for (int i = 0; i < src.Length; i += 0x80) { byte[] dst = new byte[0x80]; Buffer.BlockCopy(src, i, dst, 0, dst.Length); list.Add(new XlsDirectoryEntry(dst, hdr)); } this.m_entries = list; for (int j = 0; j < list.Count; j++) { XlsDirectoryEntry entry = list[j]; if ((this.m_root == null) && (entry.EntryType == STGTY.STGTY_ROOT)) { this.m_root = entry; } if (entry.ChildSid != uint.MaxValue) { entry.Child = list[(int)entry.ChildSid]; } if (entry.LeftSiblingSid != uint.MaxValue) { entry.LeftSibling = list[(int)entry.LeftSiblingSid]; } if (entry.RightSiblingSid != uint.MaxValue) { entry.RightSibling = list[(int)entry.RightSiblingSid]; } } stream.CalculateMiniFat(this); }
private void fail(string message) { m_exceptionMessage = message; m_isValid = false; m_file.Close(); m_isClosed = true; m_workbookData = null; m_sheets = null; m_stream = null; m_globals = null; m_encoding = null; m_hdr = null; }
private void readWorkBookGlobals() { //Read Header try { m_hdr = XlsHeader.ReadHeader(m_file); } catch (Exceptions.HeaderException ex) { fail(ex.Message); return; } catch (FormatException ex) { fail(ex.Message); return; } XlsRootDirectory dir = new XlsRootDirectory(m_hdr); XlsDirectoryEntry workbookEntry = dir.FindEntry(WORKBOOK) ?? dir.FindEntry(BOOK); if (workbookEntry == null) { fail(Errors.ErrorStreamWorkbookNotFound); return; } if (workbookEntry.EntryType != STGTY.STGTY_STREAM) { fail(Errors.ErrorWorkbookIsNotStream); return; } m_stream = new XlsBiffStream(m_hdr, workbookEntry.StreamFirstSector); m_globals = new XlsWorkbookGlobals(); m_stream.Seek(0, SeekOrigin.Begin); XlsBiffRecord rec = m_stream.Read(); XlsBiffBOF bof = rec as XlsBiffBOF; if (bof == null || bof.Type != BIFFTYPE.WorkbookGlobals) { fail(Errors.ErrorWorkbookGlobalsInvalidData); return; } bool sst = false; m_version = bof.Version; m_sheets = new List<XlsWorksheet>(); while (null != (rec = m_stream.Read())) { switch (rec.ID) { case BIFFRECORDTYPE.INTERFACEHDR: m_globals.InterfaceHdr = (XlsBiffInterfaceHdr)rec; break; case BIFFRECORDTYPE.BOUNDSHEET: XlsBiffBoundSheet sheet = (XlsBiffBoundSheet)rec; if (sheet.Type != XlsBiffBoundSheet.SheetType.Worksheet) break; sheet.IsV8 = isV8(); sheet.UseEncoding = m_encoding; m_sheets.Add(new XlsWorksheet(m_globals.Sheets.Count, sheet)); m_globals.Sheets.Add(sheet); break; case BIFFRECORDTYPE.MMS: m_globals.MMS = rec; break; case BIFFRECORDTYPE.COUNTRY: m_globals.Country = rec; break; case BIFFRECORDTYPE.CODEPAGE: m_globals.CodePage = (XlsBiffSimpleValueRecord)rec; try { m_encoding = Encoding.GetEncoding(m_globals.CodePage.Value); } catch (ArgumentException) { // Warning - Password protection // TODO: Attach to ILog } break; case BIFFRECORDTYPE.FONT: case BIFFRECORDTYPE.FONT_V34: m_globals.Fonts.Add(rec); break; case BIFFRECORDTYPE.FORMAT: case BIFFRECORDTYPE.FORMAT_V23: m_globals.Formats.Add(rec); break; case BIFFRECORDTYPE.XF: case BIFFRECORDTYPE.XF_V4: case BIFFRECORDTYPE.XF_V3: case BIFFRECORDTYPE.XF_V2: m_globals.ExtendedFormats.Add(rec); break; case BIFFRECORDTYPE.SST: m_globals.SST = (XlsBiffSST)rec; sst = true; break; case BIFFRECORDTYPE.CONTINUE: if (!sst) break; XlsBiffContinue contSST = (XlsBiffContinue)rec; m_globals.SST.Append(contSST); break; case BIFFRECORDTYPE.EXTSST: m_globals.ExtSST = rec; sst = false; break; case BIFFRECORDTYPE.PROTECT: case BIFFRECORDTYPE.PASSWORD: case BIFFRECORDTYPE.PROT4REVPASSWORD: //IsProtected break; case BIFFRECORDTYPE.EOF: if (m_globals.SST != null) m_globals.SST.ReadStrings(); return; default: continue; } } }
private void ParseXlsStream(Stream fileStream) { using (m_file = fileStream) { m_hdr = XlsHeader.ReadHeader(m_file); XlsRootDirectory dir = new XlsRootDirectory(m_hdr); XlsDirectoryEntry workbookEntry = dir.FindEntry(WORKBOOK) ?? dir.FindEntry(BOOK); if (workbookEntry == null) throw new FileNotFoundException(Errors.ErrorStreamWorkbookNotFound); if (workbookEntry.EntryType != STGTY.STGTY_STREAM) throw new FormatException(Errors.ErrorWorkbookIsNotStream); m_stream = new XlsBiffStream(m_hdr, workbookEntry.StreamFirstSector); ReadWorkbookGlobals(); 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(); GC.SuppressFinalize(this); } }
/// <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 HeaderException(Errors.ErrorHeaderSignature); if (hdr.ByteOrder != 0xFFFE) throw new FormatException(Errors.ErrorHeaderOrder); return hdr; }