private void ReadWorksheetGlobals() { using (var biffStream = new XlsBiffStream(Stream, (int)DataOffset, Workbook.BiffVersion, null, Workbook.SecretKey, Workbook.Encryption)) { // Check the expected BOF record was found in the BIFF stream if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet) { return; } XlsBiffHeaderFooterString header = null; XlsBiffHeaderFooterString footer = null; // Handle when dimensions report less columns than used by cell records. int maxCellColumn = 0; int maxRowCount = 0; Dictionary <int, bool> previousBlocksObservedRows = new Dictionary <int, bool>(); Dictionary <int, bool> observedRows = new Dictionary <int, bool>(); var recordOffset = biffStream.Position; XlsBiffRecord rec = biffStream.Read(); while (rec != null && !(rec is XlsBiffEof)) { if (rec is XlsBiffDimensions dims) { FieldCount = dims.LastColumn; RowCount = (int)dims.LastRow; } if (rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT || rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT_V2) { var defaultRowHeightRecord = (XlsBiffDefaultRowHeight)rec; DefaultRowHeight = defaultRowHeightRecord.RowHeight; } if (rec.Id == BIFFRECORDTYPE.RECORD1904) { IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1; } if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4) { ExtendedFormats.Add(rec); } if (rec.Id == BIFFRECORDTYPE.FORMAT) { var fmt = (XlsBiffFormatString)rec; if (Workbook.BiffVersion >= 5) { // fmt.Index exists on BIFF5+ only Formats.Add(fmt.Index, fmt); } else { Formats.Add((ushort)Formats.Count, fmt); } } if (rec.Id == BIFFRECORDTYPE.FORMAT_V23) { var fmt = (XlsBiffFormatString)rec; Formats.Add((ushort)Formats.Count, fmt); } if (rec.Id == BIFFRECORDTYPE.CODEPAGE) { var codePage = (XlsBiffSimpleValueRecord)rec; Encoding = EncodingHelper.GetEncoding(codePage.Value); } if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0) { header = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0) { footer = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.CODENAME) { var codeName = (XlsBiffCodeName)rec; CodeName = codeName.GetValue(Encoding); } if (rec.Id == BIFFRECORDTYPE.ROW) { var rowRecord = (XlsBiffRow)rec; SetMinMaxRowOffset(rowRecord.RowIndex, recordOffset); maxRowCount = Math.Max(maxRowCount, rowRecord.RowIndex + 1); } if (rec.IsCell) { var cell = (XlsBiffBlankCell)rec; SetMinMaxRowOffset(cell.RowIndex, recordOffset); maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1); maxRowCount = Math.Max(maxRowCount, cell.RowIndex + 1); } recordOffset = biffStream.Position; rec = biffStream.Read(); } if (header != null || footer != null) { HeaderFooter = new HeaderFooter(false, false) { OddHeader = header?.GetValue(Encoding), OddFooter = footer?.GetValue(Encoding), }; } if (FieldCount < maxCellColumn) { FieldCount = maxCellColumn; } if (RowCount < maxRowCount) { RowCount = maxRowCount; } } }
private void ReadWorksheetGlobals() { var biffStream = new XlsBiffStream(Bytes, (int)DataOffset, Workbook.BiffVersion); // Check the expected BOF record was found in the BIFF stream if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet) { return; } XlsBiffHeaderFooterString header = null; XlsBiffHeaderFooterString footer = null; // Handle when dimensions report less columns than used by cell records. int maxCellColumn = 0; Dictionary <int, bool> previousBlocksObservedRows = new Dictionary <int, bool>(); Dictionary <int, bool> observedRows = new Dictionary <int, bool>(); XlsBiffRecord rec = biffStream.Read(); while (rec != null && !(rec is XlsBiffEof)) { if (rec is XlsBiffDimensions dims) { FieldCount = dims.LastColumn; } if (rec.Id == BIFFRECORDTYPE.RECORD1904) { IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1; } if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4) { ExtendedFormats.Add(rec); } if (rec.Id == BIFFRECORDTYPE.FORMAT) { var fmt = (XlsBiffFormatString)rec; if (Workbook.BiffVersion >= 5) { // fmt.Index exists on BIFF5+ only Formats.Add(fmt.Index, fmt); } else { Formats.Add((ushort)Formats.Count, fmt); } } if (rec.Id == BIFFRECORDTYPE.FORMAT_V23) { var fmt = (XlsBiffFormatString)rec; Formats.Add((ushort)Formats.Count, fmt); } if (rec.Id == BIFFRECORDTYPE.CODEPAGE) { var codePage = (XlsBiffSimpleValueRecord)rec; Encoding = EncodingHelper.GetEncoding(codePage.Value); } if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0) { header = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0) { footer = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.CODENAME) { var codeName = (XlsBiffCodeName)rec; CodeName = codeName.GetValue(Encoding); } if (!RowContentInMultipleBlocks && rec is XlsBiffDbCell) { foreach (int row in observedRows.Keys) { previousBlocksObservedRows[row] = true; } observedRows.Clear(); } if (rec is XlsBiffBlankCell cell) { maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1); if (!RowContentInMultipleBlocks) { if (previousBlocksObservedRows.ContainsKey(cell.RowIndex)) { RowContentInMultipleBlocks = true; previousBlocksObservedRows.Clear(); observedRows.Clear(); } observedRows[cell.RowIndex] = true; } } rec = biffStream.Read(); } if (header != null || footer != null) { HeaderFooter = new HeaderFooter(false, false) { OddHeader = header?.GetValue(Encoding), OddFooter = footer?.GetValue(Encoding), }; } if (FieldCount < maxCellColumn) { FieldCount = maxCellColumn; } }
private void ReadWorksheetGlobals() { using (var biffStream = new XlsBiffStream(Stream, (int)DataOffset, Workbook.BiffVersion, null, Workbook.SecretKey, Workbook.Encryption)) { // Check the expected BOF record was found in the BIFF stream if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet) { return; } XlsBiffHeaderFooterString header = null; XlsBiffHeaderFooterString footer = null; int maxCellColumn = 0; int maxRowCount = 0; var mergeCells = new List <CellRange>(); var biffFormats = new Dictionary <ushort, XlsBiffFormatString>(); var recordOffset = biffStream.Position; var rec = biffStream.Read(); while (rec != null && !(rec is XlsBiffEof)) { if (rec is XlsBiffDimensions dims) { FieldCount = dims.LastColumn; RowCount = (int)dims.LastRow; } if (rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT || rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT_V2) { var defaultRowHeightRecord = (XlsBiffDefaultRowHeight)rec; DefaultRowHeight = defaultRowHeightRecord.RowHeight; } if (rec.Id == BIFFRECORDTYPE.RECORD1904) { IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1; } if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4) { ExtendedFormats.Add((XlsBiffXF)rec); } if (rec.Id == BIFFRECORDTYPE.MERGECELLS) { mergeCells.AddRange(((XlsBiffMergeCells)rec).MergeCells); } if (rec.Id == BIFFRECORDTYPE.FORMAT) { var fmt = (XlsBiffFormatString)rec; if (Workbook.BiffVersion >= 5) { // fmt.Index exists on BIFF5+ only biffFormats.Add(fmt.Index, fmt); } else { biffFormats.Add((ushort)biffFormats.Count, fmt); } } if (rec.Id == BIFFRECORDTYPE.FORMAT_V23) { var fmt = (XlsBiffFormatString)rec; biffFormats.Add((ushort)biffFormats.Count, fmt); } if (rec.Id == BIFFRECORDTYPE.CODEPAGE) { var codePage = (XlsBiffSimpleValueRecord)rec; Encoding = EncodingHelper.GetEncoding(codePage.Value); } if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0) { header = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0) { footer = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.CODENAME) { var codeName = (XlsBiffCodeName)rec; CodeName = codeName.GetValue(Encoding); } if (rec.Id == BIFFRECORDTYPE.ROW) { var rowRecord = (XlsBiffRow)rec; SetMinMaxRowOffset(rowRecord.RowIndex, recordOffset); maxRowCount = Math.Max(maxRowCount, rowRecord.RowIndex + 1); } if (rec.IsCell) { var cell = (XlsBiffBlankCell)rec; SetMinMaxRowOffset(cell.RowIndex, recordOffset); maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1); maxRowCount = Math.Max(maxRowCount, cell.RowIndex + 1); } recordOffset = biffStream.Position; rec = biffStream.Read(); } if (header != null || footer != null) { HeaderFooter = new HeaderFooter(false, false) { OddHeader = header?.GetValue(Encoding), OddFooter = footer?.GetValue(Encoding), }; } foreach (var biffFormat in biffFormats) { var formatString = biffFormat.Value.GetValue(Encoding); Formats.Add(biffFormat.Key, new NumberFormatString(formatString)); } if (mergeCells.Count > 0) { MergeCells = mergeCells.ToArray(); } if (FieldCount < maxCellColumn) { FieldCount = maxCellColumn; } if (RowCount < maxRowCount) { RowCount = maxRowCount; } } }
private void ReadWorksheetGlobals() { using (var biffStream = new XlsBiffStream(Stream, (int)DataOffset, Workbook.BiffVersion, null, Workbook.SecretKey, Workbook.Encryption)) { // Check the expected BOF record was found in the BIFF stream if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet) { return; } XlsBiffHeaderFooterString header = null; XlsBiffHeaderFooterString footer = null; var ixfeOffset = -1; int maxCellColumn = 0; int maxRowCount = 0; // number of rows with cell records int maxRowCountFromRowRecord = 0; // number of rows with row records var mergeCells = new List <CellRange>(); var biffFormats = new Dictionary <ushort, XlsBiffFormatString>(); var recordOffset = biffStream.Position; var rec = biffStream.Read(); var columnWidths = new List <Col>(); while (rec != null && !(rec is XlsBiffEof)) { if (rec is XlsBiffDimensions dims) { FieldCount = dims.LastColumn; RowCount = (int)dims.LastRow; } if (rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT || rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT_V2) { var defaultRowHeightRecord = (XlsBiffDefaultRowHeight)rec; DefaultRowHeight = defaultRowHeightRecord.RowHeight; } if (rec.Id == BIFFRECORDTYPE.RECORD1904) { IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1; } if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4) { // NOTE: XF records should only occur in raw BIFF2-4 single worksheet documents without the workbook stream, or globally in the workbook stream. // It is undefined behavior if multiple worksheets in a workbook declare XF records. Workbook.AddExtendedFormat(-1, ((XlsBiffXF)rec).Format, true); } if (rec.Id == BIFFRECORDTYPE.MERGECELLS) { mergeCells.AddRange(((XlsBiffMergeCells)rec).MergeCells); } if (rec.Id == BIFFRECORDTYPE.COLINFO) { columnWidths.Add(((XlsBiffColInfo)rec).Value); } if (rec.Id == BIFFRECORDTYPE.FORMAT) { var fmt = (XlsBiffFormatString)rec; if (Workbook.BiffVersion >= 5) { // fmt.Index exists on BIFF5+ only biffFormats.Add(fmt.Index, fmt); } else { biffFormats.Add((ushort)biffFormats.Count, fmt); } } if (rec.Id == BIFFRECORDTYPE.FORMAT_V23) { var fmt = (XlsBiffFormatString)rec; biffFormats.Add((ushort)biffFormats.Count, fmt); } if (rec.Id == BIFFRECORDTYPE.CODEPAGE) { var codePage = (XlsBiffSimpleValueRecord)rec; Encoding = EncodingHelper.GetEncoding(codePage.Value); } if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0) { header = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0) { footer = (XlsBiffHeaderFooterString)rec; } if (rec.Id == BIFFRECORDTYPE.CODENAME) { var codeName = (XlsBiffCodeName)rec; CodeName = codeName.GetValue(Encoding); } if (rec.Id == BIFFRECORDTYPE.ROW || rec.Id == BIFFRECORDTYPE.ROW_V2) { var rowRecord = (XlsBiffRow)rec; SetMinMaxRow(rowRecord.RowIndex, rowRecord); // Count rows by row records without affecting the overlap in OffsetMap maxRowCountFromRowRecord = Math.Max(maxRowCountFromRowRecord, rowRecord.RowIndex + 1); } if (rec.Id == BIFFRECORDTYPE.IXFE) { ixfeOffset = recordOffset; } if (rec.IsCell) { var cell = (XlsBiffBlankCell)rec; maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1); maxRowCount = Math.Max(maxRowCount, cell.RowIndex + 1); if (ixfeOffset != -1) { SetMinMaxRowOffset(cell.RowIndex, ixfeOffset, maxRowCount - 1); ixfeOffset = -1; } SetMinMaxRowOffset(cell.RowIndex, recordOffset, maxRowCount - 1); } recordOffset = biffStream.Position; rec = biffStream.Read(); // Stop if we find the start out a new substream. Not always that files have the required EOF before a substream BOF. if (rec is XlsBiffBOF) { break; } } if (header != null || footer != null) { HeaderFooter = new HeaderFooter(false, false) { OddHeader = header?.GetValue(Encoding), OddFooter = footer?.GetValue(Encoding), }; } foreach (var biffFormat in biffFormats) { Workbook.AddNumberFormat(biffFormat.Key, biffFormat.Value.GetValue(Encoding)); } if (mergeCells.Count > 0) { MergeCells = mergeCells.ToArray(); } if (FieldCount < maxCellColumn) { FieldCount = maxCellColumn; } maxRowCount = Math.Max(maxRowCount, maxRowCountFromRowRecord); if (RowCount < maxRowCount) { RowCount = maxRowCount; } if (columnWidths.Count > 0) { ColumnWidths = columnWidths.ToArray(); } } }