Example #1
0
        private XlsRowBlock ReadNextBlock(XlsBiffStream biffStream, int startRow, int rows, int minOffset, int maxOffset)
        {
            var result = new XlsRowBlock {
                Rows = new Dictionary <int, Row>()
            };

            // Ensure rows with physical records are initialized with height
            for (var i = 0; i < rows; i++)
            {
                if (RowOffsetMap.TryGetValue(startRow + i, out _))
                {
                    EnsureRow(result, startRow + i);
                }
            }

            if (minOffset == int.MaxValue)
            {
                return(result);
            }

            biffStream.Position = minOffset;

            XlsBiffRecord rec;
            XlsBiffRecord ixfe = null;

            while (biffStream.Position <= maxOffset && (rec = biffStream.Read()) != null)
            {
                if (rec.Id == BIFFRECORDTYPE.IXFE)
                {
                    // BIFF2: If cell.xformat == 63, this contains the actual XF index >= 63
                    ixfe = rec;
                }

                if (rec.IsCell)
                {
                    var cell       = (XlsBiffBlankCell)rec;
                    var currentRow = EnsureRow(result, cell.RowIndex);

                    if (cell.Id == BIFFRECORDTYPE.MULRK)
                    {
                        var cellValues = ReadMultiCell(cell);
                        currentRow.Cells.AddRange(cellValues);
                    }
                    else
                    {
                        var xfIndex   = GetXfIndexForCell(cell, ixfe);
                        var cellValue = ReadSingleCell(biffStream, cell, xfIndex);
                        currentRow.Cells.Add(cellValue);
                    }

                    ixfe = null;
                }
            }

            return(result);
        }
Example #2
0
        private Row EnsureRow(XlsRowBlock result, int rowIndex)
        {
            if (!result.Rows.TryGetValue(rowIndex, out var currentRow))
            {
                currentRow = new Row()
                {
                    RowIndex = rowIndex,
                    Height   = DefaultRowHeight / 20.0,
                    Cells    = new List <Cell>()
                };

                result.Rows.Add(rowIndex, currentRow);
            }

            return(currentRow);
        }
Example #3
0
        private Row EnsureRow(XlsRowBlock result, int rowIndex)
        {
            if (!result.Rows.TryGetValue(rowIndex, out var currentRow))
            {
                var height = DefaultRowHeight / 20.0;
                if (RowOffsetMap.TryGetValue(rowIndex, out var rowOffset) && rowOffset.Record != null)
                {
                    height = (rowOffset.Record.UseDefaultRowHeight ? DefaultRowHeight : rowOffset.Record.RowHeight) / 20.0;
                }

                currentRow = new Row(rowIndex, height, new List <Cell>());

                result.Rows.Add(rowIndex, currentRow);
            }

            return(currentRow);
        }
Example #4
0
        private XlsRowBlock ReadNextBlock(XlsBiffStream biffStream, int startRow, int rows)
        {
            var result = new XlsRowBlock {
                Rows = new Dictionary <int, Row>()
            };

            XlsBiffRecord rec;
            XlsBiffRecord ixfe = null;

            if (!GetMinMaxOffsetsForRowBlock(startRow, rows, out var minOffset, out var maxOffset))
            {
                return(result);
            }

            biffStream.Position = minOffset;

            while (biffStream.Position <= maxOffset && (rec = biffStream.Read()) != null)
            {
                if (rec.Id == BIFFRECORDTYPE.ROW || rec.Id == BIFFRECORDTYPE.ROW_V2)
                {
                    var rowRecord  = (XlsBiffRow)rec;
                    var currentRow = EnsureRow(result, rowRecord.RowIndex);
                    currentRow.Height = (rowRecord.UseDefaultRowHeight ? DefaultRowHeight : rowRecord.RowHeight) / 20.0;
                }

                if (rec.Id == BIFFRECORDTYPE.IXFE)
                {
                    // BIFF2: If cell.xformat == 63, this contains the actual XF index >= 63
                    ixfe = rec;
                }

                if (rec.IsCell)
                {
                    var cell       = (XlsBiffBlankCell)rec;
                    var currentRow = EnsureRow(result, cell.RowIndex);

                    if (cell.Id == BIFFRECORDTYPE.MULRK)
                    {
                        var cellValues = ReadMultiCell(cell);
                        currentRow.Cells.AddRange(cellValues);
                    }
                    else
                    {
                        ushort xFormat;
                        if (Workbook.BiffVersion == 2 && cell.XFormat == 63 && ixfe != null)
                        {
                            xFormat = ixfe.ReadUInt16(0);
                        }
                        else
                        {
                            xFormat = cell.XFormat;
                        }

                        var cellValue = ReadSingleCell(biffStream, cell, xFormat);
                        currentRow.Cells.Add(cellValue);
                    }

                    ixfe = null;
                }
            }

            return(result);
        }
Example #5
0
        private XlsRowBlock ReadNextBlock(XlsBiffStream biffStream)
        {
            var result = new XlsRowBlock {
                Rows = new Dictionary <int, object[]>()
            };

            var currentRowIndex = -1;

            object[] currentRow = null;

            XlsBiffRecord rec;
            XlsBiffRecord ixfe = null;

            while ((rec = biffStream.Read()) != null)
            {
                if (rec is XlsBiffEof)
                {
                    result.EndOfSheet = true;
                    break;
                }

                if (rec is XlsBiffMSODrawing || (!RowContentInMultipleBlocks && rec is XlsBiffDbCell))
                {
                    break;
                }

                if (rec.Id == BIFFRECORDTYPE.IXFE)
                {
                    // BIFF2: If cell.xformat == 63, this contains the actual XF index >= 63
                    ixfe = rec;
                }

                if (rec is XlsBiffBlankCell cell)
                {
                    // In most cases cells are grouped by row
                    if (currentRowIndex != cell.RowIndex)
                    {
                        if (!result.Rows.TryGetValue(cell.RowIndex, out currentRow))
                        {
                            currentRow = new object[FieldCount];
                            result.Rows.Add(cell.RowIndex, currentRow);
                        }

                        currentRowIndex = cell.RowIndex;
                    }

                    ushort xFormat;
                    if (Workbook.BiffVersion == 2 && cell.XFormat == 63 && ixfe != null)
                    {
                        xFormat = ixfe.ReadUInt16(0);
                    }
                    else
                    {
                        xFormat = cell.XFormat;
                    }

                    var additionalRecords = new List <XlsBiffRecord>();
                    while (!PushCellValue(currentRow, cell, xFormat, additionalRecords))
                    {
                        var additionalRecord = biffStream.Read();
                        additionalRecords.Add(additionalRecord);
                    }

                    ixfe = null;
                }
            }

            return(result);
        }