public static List<Record> Encode(Worksheet worksheet, SharedResource sharedResource)
        {
            List<Record> records = new List<Record>();
            BOF bof = new BOF();
            bof.BIFFversion = 0x0600; //0600H = BIFF8
            bof.StreamType = StreamType.Worksheet;
            bof.BuildID = 3515;
            bof.BuildYear = 1996;
            bof.RequiredExcelVersion = 6;
            records.Add(bof);

            foreach (KeyValuePair<Pair<UInt16, UInt16>, UInt16> colWidth in worksheet.Cells.ColumnWidth)
            {
                COLINFO colInfo = new COLINFO();
                colInfo.FirstColIndex = colWidth.Key.Left;
                colInfo.LastColIndex = colWidth.Key.Right;
                colInfo.Width = colWidth.Value;
                records.Add(colInfo);
            }

            DIMENSIONS dimensions = new DIMENSIONS();
            if (worksheet.Cells.Rows.Count > 0)
            {
                dimensions.FirstRow = worksheet.Cells.FirstRowIndex;
                dimensions.FirstColumn = (Int16)worksheet.Cells.FirstColIndex;
                dimensions.LastRow = worksheet.Cells.LastRowIndex + 1;
                dimensions.LastColumn = (Int16)(worksheet.Cells.LastColIndex + 1);
            }
            records.Add(dimensions);

            // each Row Block contains 32 consecutive rows
            List<Record> rowblock = new List<Record>(32);
            List<Record> cellblock = new List<Record>();
            for (int rowIndex = dimensions.FirstRow; rowIndex < dimensions.LastRow; rowIndex++)
            {
                if (worksheet.Cells.Rows.ContainsKey(rowIndex))
                {
                    Row sheetRow = worksheet.Cells.Rows[rowIndex];

                    ROW biffRow = new ROW();
                    biffRow.RowIndex = (UInt16)rowIndex;
                    biffRow.FirstColIndex = (UInt16)sheetRow.FirstColIndex;
                    biffRow.LastColIndex = (UInt16)(sheetRow.LastColIndex + 1);
                    biffRow.RowHeight = sheetRow.Height;
                    biffRow.Flags = 0x0F0100; // defaul value 0x0100
                    rowblock.Add(biffRow);

                    for (int colIndex = sheetRow.FirstColIndex; colIndex <= sheetRow.LastColIndex; colIndex++)
                    {
                        Cell cell = sheetRow.GetCell(colIndex);
                        if (cell != Cell.EmptyCell && cell.Value != null)
                        {
                            CellValue cellRecord = EncodeCell(cell, sharedResource);
                            cellRecord.RowIndex = (UInt16)rowIndex;
                            cellRecord.ColIndex = (UInt16)colIndex;
                            cellRecord.XFIndex = (UInt16)sharedResource.GetXFIndex(cell.CellFormat);
                            cellblock.Add(cellRecord);
                        }
                    }

                    if (rowblock.Count == 32)
                    {
                        records.AddRange(rowblock);
                        records.AddRange(cellblock);

                        rowblock.Clear();
                        cellblock.Clear();
                    }
                }
            }

            if (rowblock.Count > 0)
            {
                records.AddRange(rowblock);
                records.AddRange(cellblock);
            }

            if (worksheet.Pictures.Count > 0)
            {
                records.Add(EncodePictures(worksheet.Pictures, sharedResource, worksheet));
                for (ushort id = 1; id <= worksheet.Pictures.Count; id++)
                {
                    OBJ obj = new OBJ();
                    CommonObjectData objData = new CommonObjectData();
                    objData.ObjectID = id;
                    objData.ObjectType = 8;
                    objData.OptionFlags = 24593;
                    obj.SubRecords.Add(objData);
                    obj.SubRecords.Add(new End());
                    records.Add(obj);
                }
            }

            EOF eof = new EOF();
            records.Add(eof);
            return records;
        }
        public static List <Record> Encode(Worksheet worksheet, SharedResource sharedResource)
        {
            List <Record> list = new List <Record>();

            list.Add(new BOF
            {
                BIFFversion          = 1536,
                StreamType           = 16,
                BuildID              = 3515,
                BuildYear            = 1996,
                RequiredExcelVersion = 6u
            });
            foreach (KeyValuePair <Pair <ushort, ushort>, ushort> current in worksheet.Cells.ColumnWidth)
            {
                list.Add(new COLINFO
                {
                    FirstColIndex = current.Key.Left,
                    LastColIndex  = current.Key.Right,
                    Width         = current.Value
                });
            }
            DIMENSIONS dIMENSIONS = new DIMENSIONS();

            checked
            {
                if (worksheet.Cells.Rows.Count > 0)
                {
                    dIMENSIONS.FirstRow    = worksheet.Cells.FirstRowIndex;
                    dIMENSIONS.FirstColumn = (short)worksheet.Cells.FirstColIndex;
                    dIMENSIONS.LastRow     = worksheet.Cells.LastRowIndex + 1;
                    dIMENSIONS.LastColumn  = (short)(worksheet.Cells.LastColIndex + 1);
                }
                list.Add(dIMENSIONS);
                List <Record> list2 = new List <Record>(32);
                List <Record> list3 = new List <Record>();
                for (int i = dIMENSIONS.FirstRow; i < dIMENSIONS.LastRow; i++)
                {
                    if (worksheet.Cells.Rows.ContainsKey(i))
                    {
                        Row row = worksheet.Cells.Rows[i];
                        list2.Add(new ROW
                        {
                            RowIndex      = (ushort)i,
                            FirstColIndex = (ushort)row.FirstColIndex,
                            LastColIndex  = (ushort)(row.LastColIndex + 1),
                            RowHeight     = row.Height,
                            Flags         = 983296u
                        });
                        for (int j = row.FirstColIndex; j <= row.LastColIndex; j++)
                        {
                            Cell cell = row.GetCell(j);
                            if (cell != Cell.EmptyCell && cell.Value != null)
                            {
                                CellValue cellValue = WorkSheetEncoder.EncodeCell(cell, sharedResource);
                                cellValue.RowIndex = (ushort)i;
                                cellValue.ColIndex = (ushort)j;
                                cellValue.XFIndex  = (ushort)sharedResource.GetXFIndex(cell.Format);
                                list3.Add(cellValue);
                            }
                        }
                        if (list2.Count == 32)
                        {
                            list.AddRange(list2);
                            list.AddRange(list3);
                            list2.Clear();
                            list3.Clear();
                        }
                    }
                }
                if (list2.Count > 0)
                {
                    list.AddRange(list2);
                    list.AddRange(list3);
                }
                if (worksheet.Pictures.Count > 0)
                {
                    list.Add(WorkSheetEncoder.EncodePictures(worksheet.Pictures, sharedResource, worksheet));
                    ushort num = 1;
                    while ((int)num <= worksheet.Pictures.Count)
                    {
                        OBJ oBJ = new OBJ();
                        CommonObjectData commonObjectData = new CommonObjectData();
                        commonObjectData.ObjectID    = num;
                        commonObjectData.ObjectType  = 8;
                        commonObjectData.OptionFlags = 24593;
                        oBJ.SubRecords.Add(commonObjectData);
                        oBJ.SubRecords.Add(new End());
                        list.Add(oBJ);
                        num += 1;
                    }
                }
                EOF item = new EOF();
                list.Add(item);
                return(list);
            }
        }
Example #3
0
        public static List <Record> Encode(Worksheet worksheet, SharedResource sharedResource)
        {
            List <Record> records = new List <Record>();
            BOF           bof     = new BOF();

            bof.BIFFversion          = 0x0600; //0600H = BIFF8
            bof.StreamType           = StreamType.Worksheet;
            bof.BuildID              = 3515;
            bof.BuildYear            = 1996;
            bof.RequiredExcelVersion = 6;
            records.Add(bof);

            foreach (KeyValuePair <Pair <UInt16, UInt16>, UInt16> colWidth in worksheet.Cells.ColumnWidth)
            {
                COLINFO colInfo = new COLINFO();
                colInfo.FirstColIndex = colWidth.Key.Left;
                colInfo.LastColIndex  = colWidth.Key.Right;
                colInfo.Width         = colWidth.Value;
                records.Add(colInfo);
            }

            DIMENSIONS dimensions = new DIMENSIONS();

            if (worksheet.Cells.Rows.Count > 0)
            {
                dimensions.FirstRow    = worksheet.Cells.FirstRowIndex;
                dimensions.FirstColumn = (Int16)worksheet.Cells.FirstColIndex;
                dimensions.LastRow     = worksheet.Cells.LastRowIndex + 1;
                dimensions.LastColumn  = (Int16)(worksheet.Cells.LastColIndex + 1);
            }
            records.Add(dimensions);

            // each Row Block contains 32 consecutive rows
            List <Record> rowblock  = new List <Record>(32);
            List <Record> cellblock = new List <Record>();

            for (int rowIndex = dimensions.FirstRow; rowIndex < dimensions.LastRow; rowIndex++)
            {
                if (worksheet.Cells.Rows.ContainsKey(rowIndex))
                {
                    Row sheetRow = worksheet.Cells.Rows[rowIndex];

                    ROW biffRow = new ROW();
                    biffRow.RowIndex      = (UInt16)rowIndex;
                    biffRow.FirstColIndex = (UInt16)sheetRow.FirstColIndex;
                    biffRow.LastColIndex  = (UInt16)(sheetRow.LastColIndex + 1);
                    biffRow.RowHeight     = sheetRow.Height;
                    biffRow.Flags         = 0x0F0100; // defaul value 0x0100
                    rowblock.Add(biffRow);

                    for (int colIndex = sheetRow.FirstColIndex; colIndex <= sheetRow.LastColIndex; colIndex++)
                    {
                        Cell cell = sheetRow.GetCell(colIndex);
                        if (cell != Cell.EmptyCell && cell.Value != null)
                        {
                            CellValue cellRecord = EncodeCell(cell, sharedResource);
                            cellRecord.RowIndex = (UInt16)rowIndex;
                            cellRecord.ColIndex = (UInt16)colIndex;
                            cellRecord.XFIndex  = (UInt16)sharedResource.GetXFIndex(cell.Format);
                            cellblock.Add(cellRecord);
                        }
                    }

                    if (rowblock.Count == 32)
                    {
                        records.AddRange(rowblock);
                        records.AddRange(cellblock);

                        rowblock.Clear();
                        cellblock.Clear();
                    }
                }
            }

            if (rowblock.Count > 0)
            {
                records.AddRange(rowblock);
                records.AddRange(cellblock);
            }

            if (worksheet.Pictures.Count > 0)
            {
                records.Add(EncodePictures(worksheet.Pictures, sharedResource, worksheet));
                for (ushort id = 1; id <= worksheet.Pictures.Count; id++)
                {
                    OBJ obj = new OBJ();
                    CommonObjectData objData = new CommonObjectData();
                    objData.ObjectID    = id;
                    objData.ObjectType  = 8;
                    objData.OptionFlags = 24593;
                    obj.SubRecords.Add(objData);
                    obj.SubRecords.Add(new End());
                    records.Add(obj);
                }
            }

            EOF eof = new EOF();

            records.Add(eof);
            return(records);
        }