Exemplo n.º 1
0
        private static List <Record> EncodeWorkbook(Workbook workbook)
        {
            SharedResource sharedResource = new SharedResource(true);
            List <Record>  list           = new List <Record>();

            list.Add(new BOF
            {
                BIFFversion          = 1536,
                StreamType           = 5,
                BuildID              = 3515,
                BuildYear            = 1996,
                RequiredExcelVersion = 6u
            });
            checked
            {
                list.Add(new CODEPAGE
                {
                    CodePageIdentifier = (ushort)Encoding.Unicode.CodePage
                });
                list.Add(new WINDOW1
                {
                    WindowWidth       = 16384,
                    WindowHeight      = 8192,
                    SelecteWorksheets = 1,
                    TabBarWidth       = 600,
                    OptionFlags       = 56
                });
                DATEMODE dATEMODE = new DATEMODE();
                dATEMODE.Mode           = 1;
                sharedResource.BaseDate = DateTime.Parse("1904-01-01");
                list.Add(dATEMODE);
                List <List <Record> > list2 = new List <List <Record> >();
                foreach (Worksheet current in workbook.Worksheets)
                {
                    List <Record> list3 = WorkSheetEncoder.Encode(current, sharedResource);
                    Record.EncodeRecords(list3);
                    list2.Add(list3);
                }
                list.AddRange(sharedResource.FormatRecords.ToArray());
                list.AddRange(sharedResource.ExtendedFormats.ToArray());
                List <BOUNDSHEET> list4 = new List <BOUNDSHEET>();
                foreach (Worksheet current in workbook.Worksheets)
                {
                    BOUNDSHEET bOUNDSHEET = new BOUNDSHEET();
                    bOUNDSHEET.Visibility     = 0;
                    bOUNDSHEET.SheetType      = 0;
                    bOUNDSHEET.SheetName      = current.Name;
                    bOUNDSHEET.StreamPosition = 0u;
                    list4.Add(bOUNDSHEET);
                    list.Add(bOUNDSHEET);
                }
                if (sharedResource.Images.Count > 0)
                {
                    list.Add(WorkbookEncoder.EncodeImages(sharedResource.Images));
                }
                Record.EncodeRecords(list);
                int sstOffset = Record.CountDataLength(list);
                list.Add(sharedResource.SharedStringTable);
                list.Add(WorkbookEncoder.CreateEXTSST(sharedResource.SharedStringTable, sstOffset));
                EOF item = new EOF();
                list.Add(item);
                Record.EncodeRecords(list);
                int num = Record.CountDataLength(list);
                for (int i = 0; i < workbook.Worksheets.Count; i++)
                {
                    list4[i].StreamPosition = (uint)num;
                    list4[i].Encode();
                    int num2 = Record.CountDataLength(list2[i]);
                    num += num2;
                }
                List <Record> list5 = new List <Record>();
                list5.AddRange(list);
                foreach (List <Record> list3 in list2)
                {
                    list5.AddRange(list3);
                }
                return(list5);
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo 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);
        }