Example #1
0
        private bool readWorkSheetGlobals(XlsWorksheet sheet, out XlsBiffIndex idx, out XlsBiffRow row)
        {
            idx = null;
            row = null;

            m_stream.Seek((int)sheet.DataOffset, SeekOrigin.Begin);

            //
            // Read BOF
            //
            XlsBiffBOF bof = m_stream.Read() as XlsBiffBOF;

            if (bof == null || bof.Type != BIFFTYPE.Worksheet)
            {
                return(false);
            }

            //DumpBiffRecords();

            //
            // Read Index
            //
            XlsBiffRecord rec = m_stream.Read();

            if (rec == null)
            {
                return(false);
            }
            if (rec is XlsBiffIndex)
            {
                idx = rec as XlsBiffIndex;
            }
            else if (rec is XlsBiffUncalced)
            {
                // Sometimes this come before the index...
                idx = m_stream.Read() as XlsBiffIndex;
            }

            //if (null == idx)
            //{
            //	// There is a record before the index! Chech his type and see the MS Biff Documentation
            //	return false;
            //}

            if (idx != null)
            {
                idx.IsV8 = isV8();
            }

            //
            // Read Demension
            //
            XlsBiffRecord     trec;
            XlsBiffDimensions dims = null;

            do
            {
                trec = m_stream.Read();
                if (trec.ID == BIFFRECORDTYPE.DIMENSIONS)
                {
                    dims = (XlsBiffDimensions)trec;
                    break;
                }
            } while (trec != null && trec.ID != BIFFRECORDTYPE.ROW);

            //
            // Read Row
            //
            //if we are already on row record then set that as the row, otherwise step forward till we get to a row record
            if (trec.ID == BIFFRECORDTYPE.ROW)
            {
                row = (XlsBiffRow)trec;
            }

            XlsBiffRow rowRecord = null;

            while (rowRecord == null)
            {
                if (m_stream.Position >= m_stream.Size)
                {
                    break;
                }
                var thisRec = m_stream.Read();

                if (thisRec is XlsBiffEOF)
                {
                    break;
                }
                rowRecord = thisRec as XlsBiffRow;
            }

            if (rowRecord != null)
            {
                ////Console.WriteLine("Got row {0}, rec: id={1},rowindex={2}, rowColumnStart={3}, rowColumnEnd={4}", rowRecord.Offset, rowRecord.ID, rowRecord.RowIndex, rowRecord.FirstDefinedColumn, rowRecord.LastDefinedColumn);
            }

            row = rowRecord;

            if (dims != null)
            {
                dims.IsV8 = isV8();
                ////LogManager.Log(this).Debug("dims IsV8={0}", dims.IsV8);
                m_maxCol = dims.LastColumn - 1;

                //handle case where sheet reports last column is 1 but there are actually more
                if (m_maxCol <= 0 && rowRecord != null)
                {
                    m_maxCol = rowRecord.LastDefinedColumn;
                }

                m_maxRow         = (int)dims.LastRow;
                sheet.Dimensions = dims;
            }
            else
            {
                m_maxCol = 256;
                m_maxRow = (int)idx.LastExistingRow;
            }

            if (idx != null && idx.LastExistingRow <= idx.FirstExistingRow)
            {
                return(false);
            }
            else if (row == null)
            {
                return(false);
            }

            m_depth = 0;

            //
            // Read Hyper Link
            //
            bool hasFound = false;

            while (true)
            {
                if (m_stream.Position >= m_stream.Size)
                {
                    break;
                }
                var thisRecord = m_stream.Read();

                if (thisRecord is XlsBiffEOF)
                {
                    break;
                }

                XlsBiffHyperLink hyperLink = thisRecord as XlsBiffHyperLink;
                if (hyperLink != null)
                {
                    hasFound = true;
                    ////Console.WriteLine("Read HyperLink");
                    //Console.WriteLine("Url:{0},{1}",hyperLink.Url,hyperLink.CellRangeAddress.ToString());
                    m_globals.AddHyperLink(hyperLink);
                }

                if (hasFound == true && hyperLink == null)
                {
                    break;
                }
            }

            return(true);
        }
Example #2
0
        private void pushCellValue(XlsBiffBlankCell cell)
        {
            double _dValue;

            bool hasValue = true;

            switch (cell.ID)
            {
            case BIFFRECORDTYPE.BOOLERR:
                if (cell.ReadByte(7) == 0)
                {
                    m_cellsValues[cell.ColumnIndex] = new XlsCell(cell.ReadByte(6) != 0);
                }
                else
                {
                    hasValue = false;
                }
                break;

            case BIFFRECORDTYPE.BOOLERR_OLD:
                if (cell.ReadByte(8) == 0)
                {
                    m_cellsValues[cell.ColumnIndex] = new XlsCell(cell.ReadByte(7) != 0);
                }
                else
                {
                    hasValue = false;
                }
                break;

            case BIFFRECORDTYPE.INTEGER:
            case BIFFRECORDTYPE.INTEGER_OLD:
                m_cellsValues[cell.ColumnIndex] = new XlsCell(((XlsBiffIntegerCell)cell).Value);
                break;

            case BIFFRECORDTYPE.NUMBER:
            case BIFFRECORDTYPE.NUMBER_OLD:
                _dValue = ((XlsBiffNumberCell)cell).Value;
                m_cellsValues[cell.ColumnIndex] =
                    new XlsCell(_dValue);
                break;

            case BIFFRECORDTYPE.LABEL:
            case BIFFRECORDTYPE.LABEL_OLD:
            case BIFFRECORDTYPE.RSTRING:
                m_cellsValues[cell.ColumnIndex] = new XlsCell(((XlsBiffLabelCell)cell).Value);
                break;

            case BIFFRECORDTYPE.LABELSST:
                string tmp = m_globals.SST.GetString(((XlsBiffLabelSSTCell)cell).SSTIndex);
                m_cellsValues[cell.ColumnIndex] = new XlsCell(tmp);
                break;

            case BIFFRECORDTYPE.RK:
                _dValue = ((XlsBiffRKCell)cell).Value;
                m_cellsValues[cell.ColumnIndex] = new XlsCell(_dValue);
                break;

            case BIFFRECORDTYPE.MULRK:
                var  _rkCell = (XlsBiffMulRKCell)cell;
                bool hasSet  = false;
                for (ushort j = cell.ColumnIndex; j <= _rkCell.LastColumnIndex; j++)
                {
                    _dValue          = _rkCell.GetValue(j);
                    m_cellsValues[j] = new XlsCell(_dValue);
                    hasSet           = true;
                }
                hasValue = hasSet;

                break;

            case BIFFRECORDTYPE.BLANK:
            case BIFFRECORDTYPE.BLANK_OLD:
            case BIFFRECORDTYPE.MULBLANK:
                // Skip blank cells
                hasValue = false;
                break;

            case BIFFRECORDTYPE.FORMULA:
            case BIFFRECORDTYPE.FORMULA_OLD:
                object _oValue = ((XlsBiffFormulaCell)cell).Value;
                if (!(_oValue is FORMULAERROR))
                {
                    m_cellsValues[cell.ColumnIndex] =
                        new XlsCell(_oValue);
                }
                else
                {
                    hasValue = false;
                }
                break;

            default:
                hasValue = false;
                break;
            }

            if (hasValue)
            {
                XlsBiffHyperLink hyperLink = m_globals.GetHyperLink(cell.RowIndex, cell.ColumnIndex);
                if (hyperLink != null)
                {
                    m_cellsValues[cell.ColumnIndex].SetHyperLink(hyperLink.Url);
                }
            }
        }