Example #1
0
        private void AddCells(Record record)
        {
            Bytes bytes = record.Data;
            ushort rowIndex = bytes.Get(0, 2).GetBits().ToUInt16();
            ushort colIndex = bytes.Get(2, 2).GetBits().ToUInt16();
            ushort lastColIndex = colIndex;
            ushort offset = 4;

            byte[] rid = record.RID;
            bool isMulti = false;

            if (rid == RID.MULBLANK)
            {
                isMulti = true;
                rid = RID.BLANK;
            }
            else if (rid == RID.MULRK)
            {
                isMulti = true;
                rid = RID.RK;
            }

            if (isMulti)
                lastColIndex = bytes.Get(bytes.Length - 2, 2).GetBits().ToUInt16();


            while (colIndex <= lastColIndex)
            {
                Cell cell = Cells.Add((ushort)(rowIndex + 1), (ushort)(colIndex + 1));
                ushort xfIndex = bytes.Get(offset, 2).GetBits().ToUInt16();
                offset += 2;

                Bytes data;
                if (rid == RID.BLANK)
                    data = new Bytes();
                else if (rid == RID.RK)
                {
                    data = bytes.Get(offset, 4);
                    offset += 4;
                    cell.SetValue(rid, data);
                }
                else
                {
                    data = bytes.Get(offset, bytes.Length - offset);
                    if (rid == RID.FORMULA)
                    {
                        FormulaRecord formulaRecord = record as FormulaRecord;
                        cell.SetFormula(data, formulaRecord.StringRecord);
                    }
                    else
                        cell.SetValue(rid, data);
                }
                colIndex++;
            }
        }
Example #2
0
        internal Worksheet(XlsDocument doc, Record boundSheet, List<Record> sheetRecords) : this(doc)
        {
            byte[] byteArray = boundSheet.Data.ByteArray;

            byte visibility = byteArray[4];
            if (visibility == 0x00)
                _visibility = WorksheetVisibilities.Visible;
            else if (visibility == 0x01)
                _visibility = WorksheetVisibilities.Hidden;
            else if (visibility == 0x02)
                _visibility = WorksheetVisibilities.StrongHidden;
            else
                throw new Exception(string.Format("Unknown Visibility {0}", visibility));

            byte type = byteArray[5];
            if (type == 0x00)
                _sheettype = WorksheetTypes.Worksheet;
            else if (type == 0x02)
                _sheettype = WorksheetTypes.Chart;
            else if (type == 0x06)
                _sheettype = WorksheetTypes.VBModule;
            else
                throw new Exception(string.Format("Unknown Sheet Type {0}", type));

            List<Record> rowRecords = new List<Record>();
            List<Record> cellRecords = new List<Record>();

            for (int i = 0; i < sheetRecords.Count; i++)
            {
                Record record = sheetRecords[i];
                if (record.IsCellRecord())
                {
                    if (record.RID == RID.FORMULA)
                    {
                        Record formulaStringRecord = null;
                        if ((i + i) < sheetRecords.Count)
                        {
                            formulaStringRecord = sheetRecords[i + 1];
                            if (formulaStringRecord.RID != RID.STRING)
                                formulaStringRecord = null;
                        }
                        record = new FormulaRecord(record, formulaStringRecord);
                    }

                    cellRecords.Add(record);
                }
                else if (record.RID == RID.ROW)
                    rowRecords.Add(record);
            }

            //Add the Rows first so they exist for adding the Cells
            foreach (Record rowRecord in rowRecords)
            {
                Bytes rowBytes = rowRecord.Data;
                ushort rowIndex = rowBytes.Get(0, 2).GetBits().ToUInt16();
                Row row = Rows.AddRow(rowIndex);
                bool isDefaultHeight = rowBytes.Get(6, 2).GetBits().Values[15];
                ushort height = 0;
                if (!isDefaultHeight)
                {
                    height = rowBytes.Get(6, 2).GetBits().Get(0, 14).ToUInt16();
                    //TODO: Set height on Row when reading (after Row Height implemented)
                }
                bool defaultsWritten = (rowBytes.Get(10, 1).ByteArray[0] == 0x01);
                if (defaultsWritten)
                {
                    //TODO: Read ROW record defaults
                }
            }

            foreach (Record record in cellRecords)
                AddCells(record);

            _name = UnicodeBytes.Read(boundSheet.Data.Get(6, boundSheet.Data.Length - 6), 8);
        }