Пример #1
0
        private void ReadStyles()
        {
            using var reader = _zipWorker.GetStylesReader();
            if (reader == null)
            {
                return;
            }

            Record record;

            while ((record = reader.Read()) != null)
            {
                switch (record)
                {
                case ExtendedFormatRecord xf:
                    ExtendedFormats.Add(xf.ExtendedFormat);
                    break;

                case CellStyleExtendedFormatRecord csxf:
                    CellStyleExtendedFormats.Add(csxf.ExtendedFormat);
                    break;

                case NumberFormatRecord nf:
                    AddNumberFormat(nf.FormatIndexInFile, nf.FormatString);
                    break;
                }
            }
        }
Пример #2
0
        internal void AddXf(XlsBiffXF xf)
        {
            var extendedFormat = new ExtendedFormat()
            {
                FontIndex           = xf.Font,
                NumberFormatIndex   = xf.Format,
                Locked              = xf.IsLocked,
                Hidden              = xf.IsHidden,
                HorizontalAlignment = xf.HorizontalAlignment,
                IndentLevel         = xf.IndentLevel,
                ParentCellStyleXf   = xf.ParentCellStyleXf,
            };

            // The workbook holds two kinds of XF records: Cell XFs, and Cell Style XFs.
            // In the binary XLS format, both kinds of XF records are saved in a single list,
            // whereas the XLSX format has two separate lists - like the CommonWorkbook internals.
            // The Cell XFs hold indexes into the Cell Style XF list, so adding the XF in both lists
            // here to keep the indexes the same.
            ExtendedFormats.Add(extendedFormat);
            CellStyleExtendedFormats.Add(extendedFormat);
        }
Пример #3
0
        private void ReadWorksheetGlobals()
        {
            using (var biffStream = new XlsBiffStream(Stream, (int)DataOffset, Workbook.BiffVersion, null, Workbook.SecretKey, Workbook.Encryption))
            {
                // Check the expected BOF record was found in the BIFF stream
                if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet)
                {
                    return;
                }

                XlsBiffHeaderFooterString header = null;
                XlsBiffHeaderFooterString footer = null;

                // Handle when dimensions report less columns than used by cell records.
                int maxCellColumn = 0;
                int maxRowCount   = 0;
                Dictionary <int, bool> previousBlocksObservedRows = new Dictionary <int, bool>();
                Dictionary <int, bool> observedRows = new Dictionary <int, bool>();

                var           recordOffset = biffStream.Position;
                XlsBiffRecord rec          = biffStream.Read();
                while (rec != null && !(rec is XlsBiffEof))
                {
                    if (rec is XlsBiffDimensions dims)
                    {
                        FieldCount = dims.LastColumn;
                        RowCount   = (int)dims.LastRow;
                    }

                    if (rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT || rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT_V2)
                    {
                        var defaultRowHeightRecord = (XlsBiffDefaultRowHeight)rec;
                        DefaultRowHeight = defaultRowHeightRecord.RowHeight;
                    }

                    if (rec.Id == BIFFRECORDTYPE.RECORD1904)
                    {
                        IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1;
                    }

                    if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4)
                    {
                        ExtendedFormats.Add(rec);
                    }

                    if (rec.Id == BIFFRECORDTYPE.FORMAT)
                    {
                        var fmt = (XlsBiffFormatString)rec;
                        if (Workbook.BiffVersion >= 5)
                        {
                            // fmt.Index exists on BIFF5+ only
                            Formats.Add(fmt.Index, fmt);
                        }
                        else
                        {
                            Formats.Add((ushort)Formats.Count, fmt);
                        }
                    }

                    if (rec.Id == BIFFRECORDTYPE.FORMAT_V23)
                    {
                        var fmt = (XlsBiffFormatString)rec;
                        Formats.Add((ushort)Formats.Count, fmt);
                    }

                    if (rec.Id == BIFFRECORDTYPE.CODEPAGE)
                    {
                        var codePage = (XlsBiffSimpleValueRecord)rec;
                        Encoding = EncodingHelper.GetEncoding(codePage.Value);
                    }

                    if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0)
                    {
                        header = (XlsBiffHeaderFooterString)rec;
                    }

                    if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0)
                    {
                        footer = (XlsBiffHeaderFooterString)rec;
                    }

                    if (rec.Id == BIFFRECORDTYPE.CODENAME)
                    {
                        var codeName = (XlsBiffCodeName)rec;
                        CodeName = codeName.GetValue(Encoding);
                    }

                    if (rec.Id == BIFFRECORDTYPE.ROW)
                    {
                        var rowRecord = (XlsBiffRow)rec;
                        SetMinMaxRowOffset(rowRecord.RowIndex, recordOffset);
                        maxRowCount = Math.Max(maxRowCount, rowRecord.RowIndex + 1);
                    }

                    if (rec.IsCell)
                    {
                        var cell = (XlsBiffBlankCell)rec;
                        SetMinMaxRowOffset(cell.RowIndex, recordOffset);
                        maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1);
                        maxRowCount   = Math.Max(maxRowCount, cell.RowIndex + 1);
                    }

                    recordOffset = biffStream.Position;
                    rec          = biffStream.Read();
                }

                if (header != null || footer != null)
                {
                    HeaderFooter = new HeaderFooter(false, false)
                    {
                        OddHeader = header?.GetValue(Encoding),
                        OddFooter = footer?.GetValue(Encoding),
                    };
                }

                if (FieldCount < maxCellColumn)
                {
                    FieldCount = maxCellColumn;
                }

                if (RowCount < maxRowCount)
                {
                    RowCount = maxRowCount;
                }
            }
        }
Пример #4
0
        private void ReadWorksheetGlobals()
        {
            using (var biffStream = new XlsBiffStream(Stream, (int)DataOffset, Workbook.BiffVersion, null, Workbook.SecretKey, Workbook.Encryption))
            {
                // Check the expected BOF record was found in the BIFF stream
                if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet)
                {
                    return;
                }

                XlsBiffHeaderFooterString header = null;
                XlsBiffHeaderFooterString footer = null;

                int maxCellColumn = 0;
                int maxRowCount   = 0;

                var mergeCells   = new List <CellRange>();
                var biffFormats  = new Dictionary <ushort, XlsBiffFormatString>();
                var recordOffset = biffStream.Position;
                var rec          = biffStream.Read();

                while (rec != null && !(rec is XlsBiffEof))
                {
                    if (rec is XlsBiffDimensions dims)
                    {
                        FieldCount = dims.LastColumn;
                        RowCount   = (int)dims.LastRow;
                    }

                    if (rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT || rec.Id == BIFFRECORDTYPE.DEFAULTROWHEIGHT_V2)
                    {
                        var defaultRowHeightRecord = (XlsBiffDefaultRowHeight)rec;
                        DefaultRowHeight = defaultRowHeightRecord.RowHeight;
                    }

                    if (rec.Id == BIFFRECORDTYPE.RECORD1904)
                    {
                        IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1;
                    }

                    if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4)
                    {
                        ExtendedFormats.Add((XlsBiffXF)rec);
                    }

                    if (rec.Id == BIFFRECORDTYPE.MERGECELLS)
                    {
                        mergeCells.AddRange(((XlsBiffMergeCells)rec).MergeCells);
                    }

                    if (rec.Id == BIFFRECORDTYPE.FORMAT)
                    {
                        var fmt = (XlsBiffFormatString)rec;
                        if (Workbook.BiffVersion >= 5)
                        {
                            // fmt.Index exists on BIFF5+ only
                            biffFormats.Add(fmt.Index, fmt);
                        }
                        else
                        {
                            biffFormats.Add((ushort)biffFormats.Count, fmt);
                        }
                    }

                    if (rec.Id == BIFFRECORDTYPE.FORMAT_V23)
                    {
                        var fmt = (XlsBiffFormatString)rec;
                        biffFormats.Add((ushort)biffFormats.Count, fmt);
                    }

                    if (rec.Id == BIFFRECORDTYPE.CODEPAGE)
                    {
                        var codePage = (XlsBiffSimpleValueRecord)rec;
                        Encoding = EncodingHelper.GetEncoding(codePage.Value);
                    }

                    if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0)
                    {
                        header = (XlsBiffHeaderFooterString)rec;
                    }

                    if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0)
                    {
                        footer = (XlsBiffHeaderFooterString)rec;
                    }

                    if (rec.Id == BIFFRECORDTYPE.CODENAME)
                    {
                        var codeName = (XlsBiffCodeName)rec;
                        CodeName = codeName.GetValue(Encoding);
                    }

                    if (rec.Id == BIFFRECORDTYPE.ROW)
                    {
                        var rowRecord = (XlsBiffRow)rec;
                        SetMinMaxRowOffset(rowRecord.RowIndex, recordOffset);
                        maxRowCount = Math.Max(maxRowCount, rowRecord.RowIndex + 1);
                    }

                    if (rec.IsCell)
                    {
                        var cell = (XlsBiffBlankCell)rec;
                        SetMinMaxRowOffset(cell.RowIndex, recordOffset);
                        maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1);
                        maxRowCount   = Math.Max(maxRowCount, cell.RowIndex + 1);
                    }

                    recordOffset = biffStream.Position;
                    rec          = biffStream.Read();
                }

                if (header != null || footer != null)
                {
                    HeaderFooter = new HeaderFooter(false, false)
                    {
                        OddHeader = header?.GetValue(Encoding),
                        OddFooter = footer?.GetValue(Encoding),
                    };
                }

                foreach (var biffFormat in biffFormats)
                {
                    var formatString = biffFormat.Value.GetValue(Encoding);
                    Formats.Add(biffFormat.Key, new NumberFormatString(formatString));
                }

                if (mergeCells.Count > 0)
                {
                    MergeCells = mergeCells.ToArray();
                }

                if (FieldCount < maxCellColumn)
                {
                    FieldCount = maxCellColumn;
                }

                if (RowCount < maxRowCount)
                {
                    RowCount = maxRowCount;
                }
            }
        }
Пример #5
0
        private void ReadWorksheetGlobals()
        {
            var biffStream = new XlsBiffStream(Bytes, (int)DataOffset, Workbook.BiffVersion);

            // Check the expected BOF record was found in the BIFF stream
            if (biffStream.BiffVersion == 0 || biffStream.BiffType != BIFFTYPE.Worksheet)
            {
                return;
            }

            XlsBiffHeaderFooterString header = null;
            XlsBiffHeaderFooterString footer = null;

            // Handle when dimensions report less columns than used by cell records.
            int maxCellColumn = 0;
            Dictionary <int, bool> previousBlocksObservedRows = new Dictionary <int, bool>();
            Dictionary <int, bool> observedRows = new Dictionary <int, bool>();

            XlsBiffRecord rec = biffStream.Read();

            while (rec != null && !(rec is XlsBiffEof))
            {
                if (rec is XlsBiffDimensions dims)
                {
                    FieldCount = dims.LastColumn;
                }

                if (rec.Id == BIFFRECORDTYPE.RECORD1904)
                {
                    IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1;
                }

                if (rec.Id == BIFFRECORDTYPE.XF_V2 || rec.Id == BIFFRECORDTYPE.XF_V3 || rec.Id == BIFFRECORDTYPE.XF_V4)
                {
                    ExtendedFormats.Add(rec);
                }

                if (rec.Id == BIFFRECORDTYPE.FORMAT)
                {
                    var fmt = (XlsBiffFormatString)rec;
                    if (Workbook.BiffVersion >= 5)
                    {
                        // fmt.Index exists on BIFF5+ only
                        Formats.Add(fmt.Index, fmt);
                    }
                    else
                    {
                        Formats.Add((ushort)Formats.Count, fmt);
                    }
                }

                if (rec.Id == BIFFRECORDTYPE.FORMAT_V23)
                {
                    var fmt = (XlsBiffFormatString)rec;
                    Formats.Add((ushort)Formats.Count, fmt);
                }

                if (rec.Id == BIFFRECORDTYPE.CODEPAGE)
                {
                    var codePage = (XlsBiffSimpleValueRecord)rec;
                    Encoding = EncodingHelper.GetEncoding(codePage.Value);
                }

                if (rec.Id == BIFFRECORDTYPE.HEADER && rec.RecordSize > 0)
                {
                    header = (XlsBiffHeaderFooterString)rec;
                }

                if (rec.Id == BIFFRECORDTYPE.FOOTER && rec.RecordSize > 0)
                {
                    footer = (XlsBiffHeaderFooterString)rec;
                }

                if (rec.Id == BIFFRECORDTYPE.CODENAME)
                {
                    var codeName = (XlsBiffCodeName)rec;
                    CodeName = codeName.GetValue(Encoding);
                }

                if (!RowContentInMultipleBlocks && rec is XlsBiffDbCell)
                {
                    foreach (int row in observedRows.Keys)
                    {
                        previousBlocksObservedRows[row] = true;
                    }

                    observedRows.Clear();
                }

                if (rec is XlsBiffBlankCell cell)
                {
                    maxCellColumn = Math.Max(maxCellColumn, cell.ColumnIndex + 1);

                    if (!RowContentInMultipleBlocks)
                    {
                        if (previousBlocksObservedRows.ContainsKey(cell.RowIndex))
                        {
                            RowContentInMultipleBlocks = true;
                            previousBlocksObservedRows.Clear();
                            observedRows.Clear();
                        }

                        observedRows[cell.RowIndex] = true;
                    }
                }

                rec = biffStream.Read();
            }

            if (header != null || footer != null)
            {
                HeaderFooter = new HeaderFooter(false, false)
                {
                    OddHeader = header?.GetValue(Encoding),
                    OddFooter = footer?.GetValue(Encoding),
                };
            }

            if (FieldCount < maxCellColumn)
            {
                FieldCount = maxCellColumn;
            }
        }
Пример #6
0
        private void ReadWorkbookGlobals(XlsBiffStream biffStream)
        {
            XlsBiffRecord rec;
            var           biffFormats = new Dictionary <ushort, XlsBiffFormatString>();

            while ((rec = biffStream.Read()) != null && rec.Id != BIFFRECORDTYPE.EOF)
            {
                switch (rec.Id)
                {
                case BIFFRECORDTYPE.INTERFACEHDR:
                    InterfaceHdr = (XlsBiffInterfaceHdr)rec;
                    break;

                case BIFFRECORDTYPE.BOUNDSHEET:
                    XlsBiffBoundSheet sheet = (XlsBiffBoundSheet)rec;

                    if (sheet.Type != XlsBiffBoundSheet.SheetType.Worksheet)
                    {
                        break;
                    }

                    Sheets.Add(sheet);
                    break;

                case BIFFRECORDTYPE.MMS:
                    Mms = rec;
                    break;

                case BIFFRECORDTYPE.COUNTRY:
                    Country = rec;
                    break;

                case BIFFRECORDTYPE.CODEPAGE:
                    // [MS-XLS 2.4.52 CodePage] An unsigned integer that specifies the workbook’s code page.The value MUST be one
                    // of the code page values specified in [CODEPG] or the special value 1200, which means that the
                    // workbook is Unicode.
                    CodePage = (XlsBiffSimpleValueRecord)rec;
                    Encoding = EncodingHelper.GetEncoding(CodePage.Value);
                    break;

                case BIFFRECORDTYPE.FONT:
                case BIFFRECORDTYPE.FONT_V34:
                    Fonts.Add(rec);
                    break;

                case BIFFRECORDTYPE.FORMAT_V23:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    biffFormats.Add((ushort)biffFormats.Count, fmt);
                }

                break;

                case BIFFRECORDTYPE.FORMAT:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    biffFormats.Add(fmt.Index, fmt);
                }

                break;

                case BIFFRECORDTYPE.XF:
                case BIFFRECORDTYPE.XF_V4:
                case BIFFRECORDTYPE.XF_V3:
                case BIFFRECORDTYPE.XF_V2:
                    ExtendedFormats.Add((XlsBiffXF)rec);
                    break;

                case BIFFRECORDTYPE.SST:
                    SST = (XlsBiffSST)rec;
                    SST.ReadStrings(biffStream);
                    break;

                case BIFFRECORDTYPE.CONTINUE:
                    break;

                case BIFFRECORDTYPE.EXTSST:
                    ExtSST = rec;
                    break;

                case BIFFRECORDTYPE.PASSWORD:
                    break;

                case BIFFRECORDTYPE.PROTECT:
                case BIFFRECORDTYPE.PROT4REVPASSWORD:
                    // IsProtected
                    break;

                case BIFFRECORDTYPE.RECORD1904:
                    IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1;
                    break;

                default:
                    break;
                }
            }

            foreach (var biffFormat in biffFormats)
            {
                var formatString = biffFormat.Value.GetValue(Encoding);
                Formats.Add(biffFormat.Key, new NumberFormatString(formatString));
            }
        }
Пример #7
0
        private void ReadWorkbookGlobals(XlsBiffStream biffStream)
        {
            bool          sst = false;
            XlsBiffRecord rec;

            while ((rec = biffStream.Read()) != null)
            {
                switch (rec.Id)
                {
                case BIFFRECORDTYPE.INTERFACEHDR:
                    InterfaceHdr = (XlsBiffInterfaceHdr)rec;
                    break;

                case BIFFRECORDTYPE.BOUNDSHEET:
                    XlsBiffBoundSheet sheet = (XlsBiffBoundSheet)rec;

                    if (sheet.Type != XlsBiffBoundSheet.SheetType.Worksheet)
                    {
                        break;
                    }

                    Sheets.Add(sheet);
                    break;

                case BIFFRECORDTYPE.MMS:
                    Mms = rec;
                    break;

                case BIFFRECORDTYPE.COUNTRY:
                    Country = rec;
                    break;

                case BIFFRECORDTYPE.CODEPAGE:
                    // [MS-XLS 2.4.52 CodePage] An unsigned integer that specifies the workbook’s code page.The value MUST be one
                    // of the code page values specified in [CODEPG] or the special value 1200, which means that the
                    // workbook is Unicode.
                    CodePage = (XlsBiffSimpleValueRecord)rec;
                    Encoding = EncodingHelper.GetEncoding(CodePage.Value);
                    break;

                case BIFFRECORDTYPE.FONT:
                case BIFFRECORDTYPE.FONT_V34:
                    Fonts.Add(rec);
                    break;

                case BIFFRECORDTYPE.FORMAT_V23:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    Formats.Add((ushort)Formats.Count, fmt);
                }

                break;

                case BIFFRECORDTYPE.FORMAT:
                {
                    var fmt = (XlsBiffFormatString)rec;
                    Formats.Add(fmt.Index, fmt);
                }

                break;

                case BIFFRECORDTYPE.XF:
                case BIFFRECORDTYPE.XF_V4:
                case BIFFRECORDTYPE.XF_V3:
                case BIFFRECORDTYPE.XF_V2:
                    ExtendedFormats.Add(rec);
                    break;

                case BIFFRECORDTYPE.SST:
                    SST = (XlsBiffSST)rec;
                    sst = true;
                    break;

                case BIFFRECORDTYPE.CONTINUE:
                    if (!sst)
                    {
                        break;
                    }
                    XlsBiffContinue contSST = (XlsBiffContinue)rec;
                    SST.Append(contSST);
                    break;

                case BIFFRECORDTYPE.EXTSST:
                    ExtSST = rec;
                    sst    = false;
                    break;

                case BIFFRECORDTYPE.PASSWORD:
                    break;

                case BIFFRECORDTYPE.PROTECT:
                case BIFFRECORDTYPE.PROT4REVPASSWORD:
                    // IsProtected
                    break;

                case BIFFRECORDTYPE.RECORD1904:
                    IsDate1904 = ((XlsBiffSimpleValueRecord)rec).Value == 1;
                    break;

                case BIFFRECORDTYPE.EOF:
                    SST?.ReadStrings();
                    return;

                default:
                    continue;
                }
            }
        }