コード例 #1
0
ファイル: TestAreaReference.cs プロジェクト: yesonsik/npoi
        public void TestDiscontinousReference()
        {
            Stream                 is1      = HSSFTestDataSamples.OpenSampleFileStream("44167.xls");
            HSSFWorkbook           wb       = new HSSFWorkbook(is1);
            InternalWorkbook       workbook = wb.Workbook;
            HSSFEvaluationWorkbook eb       = HSSFEvaluationWorkbook.Create(wb);

            Assert.AreEqual(1, wb.NumberOfNames);
            String sheetName = "Tabelle1";
            String rawRefA   = "$C$10:$C$14";
            String rawRefB   = "$C$16:$C$18";
            String refA      = sheetName + "!" + rawRefA;
            String refB      = sheetName + "!" + rawRefB;
            String ref1      = refA + "," + refB;

            // Check the low level record
            NameRecord nr = workbook.GetNameRecord(0);

            Assert.IsNotNull(nr);
            Assert.AreEqual("test", nr.NameText);

            Ptg[] def = nr.NameDefinition;
            Assert.AreEqual(4, def.Length);

            MemFuncPtg ptgA = (MemFuncPtg)def[0];
            Area3DPtg  ptgB = (Area3DPtg)def[1];
            Area3DPtg  ptgC = (Area3DPtg)def[2];
            UnionPtg   ptgD = (UnionPtg)def[3];

            Assert.AreEqual("", ptgA.ToFormulaString());
            Assert.AreEqual(refA, ptgB.ToFormulaString(eb));
            Assert.AreEqual(refB, ptgC.ToFormulaString(eb));
            Assert.AreEqual(",", ptgD.ToFormulaString());

            Assert.AreEqual(ref1, NPOI.HSSF.Model.HSSFFormulaParser.ToFormulaString(wb, nr.NameDefinition));

            // Check the high level definition
            int idx = wb.GetNameIndex("test");

            Assert.AreEqual(0, idx);
            NPOI.SS.UserModel.IName aNamedCell = wb.GetNameAt(idx);

            // Should have 2 references
            Assert.AreEqual(ref1, aNamedCell.RefersToFormula);

            // Check the parsing of the reference into cells
            Assert.IsFalse(AreaReference.IsContiguous(aNamedCell.RefersToFormula));
            AreaReference[] arefs = AreaReference.GenerateContiguous(aNamedCell.RefersToFormula);
            Assert.AreEqual(2, arefs.Length);
            Assert.AreEqual(refA, arefs[0].FormatAsString());
            Assert.AreEqual(refB, arefs[1].FormatAsString());

            for (int i = 0; i < arefs.Length; i++)
            {
                AreaReference ar = arefs[i];
                ConfirmResolveCellRef(wb, ar.FirstCell);
                ConfirmResolveCellRef(wb, ar.LastCell);
            }
        }
コード例 #2
0
ファイル: HSSFWorkbook.cs プロジェクト: missxiaohuang/Weekly
            /**
     * given a POI POIFSFileSystem object, and a specific directory
     *  within it, read in its Workbook and populate the high and
     *  low level models.  If you're reading in a workbook...start here.
     *
     * @param directory the POI filesystem directory to process from
     * @param preserveNodes whether to preseve other nodes, such as
     *        macros.  This takes more memory, so only say yes if you
     *        need to. If set, will store all of the POIFSFileSystem
     *        in memory
     * @see org.apache.poi.poifs.filesystem.POIFSFileSystem
     * @exception IOException if the stream cannot be read
     */
        public HSSFWorkbook(DirectoryNode directory, bool preserveNodes):base(directory)
        {

            String workbookName = GetWorkbookDirEntryName(directory);

            this.preserveNodes = preserveNodes;

            // If we're not preserving nodes, don't track the
            //  POIFS any more
            if (!preserveNodes)
            {
                this.directory = null;
            }

            _sheets = new List<HSSFSheet>(INITIAL_CAPACITY);
            names = new List<HSSFName>(INITIAL_CAPACITY);

            // Grab the data from the workbook stream, however
            //  it happens to be spelled.
            Stream stream = directory.CreatePOIFSDocumentReader(workbookName);


            List<Record> records = RecordFactory.CreateRecords(stream);

            workbook = InternalWorkbook.CreateWorkbook(records);
            SetPropertiesFromWorkbook(workbook);
            int recOffset = workbook.NumRecords;

            // Convert all LabelRecord records to LabelSSTRecord
            ConvertLabelRecords(records, recOffset);
            RecordStream rs = new RecordStream(records, recOffset);
            while (rs.HasNext())
            {
                InternalSheet sheet = InternalSheet.CreateSheet(rs);
                _sheets.Add(new HSSFSheet(this, sheet));
            }

            for (int i = 0; i < workbook.NumNames; ++i)
            {
                NameRecord nameRecord = workbook.GetNameRecord(i);
                HSSFName name = new HSSFName(this, workbook.GetNameRecord(i), workbook.GetNameCommentRecord(nameRecord));
                names.Add(name);
            }
        }