Exemple #1
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;
 }
Exemple #2
0
 private void ReadBytes(Bytes bytes)
 {
     byte[] byteArray = bytes.ByteArray;
     _height = BitConverter.ToUInt16(byteArray, 0);
     SetOptionsValue(bytes.Get(2, 2));
     _colorIndex   = BitConverter.ToUInt16(byteArray, 4);
     _weight       = FontWeightConverter.Convert(BitConverter.ToUInt16(byteArray, 6));
     _escapement   = (EscapementTypes)BitConverter.ToUInt16(byteArray, 8);
     _underline    = (UnderlineTypes)byteArray[10];
     _fontFamily   = (FontFamilies)byteArray[11];
     _characterSet = (CharacterSets)byteArray[12];
     //skip byte index 13
     _fontName = UnicodeBytes.Read(bytes.Get(14, bytes.Length - 14), 8);
 }
Exemple #3
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;
        }
Exemple #4
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);
        }
Exemple #5
0
        private void ReadBytes(Bytes bytes, BytesReadCallback bytesReadCallback)
        {
            if (bytes == null)
            {
                throw new Exception("bytes");
            }

            if (bytes.Length == 0)
            {
                throw new Exception("bytes can't be zero-length ");
            }

            //The XF's read in won't necessarily have the same ID (index) once added to this Workbook,
            //so we need to keep the cross-reference list for re-assignment as we read in the cell records later
            SortedList <ushort, ushort> xfIdLookups = new SortedList <ushort, ushort>();

            List <Record> records = Record.GetAll(bytes);

            List <Record> fontRecords       = new List <Record>();
            List <Record> formatRecords     = new List <Record>();
            List <Record> xfRecords         = new List <Record>();
            List <Record> boundSheetRecords = new List <Record>();
            Record        sstRecord         = Record.Empty;

            SortedList <int, List <Record> > sheetRecords = new SortedList <int, List <Record> >();

            int sheetIndex = -1;

            foreach (Record record in records)
            {
                if (sheetIndex >= 0)
                {
                    if (!sheetRecords.ContainsKey(sheetIndex))
                    {
                        sheetRecords[sheetIndex] = new List <Record>();
                    }
                    sheetRecords[sheetIndex].Add(record);
                    if (record.RID == RID.EOF)
                    {
                        sheetIndex++;
                    }
                }
                else if (record.RID == RID.FONT)
                {
                    fontRecords.Add(record);
                }
                else if (record.RID == RID.FORMAT)
                {
                    formatRecords.Add(record);
                }
                else if (record.RID == RID.XF)
                {
                    xfRecords.Add(record);
                }
                else if (record.RID == RID.BOUNDSHEET)
                {
                    boundSheetRecords.Add(record);
                }
                else if (record.RID == RID.SST)
                {
                    sstRecord = record;
                }
                else if (record.RID == RID.EOF)
                {
                    sheetIndex++;
                }
            }

            SortedList <ushort, Font>       fonts   = new SortedList <ushort, Font>();
            SortedList <ushort, string>     formats = new SortedList <ushort, string>();
            SortedList <ushort, CellFormat> xfs     = new SortedList <ushort, CellFormat>();

            ushort index = 0;

            foreach (Record record in fontRecords)
            {
                Font font = new Font(_doc, record.Data);
                fonts[index++] = font;
                this.Fonts.Add(font);
            }

            foreach (Record record in formatRecords)
            {
                Bytes  recordData = record.Data;
                string format     = UnicodeBytes.Read(recordData.Get(2, recordData.Length - 2), 16);
                index          = BitConverter.ToUInt16(recordData.Get(2).ByteArray, 0);
                formats[index] = format;
                this.Formats.Add(format);
            }

            index = 0;
            for (index = 0; index < xfRecords.Count; index++)
            {
                Record record      = xfRecords[index];
                Bytes  recordData  = record.Data;
                ushort fontIndex   = BitConverter.ToUInt16(recordData.Get(0, 2).ByteArray, 0);
                ushort formatIndex = BitConverter.ToUInt16(recordData.Get(2, 2).ByteArray, 0);
                //ushort styleIndex = BitConverter.ToUInt16(recordData.Get(4, 2))
                if (!fonts.ContainsKey(fontIndex))
                {
                    continue; //TODO: Perhaps default to default XF?  NOTE: This is encountered with TestReferenceFile BlankBudgetWorksheet.xls
                }
                Font   font = fonts[fontIndex];
                string format;
                if (formats.ContainsKey(formatIndex))
                {
                    format = formats[formatIndex];
                }
                else if (_formats.ContainsKey(formatIndex))
                {
                    format = _formats[formatIndex];
                }
                else
                {
                    throw new Exception(string.Format("Format {0} not found in read FORMAT records or standard/default FORMAT records.", formatIndex));
                }
                xfIdLookups[index] = this.XFs.Add(new CellFormat(_doc, record.Data, font, format));
            }
            this.XFs.XfIdxLookups = xfIdLookups;

            if (sstRecord != Record.Empty)
            {
                this.SharedStringTable.ReadBytes(sstRecord);
            }

            if (bytesReadCallback != null)
            {
                bytesReadCallback(records);
            }

            for (int i = 0; i < boundSheetRecords.Count; i++)
            {
                _worksheets.Add(boundSheetRecords[i], sheetRecords[i]);
            }
        }
Exemple #6
0
 private void DecodeLABEL(Bytes data)
 {
     _value = UnicodeBytes.Read(data, 16);
     _type  = CellTypes.Text;
 }