コード例 #1
0
ファイル: TestRecordFactory.cs プロジェクト: thinhmascot/NPOI
        public void TestBasicRecordConstruction()
        {
            short recType = BOFRecord.sid;

            byte[] data =
            {
                0, 6, 5, 0, unchecked ((byte)-2), 28, unchecked ((byte)-51), 7, unchecked ((byte)-55), 64, 0, 0, 6, 1, 0, 0
            };
            Record[] record = RecordFactory.CreateRecord(TestcaseRecordInputStream.Create(recType, data));

            Assert.AreEqual(typeof(BOFRecord).Name,
                            record[0].GetType().Name);
            BOFRecord bofRecord = (BOFRecord)record[0];

            Assert.AreEqual(7422, bofRecord.Build);
            Assert.AreEqual(1997, bofRecord.BuildYear);
            Assert.AreEqual(16585, bofRecord.HistoryBitMask);
            Assert.AreEqual(20, bofRecord.RecordSize);
            Assert.AreEqual(262, bofRecord.RequiredVersion);
            Assert.AreEqual(2057, bofRecord.Sid);
            Assert.AreEqual(5, bofRecord.Type);
            Assert.AreEqual(1536, bofRecord.Version);
            recType = MMSRecord.sid;
            //size = 2;
            data = new byte[]
            {
                0, 0
            };
            record = RecordFactory.CreateRecord(TestcaseRecordInputStream.Create(recType, data));
            Assert.AreEqual(typeof(MMSRecord).Name,
                            record[0].GetType().Name);
            MMSRecord mmsRecord = (MMSRecord)record[0];

            Assert.AreEqual(0, mmsRecord.GetAddMenuCount());
            Assert.AreEqual(0, mmsRecord.GetDelMenuCount());
            Assert.AreEqual(6, mmsRecord.RecordSize);
            Assert.AreEqual(193, mmsRecord.Sid);
        }
コード例 #2
0
        /**
         * Writes out this sheet to the output file.  First it writes out
         * the standard workbook information required by excel, before calling
         * the write method on each sheet individually
         *
         * @exception IOException
         */
        public override void write()
        {
            // Perform some preliminary sheet check before we start writing out
            // the workbook
            WritableSheetImpl wsi = null;
            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                wsi = (WritableSheetImpl)getSheet(i);

                // Check the merged records.  This has to be done before the
                // globals are written out because some more XF formats might be created
                wsi.checkMergedBorders();

                // Check to see if there are any predefined names
                Range range = wsi.getSettings().getPrintArea();
                if (range != null)
                    {
                    addNameArea(BuiltInName.PRINT_AREA,
                                wsi,
                                range.getTopLeft().getColumn(),
                                range.getTopLeft().getRow(),
                                range.getBottomRight().getColumn(),
                                range.getBottomRight().getRow(),
                                false);
                    }

                // Check to see if print titles by row were set
                Range rangeR = wsi.getSettings().getPrintTitlesRow();
                Range rangeC = wsi.getSettings().getPrintTitlesCol();
                if (rangeR != null && rangeC != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                                wsi,
                                rangeR.getTopLeft().getColumn(),
                                rangeR.getTopLeft().getRow(),
                                rangeR.getBottomRight().getColumn(),
                                rangeR.getBottomRight().getRow(),
                                rangeC.getTopLeft().getColumn(),
                                rangeC.getTopLeft().getRow(),
                                rangeC.getBottomRight().getColumn(),
                                rangeC.getBottomRight().getRow(),
                                false);
                    }
                // Check to see if print titles by row were set
                else if (rangeR != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                              wsi,
                              rangeR.getTopLeft().getColumn(),
                              rangeR.getTopLeft().getRow(),
                              rangeR.getBottomRight().getColumn(),
                              rangeR.getBottomRight().getRow(),
                              false);
                    }
                // Check to see if print titles by column were set
                else if (rangeC != null)
                    {
                    addNameArea(BuiltInName.PRINT_TITLES,
                                wsi,
                                rangeC.getTopLeft().getColumn(),
                                rangeC.getTopLeft().getRow(),
                                rangeC.getBottomRight().getColumn(),
                                rangeC.getBottomRight().getRow(),
                                false);
                    }
                }

            // Rationalize all the XF and number formats
            if (!settings.getRationalizationDisabled())
                rationalize();

            // Write the workbook globals
            BOFRecord bof = new BOFRecord(BOFRecord.workbookGlobals);
            outputFile.write(bof);

            // Must immediatly follow the BOF record
            if (settings.getTemplate())
                {
                // Only write record if we are a template
                TemplateRecord trec = new TemplateRecord();
                outputFile.write(trec);
                }

            InterfaceHeaderRecord ihr = new InterfaceHeaderRecord();
            outputFile.write(ihr);

            MMSRecord mms = new MMSRecord(0, 0);
            outputFile.write(mms);

            InterfaceEndRecord ier = new InterfaceEndRecord();
            outputFile.write(ier);

            WriteAccessRecord wr = new WriteAccessRecord(settings.getWriteAccess());
            outputFile.write(wr);

            CodepageRecord cp = new CodepageRecord();
            outputFile.write(cp);

            DSFRecord dsf = new DSFRecord();
            outputFile.write(dsf);

            if (settings.getExcel9File())
                {
                // Only write record if we are a template
                // We are not excel 2000, should we still set the flag
                Excel9FileRecord e9rec = new Excel9FileRecord();
                outputFile.write(e9rec);
                }

            TabIdRecord tabid = new TabIdRecord(getNumberOfSheets());
            outputFile.write(tabid);

            if (containsMacros)
                {
                ObjProjRecord objproj = new ObjProjRecord();
                outputFile.write(objproj);
                }

            if (buttonPropertySet != null)
                outputFile.write(buttonPropertySet);

            FunctionGroupCountRecord fgcr = new FunctionGroupCountRecord();
            outputFile.write(fgcr);

            // do not support password protected workbooks
            WindowProtectRecord wpr = new WindowProtectRecord
              (settings.getWindowProtected());
            outputFile.write(wpr);

            ProtectRecord pr = new ProtectRecord(wbProtected);
            outputFile.write(pr);

            PasswordRecord pw = new PasswordRecord(null);
            outputFile.write(pw);

            Prot4RevRecord p4r = new Prot4RevRecord(false);
            outputFile.write(p4r);

            Prot4RevPassRecord p4rp = new Prot4RevPassRecord();
            outputFile.write(p4rp);

            // If no sheet is identified as being selected, then select
            // the first one
            bool sheetSelected = false;
            WritableSheetImpl wsheet = null;
            int selectedSheetIndex = 0;
            for (int i = 0; i < getNumberOfSheets() && !sheetSelected; i++)
                {
                wsheet = (WritableSheetImpl)getSheet(i);
                if (wsheet.getSettings().isSelected())
                    {
                    sheetSelected = true;
                    selectedSheetIndex = i;
                    }
                }

            if (!sheetSelected)
                {
                wsheet = (WritableSheetImpl)getSheet(0);
                wsheet.getSettings().setSelected(true);
                selectedSheetIndex = 0;
                }

            Window1Record w1r = new Window1Record(selectedSheetIndex);
            outputFile.write(w1r);

            BackupRecord bkr = new BackupRecord(false);
            outputFile.write(bkr);

            HideobjRecord ho = new HideobjRecord(settings.getHideobj());
            outputFile.write(ho);

            NineteenFourRecord nf = new NineteenFourRecord(false);
            outputFile.write(nf);

            PrecisionRecord pc = new PrecisionRecord(false);
            outputFile.write(pc);

            RefreshAllRecord rar = new RefreshAllRecord(settings.getRefreshAll());
            outputFile.write(rar);

            BookboolRecord bb = new BookboolRecord(true);
            outputFile.write(bb);

            // Write out all the fonts used
            fonts.write(outputFile);

            // Write out the cell formats used within this workbook
            formatRecords.write(outputFile);

            // Write out the palette, if it exists
            if (formatRecords.getPalette() != null)
                outputFile.write(formatRecords.getPalette());

            // Write out the uses elfs record
            UsesElfsRecord uer = new UsesElfsRecord();
            outputFile.write(uer);

            // Write out the boundsheet records.  Keep a handle to each one's
            // position so we can write in the stream offset later
            int[] boundsheetPos = new int[getNumberOfSheets()];
            Sheet sheet = null;

            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                boundsheetPos[i] = outputFile.getPos();
                sheet = getSheet(i);
                BoundsheetRecord br = new BoundsheetRecord(sheet.getName());
                if (sheet.getSettings().isHidden())
                    br.setHidden();

                if (((WritableSheetImpl)sheets[i]).isChartOnly())
                    br.setChartOnly();

                outputFile.write(br);
                }

            if (countryRecord == null)
                {
                CountryCode lang = CountryCode.getCountryCode(settings.getExcelDisplayLanguage());
                if (lang == CountryCode.UNKNOWN)
                    {
                    //logger.warn("Unknown country code " +
                    //            settings.getExcelDisplayLanguage() +
                    //            " using " + CountryCode.USA.getCode());
                    lang = CountryCode.USA;
                    }
                CountryCode region = CountryCode.getCountryCode(settings.getExcelRegionalSettings());
                countryRecord = new CountryRecord(lang, region);
                if (region == CountryCode.UNKNOWN)
                    {
                    //logger.warn("Unknown country code " +
                    //            settings.getExcelDisplayLanguage() +
                    //            " using " + CountryCode.UK.getCode());
                    region = CountryCode.UK;
                    }
                }

            outputFile.write(countryRecord);

            // Write out the names of any add in functions
            if (addInFunctionNames != null && addInFunctionNames.Length > 0)
                {
                // Write out the supbook record
                //      SupbookRecord supbook = new SupbookRecord();
                //      outputFile.write(supbook);

                for (int i = 0; i < addInFunctionNames.Length; i++)
                    {
                    ExternalNameRecord enr = new ExternalNameRecord(addInFunctionNames[i]);
                    outputFile.write(enr);
                    }
                }

            if (xctRecords != null)
                {
                for (int i = 0; i < xctRecords.Length; i++)
                    outputFile.write(xctRecords[i]);
                }

            // Write out the external sheet record, if it exists
            if (externSheet != null)
                {
                //Write out all the supbook records
                for (int i = 0; i < supbooks.Count; i++)
                    {
                    SupbookRecord supbook = (SupbookRecord)supbooks[i];
                    outputFile.write(supbook);
                    }
                outputFile.write(externSheet);
                }

            // Write out the names, if any exists
            if (names != null)
                {
                for (int i = 0; i < names.Count; i++)
                    {
                    NameRecord n = (NameRecord)names[i];
                    outputFile.write(n);
                    }
                }

            // Write out the mso drawing group, if it exists
            if (drawingGroup != null)
                drawingGroup.write(outputFile);

            sharedStrings.write(outputFile);

            EOFRecord eof = new EOFRecord();
            outputFile.write(eof);

            // Write out the sheets
            for (int i = 0; i < getNumberOfSheets(); i++)
                {
                // first go back and modify the offset we wrote out for the
                // boundsheet record
                outputFile.setData(IntegerHelper.getFourBytes(outputFile.getPos()),boundsheetPos[i] + 4);

                wsheet = (WritableSheetImpl)getSheet(i);
                wsheet.write();
                }
        }