Example #1
0
        internal FormulaRecord(Record formulaRecord, Record stringRecord)
            : base()
        {
            _rid = formulaRecord.RID;
            _data = formulaRecord.Data;
            _continues = formulaRecord.Continues;

            StringRecord = stringRecord;
        }
Example #2
0
        internal static string Read(Record record, int lengthBits, ref int continueIndex, ref ushort offset)
        {
            string text = string.Empty;

            ReadState state = new ReadState(record, lengthBits, continueIndex, offset);
            Read(state);
            continueIndex = state.ContinueIndex;
            offset = state.Offset;

            return new string(state.CharactersRead.ToArray());
        }
Example #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static List<Record> GetAll(Bytes stream)
        {
            int i = 0;
            List<Record> records = new List<Record>();
            Record lastNonContinue = Record.Empty;
            while (i < (stream.Length - 4))
            {
                byte[] rid = org.in2bits.MyXls.RID.ByteArray(stream.Get(i, 2).ByteArray);
                Bytes data = new Bytes();
                if (rid == MyXls.RID.Empty)
                    break;
                int length = BitConverter.ToUInt16(stream.Get(i + 2, 2).ByteArray, 0);
                data = stream.Get(i + 4, length);
                Record record = new Record(rid, data);
                i += (4 + length);
                if (rid == MyXls.RID.CONTINUE)
                {
                    if (lastNonContinue == Record.Empty)
                        throw new Exception("Found CONTINUE record without previous/parent record.");

                    lastNonContinue.Continues.Add(record);
                }
                else
                {
                    lastNonContinue = record;
                    records.Add(record);
                }
            }

            return records;
        }
Example #4
0
 private static string Read(Record record, int lengthBits, ushort offset)
 {
     int continueIndex = -1; //throw away
     return Read(record, lengthBits, ref continueIndex, ref offset);
 }
Example #5
0
            public ReadState(Record record, int lengthBits, int continueIndex, ushort offset)
            {
                LengthBits = lengthBits;
                Record = record;
                ContinueIndex = continueIndex;
                Offset = offset;

                Bytes data = GetRecordData();

                if (LengthBits == 8)
                {
                    TotalCharacters = data.Get(offset, 1).ByteArray[0];
                    Offset++;
                }
                else
                {
                    TotalCharacters = data.Get(offset, 2).GetBits().ToUInt16();
                    Offset += 2;
                }

                ReadOptionsFlags();

                HasAsianPhonetics = (OptionsFlags & 0x04) == 0x04;
                HasRichTextSettings = (OptionsFlags & 0x08) == 0x08;

                if (HasRichTextSettings)
                {
                    FormattingRunCount = BitConverter.ToUInt16(data.Get(Offset, 2).ByteArray, 0);
                    Offset += 2;
                    //bytesRemaining += (ushort)(4 * formattingRuns);
                    //NOTE: When implementing Rich Text, remember to add total length of formating runs to bytesRead
                    //throw new NotSupportedException("Rich Text text values in cells are not yet supported");
                }

                if (HasAsianPhonetics)
                {
                    PhoneticSettingsByteCount = BitConverter.ToUInt32(data.Get(Offset, 4).ByteArray, 0);
                    Offset += 4;
                    //NOTE: When implementing Asian Text, remember to add total length of Asian Phonetic Settings Block to bytesRead
                    //throw new NotSupportedException("Asian Phonetic text values in cells are not yet supported");
                }
            }
Example #6
0
 internal static string Read(Bytes bytes, int lengthBits)
 {
     Record record = new Record(RID.Empty, bytes);
     return Read(record, lengthBits, 0);
 }
Example #7
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>();
            List<Record> noteRecords = 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);
                else if (record.RID == RID.NOTE)
                    noteRecords.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);

            foreach (Record record in noteRecords)
                AddNote(record);

            _name = UnicodeBytes.Read(boundSheet.Data.Get(6, boundSheet.Data.Length - 6), 8);
        }
Example #8
0
 private void AddNote(Record record)
 {
     Bytes bytes = record.Data;
     ushort rowIndex = bytes.Get(0, 2).GetBits().ToUInt16();
     ushort colIndex = bytes.Get(2, 2).GetBits().ToUInt16();
     Bytes options = bytes.Get(4, 2);
     ushort objId = bytes.Get(6, 2).GetBits().ToUInt16();
     string authorName = UnicodeBytes.Read(bytes.Get(8, bytes.Length - 8), 16);
     Debugger.Break();
 }
Example #9
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();
                ushort index = _doc.Workbook.XFs.XfIdxLookups[xfIndex];
                cell.SetXfIndex(index);
                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 #10
0
 private void DecodeFORMULA(Bytes data, Record stringRecord)
 {
     //TODO: Read in formula properties
     if (stringRecord != null)
         _value = UnicodeBytes.Read(stringRecord.Data, 16);
     _type = CellTypes.Formula;
 }
Example #11
0
 internal void SetFormula(Bytes data, Record stringRecord)
 {
     DecodeFORMULA(data, stringRecord);
 }
Example #12
0
 //TODO: Implement Rich Text Value support
 //TODO: Implement Asian Text Value support
 internal void ReadBytes(Record sstRecord)
 {
     uint totalStrings = sstRecord.Data.Get(0, 4).GetBits().ToUInt32();
     uint uniqueStrings = sstRecord.Data.Get(4, 4).GetBits().ToUInt32();
     int stringIndex = 0;
     ushort offset = 8;
     int continueIndex = -1;
     while (stringIndex < uniqueStrings)
     {
         string sharedString = UnicodeBytes.Read(sstRecord, 16, ref continueIndex, ref offset);
         Add(sharedString);
         stringIndex++;
     }
     _countAll = totalStrings;
 }