Ejemplo n.º 1
0
        private void buttonReadFile_Click(object sender, EventArgs e)
        {
            string fileName = textBoxReadFile.Text;

            if (!File.Exists(fileName))
            {
                MessageBox.Show(string.Format("{0} not found!", fileName));
                return;
            }
            XlsDocument   xls     = new XlsDocument(fileName);
            Bytes         stream  = xls.OLEDoc.Streams[xls.OLEDoc.Streams.GetIndex(org.in2bits.MyOle2.Directory.Biff8Workbook)].Bytes;
            List <Record> records = Record.GetAll(stream);
            StringBuilder sb      = new StringBuilder();

            foreach (Record record in records)
            {
                string name = RID.Name(record.RID);
                sb.Append(name);
                sb.Append(new string(' ', RID.NAME_MAX_LENGTH - name.Length) + ": ");
                byte[] recordData = record.Data.ByteArray;
                for (int i = 0; i < recordData.Length; i++)
                {
                    if (i > 0)
                    {
                        sb.Append(" ");
                    }
                    sb.Append(string.Format("{0:x2}", recordData[i]));
                }
                sb.Append(Environment.NewLine);
            }
            richTextBoxRecordList.Text = sb.ToString();
        }
Ejemplo n.º 2
0
        private void ReadBytes(Bytes bytes, BytesReadCallback bytesReadCallback)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }

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

            //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, XF>     xfs     = new SortedList <ushort, XF>();

            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 ApplicationException(string.Format("Format {0} not found in read FORMAT records or standard/default FORMAT records.", formatIndex));
                }
                xfIdLookups[index] = this.XFs.Add(new XF(_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]);
            }
        }
Ejemplo n.º 3
0
        // Token: 0x060000CA RID: 202 RVA: 0x00005FE0 File Offset: 0x00004FE0
        private void ReadBytes(Bytes bytes, Workbook.BytesReadCallback bytesReadCallback)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentException("can't be zero-length", "bytes");
            }
            SortedList <ushort, ushort> sortedList = new SortedList <ushort, ushort>();
            List <Record> all    = Record.GetAll(bytes);
            List <Record> list   = new List <Record>();
            List <Record> list2  = new List <Record>();
            List <Record> list3  = new List <Record>();
            List <Record> list4  = new List <Record>();
            Record        record = Record.Empty;
            SortedList <int, List <Record> > sortedList2 = new SortedList <int, List <Record> >();
            int num = -1;

            foreach (Record record2 in all)
            {
                if (num >= 0)
                {
                    if (!sortedList2.ContainsKey(num))
                    {
                        sortedList2[num] = new List <Record>();
                    }
                    sortedList2[num].Add(record2);
                    if (record2.RID == RID.EOF)
                    {
                        num++;
                    }
                }
                else if (record2.RID == RID.FONT)
                {
                    list.Add(record2);
                }
                else if (record2.RID == RID.FORMAT)
                {
                    list2.Add(record2);
                }
                else if (record2.RID == RID.XF)
                {
                    list3.Add(record2);
                }
                else if (record2.RID == RID.BOUNDSHEET)
                {
                    list4.Add(record2);
                }
                else if (record2.RID == RID.SST)
                {
                    record = record2;
                }
                else if (record2.RID == RID.EOF)
                {
                    num++;
                }
            }
            SortedList <ushort, Font>   sortedList3 = new SortedList <ushort, Font>();
            SortedList <ushort, string> sortedList4 = new SortedList <ushort, string>();

            new SortedList <ushort, XF>();
            ushort num2 = 0;

            foreach (Record record3 in list)
            {
                Font font = new Font(this._doc, record3.Data);
                SortedList <ushort, Font> sortedList5 = sortedList3;
                ushort num3 = num2;
                num2 = (ushort)(num3 + 1);
                sortedList5[num3] = font;
                this.Fonts.Add(font);
            }
            foreach (Record record4 in list2)
            {
                Bytes  data = record4.Data;
                string text = UnicodeBytes.Read(data.Get(2, data.Length - 2), 16);
                num2 = BitConverter.ToUInt16(data.Get(2).ByteArray, 0);
                sortedList4[num2] = text;
                this.Formats.Add(text);
            }
            num2 = 0;
            while ((int)num2 < list3.Count)
            {
                Record record5 = list3[(int)num2];
                Bytes  data2   = record5.Data;
                ushort key     = BitConverter.ToUInt16(data2.Get(0, 2).ByteArray, 0);
                ushort num4    = BitConverter.ToUInt16(data2.Get(2, 2).ByteArray, 0);
                if (sortedList3.ContainsKey(key))
                {
                    Font   font2 = sortedList3[key];
                    string format;
                    if (sortedList4.ContainsKey(num4))
                    {
                        format = sortedList4[num4];
                    }
                    else
                    {
                        if (!this._formats.ContainsKey(num4))
                        {
                            throw new ApplicationException(string.Format("Format {0} not found in read FORMAT records or standard/default FORMAT records.", num4));
                        }
                        format = this._formats[num4];
                    }
                    sortedList[num2] = this.XFs.Add(new XF(this._doc, record5.Data, font2, format));
                }
                num2 += 1;
            }
            this.XFs.XfIdxLookups = sortedList;
            if (record != Record.Empty)
            {
                this.SharedStringTable.ReadBytes(record);
            }
            if (bytesReadCallback != null)
            {
                bytesReadCallback(all);
            }
            for (int i = 0; i < list4.Count; i++)
            {
                this._worksheets.Add(list4[i], sortedList2[i]);
            }
        }