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;
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        private static List <Record> EncodeWorkbook(Workbook workbook)
        {
            SharedResource sharedResource = new SharedResource(true);
            List <Record>  book_records   = new List <Record>();
            BOF            bof            = new BOF();

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

            CODEPAGE codepage = new CODEPAGE();

            codepage.CodePageIdentifier = (ushort)Encoding.Unicode.CodePage;
            book_records.Add(codepage);

            WINDOW1 window = new WINDOW1();

            window.WindowWidth       = 16384;
            window.WindowHeight      = 8192;
            window.SelecteWorksheets = 1;
            window.TabBarWidth       = 600;
            window.OptionFlags       = 56;
            book_records.Add(window);

            DATEMODE dateMode = new DATEMODE();

            dateMode.Mode = (Int16)workbook.DateMode;

            if (workbook.DateMode == WorkbookDateMode.Windows)
            {
                sharedResource.BaseDate = Constants.WindowsBaseDate;
            }

            if (workbook.DateMode == WorkbookDateMode.Macintosh)
            {
                sharedResource.BaseDate = Constants.MacintoshBaseDate;
            }

            book_records.Add(dateMode);

            List <List <Record> > all_sheet_records = new List <List <Record> >();

            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                List <Record> sheet_records = WorkSheetEncoder.Encode(worksheet, sharedResource);
                Record.EncodeRecords(sheet_records);
                all_sheet_records.Add(sheet_records);
            }

            book_records.AddRange(sharedResource.FormatRecords.ToArray());
            book_records.AddRange(sharedResource.ExtendedFormats.ToArray());

            List <BOUNDSHEET> boundSheets = new List <BOUNDSHEET>();

            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                BOUNDSHEET boundSheet = new BOUNDSHEET();
                boundSheet.Visibility     = 0; // 00H = Visible
                boundSheet.SheetType      = (byte)SheetType.Worksheet;
                boundSheet.SheetName      = worksheet.Name;
                boundSheet.StreamPosition = 0;
                boundSheets.Add(boundSheet);
                book_records.Add(boundSheet);
            }

            if (sharedResource.Images.Count > 0)
            {
                book_records.Add(EncodeImages(sharedResource.Images));
            }

            Record.EncodeRecords(book_records);
            int sstOffset = Record.CountDataLength(book_records);

            book_records.Add(sharedResource.SharedStringTable);
            book_records.Add(CreateEXTSST(sharedResource.SharedStringTable, sstOffset));

            EOF eof = new EOF();

            book_records.Add(eof);

            Record.EncodeRecords(book_records);
            int dataLength = Record.CountDataLength(book_records);

            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                boundSheets[i].StreamPosition = (uint)dataLength;
                boundSheets[i].Encode();

                int sheet_length = Record.CountDataLength(all_sheet_records[i]);
                dataLength += sheet_length;
            }

            List <Record> all_records = new List <Record>();

            all_records.AddRange(book_records);
            foreach (List <Record> sheet_records in all_sheet_records)
            {
                all_records.AddRange(sheet_records);
            }
            return(all_records);
        }
        private static List<Record> EncodeWorkbook(Workbook workbook)
        {
            SharedResource sharedResource = new SharedResource(true);
            List<Record> book_records = new List<Record>();
            BOF bof = new BOF();
            bof.BIFFversion = 0x0600; //0600H = BIFF8
            bof.StreamType = StreamType.WorkbookGlobals;
            bof.BuildID = 3515;
            bof.BuildYear = 1996;
            bof.RequiredExcelVersion = 6;
            book_records.Add(bof);

            CODEPAGE codepage = new CODEPAGE();
            codepage.CodePageIdentifier = (ushort)Encoding.Unicode.CodePage;
            book_records.Add(codepage);

            WINDOW1 window = new WINDOW1();
            window.WindowWidth = 16384;
            window.WindowHeight = 8192;
            window.SelecteWorksheets = 1;
            window.TabBarWidth = 600;
            window.OptionFlags = 56;
            book_records.Add(window);

            DATEMODE dateMode = new DATEMODE();
            dateMode.Mode = 1;
            sharedResource.BaseDate = DateTime.Parse("1904-01-01");
            book_records.Add(dateMode);

            List<List<Record>> all_sheet_records = new List<List<Record>>();
            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                List<Record> sheet_records = WorkSheetEncoder.Encode(worksheet, sharedResource);
                Record.EncodeRecords(sheet_records);
                all_sheet_records.Add(sheet_records);
            }

            book_records.AddRange(sharedResource.FormatRecords.ToArray());
            book_records.AddRange(sharedResource.ExtendedFormats.ToArray());

            List<BOUNDSHEET> boundSheets = new List<BOUNDSHEET>();
            foreach (Worksheet worksheet in workbook.Worksheets)
            {
                BOUNDSHEET boundSheet = new BOUNDSHEET();
                boundSheet.Visibility = 0; // 00H = Visible
                boundSheet.SheetType = (byte)SheetType.Worksheet;
                boundSheet.SheetName = worksheet.Name;
                boundSheet.StreamPosition = 0;
                boundSheets.Add(boundSheet);
                book_records.Add(boundSheet);
            }

            if (sharedResource.Images.Count > 0)
            {
                book_records.Add(EncodeImages(sharedResource.Images));
            }

            Record.EncodeRecords(book_records);
            int sstOffset = Record.CountDataLength(book_records);

            book_records.Add(sharedResource.SharedStringTable);
            book_records.Add(CreateEXTSST(sharedResource.SharedStringTable, sstOffset));

            EOF eof = new EOF();
            book_records.Add(eof);

            Record.EncodeRecords(book_records);
            int dataLength = Record.CountDataLength(book_records);

            for (int i = 0; i < workbook.Worksheets.Count; i++)
            {
                boundSheets[i].StreamPosition = (uint)dataLength;
                boundSheets[i].Encode();

                int sheet_length = Record.CountDataLength(all_sheet_records[i]);
                dataLength += sheet_length;
            }

            List<Record> all_records = new List<Record>();
            all_records.AddRange(book_records);
            foreach (List<Record> sheet_records in all_sheet_records)
            {
                all_records.AddRange(sheet_records);
            }
            return all_records;
        }
Esempio n. 5
0
        public static Record Read(Stream stream)
        {
            Record record = Record.ReadBase(stream);
            ushort type   = record.Type;
            Record result;

            if (type <= 146)
            {
                if (type <= 49)
                {
                    if (type <= 13)
                    {
                        if (type == 6)
                        {
                            result = new FORMULA(record);
                            return(result);
                        }
                        switch (type)
                        {
                        case 10:
                            result = new EOF(record);
                            return(result);

                        case 12:
                            result = new CALCCOUNT(record);
                            return(result);

                        case 13:
                            result = new CALCMODE(record);
                            return(result);
                        }
                    }
                    else
                    {
                        if (type == 34)
                        {
                            result = new DATEMODE(record);
                            return(result);
                        }
                        if (type == 41)
                        {
                            result = new BOTTOMMARGIN(record);
                            return(result);
                        }
                        if (type == 49)
                        {
                            result = new FONT(record);
                            return(result);
                        }
                    }
                }
                else
                {
                    if (type <= 93)
                    {
                        switch (type)
                        {
                        case 60:
                            result = new CONTINUE(record);
                            return(result);

                        case 61:
                            result = new WINDOW1(record);
                            return(result);

                        case 62:
                        case 63:
                        case 65:
                            break;

                        case 64:
                            result = new BACKUP(record);
                            return(result);

                        case 66:
                            result = new CODEPAGE(record);
                            return(result);

                        default:
                            if (type == 85)
                            {
                                result = new DEFCOLWIDTH(record);
                                return(result);
                            }
                            if (type == 93)
                            {
                                result = new OBJ(record);
                                return(result);
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (type == 125)
                        {
                            result = new COLINFO(record);
                            return(result);
                        }
                        if (type == 133)
                        {
                            result = new BOUNDSHEET(record);
                            return(result);
                        }
                        if (type == 146)
                        {
                            result = new PALETTE(record);
                            return(result);
                        }
                    }
                }
            }
            else
            {
                if (type <= 237)
                {
                    if (type <= 190)
                    {
                        if (type == 153)
                        {
                            result = new STANDARDWIDTH(record);
                            return(result);
                        }
                        switch (type)
                        {
                        case 189:
                            result = new MULRK(record);
                            return(result);

                        case 190:
                            result = new MULBLANK(record);
                            return(result);
                        }
                    }
                    else
                    {
                        switch (type)
                        {
                        case 214:
                            result = new RSTRING(record);
                            return(result);

                        case 215:
                            result = new DBCELL(record);
                            return(result);

                        case 216:
                        case 217:
                            break;

                        case 218:
                            result = new BOOKBOOL(record);
                            return(result);

                        default:
                            if (type == 224)
                            {
                                result = new XF(record);
                                return(result);
                            }
                            switch (type)
                            {
                            case 233:
                                result = new BITMAP(record);
                                return(result);

                            case 235:
                                result = new MSODRAWINGGROUP(record);
                                return(result);

                            case 236:
                                result = new MSODRAWING(record);
                                return(result);

                            case 237:
                                result = new MSODRAWINGSELECTION(record);
                                return(result);
                            }
                            break;
                        }
                    }
                }
                else
                {
                    if (type <= 545)
                    {
                        switch (type)
                        {
                        case 252:
                            result = new SST(record);
                            return(result);

                        case 253:
                            result = new LABELSST(record);
                            return(result);

                        case 254:
                            break;

                        case 255:
                            result = new EXTSST(record);
                            return(result);

                        default:
                            switch (type)
                            {
                            case 512:
                                result = new DIMENSIONS(record);
                                return(result);

                            case 513:
                                result = new BLANK(record);
                                return(result);

                            case 514:
                            case 516:
                            case 518:
                                break;

                            case 515:
                                result = new NUMBER(record);
                                return(result);

                            case 517:
                                result = new BOOLERR(record);
                                return(result);

                            case 519:
                                result = new STRING(record);
                                return(result);

                            case 520:
                                result = new ROW(record);
                                return(result);

                            default:
                                if (type == 545)
                                {
                                    result = new ARRAY(record);
                                    return(result);
                                }
                                break;
                            }
                            break;
                        }
                    }
                    else
                    {
                        if (type == 638)
                        {
                            result = new RK(record);
                            return(result);
                        }
                        if (type == 1054)
                        {
                            result = new FORMAT(record);
                            return(result);
                        }
                        if (type == 2057)
                        {
                            result = new BOF(record);
                            return(result);
                        }
                    }
                }
            }
            result = record;
            return(result);
        }