Esempio n. 1
0
        //private static POILogger log = POILogFactory.GetLogger(typeof(HSSFSheet));

        /// <summary>
        /// Creates new HSSFSheet - called by HSSFWorkbook to create a _sheet from
        /// scratch. You should not be calling this from application code (its protected anyhow).
        /// </summary>
        /// <param name="workbook">The HSSF Workbook object associated with the _sheet.</param>
        /// <see cref="NPOI.HSSF.UserModel.HSSFWorkbook.CreateSheet()"/>
        public HSSFSheet(HSSFWorkbook workbook)
        {
            _sheet = InternalSheet.CreateSheet();
            rows = new Dictionary<int, NPOI.SS.UserModel.IRow>();
            this._workbook = workbook;
            this.book = workbook.Workbook;
        }
Esempio n. 2
0
        private bool movedBuiltins = false;  // Flag to see if need to
        // Check the built in list
        // or if the regular list
        // has all entries.

        /// <summary>
        /// Construncts a new data formatter.  It takes a workbook to have
        /// access to the workbooks format records.
        /// </summary>
        /// <param name="workbook">the workbook the formats are tied to.</param>
        public HSSFDataFormat(InternalWorkbook workbook)
        {
            this.workbook = workbook;
            IEnumerator i = workbook.Formats.GetEnumerator();
            while (i.MoveNext())
            {
                FormatRecord r = (FormatRecord)i.Current;
                for (int j = formats.Count; formats.Count <= r.IndexCode; j++)
                {
                    formats.Add(null);
                }
                formats[r.IndexCode] = r.FormatString;
            }
        }
Esempio n. 3
0
        /**
         * Read support  for low level
         * API.  Pass in an array of Record objects, A Workbook
         * object is constructed and passed back with all of its initialization Set
         * to the passed in records and references to those records held. Unlike Sheet
         * workbook does not use an offset (its assumed to be 0) since its first in a file.
         * If you need an offset then construct a new array with a 0 offset or Write your
         * own ;-p.
         *
         * @param recs an array of Record objects
         * @return Workbook object
         */
        public static InternalWorkbook CreateWorkbook(List<Record> recs)
        {
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "Workbook (Readfile) Created with reclen=",
            //           recs.Count);
            InternalWorkbook retval = new InternalWorkbook();
            List<Record> records = new List<Record>(recs.Count / 3);
            retval.records.Records=records;

            int k;
            for (k = 0; k < recs.Count; k++)
            {
                Record rec = (Record)recs[k];

                if (rec.Sid == EOFRecord.sid)
                {
                    records.Add(rec);
                    //if (log.Check(POILogger.DEBUG))
                    //    log.Log(DEBUG, "found workbook eof record at " + k);
                    break;
                }
                switch (rec.Sid)
                {

                    case BoundSheetRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found boundsheet record at " + k);
                        retval.boundsheets.Add((BoundSheetRecord)rec);
                        retval.records.Bspos = k;
                        break;

                    case SSTRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found sst record at " + k);
                        retval.sst = (SSTRecord)rec;
                        break;

                    case FontRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found font record at " + k);
                        retval.records.Fontpos = k;
                        retval.numfonts++;
                        break;

                    case ExtendedFormatRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found XF record at " + k);
                        retval.records.Xfpos = k;
                        retval.numxfs++;
                        break;

                    case TabIdRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found tabid record at " + k);
                        retval.records.Tabpos = k;
                        break;

                    case ProtectRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found protect record at " + k);
                        retval.records.Protpos = k;
                        break;

                    case BackupRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found backup record at " + k);
                        retval.records.Backuppos = k;
                        break;
                    case ExternSheetRecord.sid:
                        throw new Exception("Extern sheet is part of LinkTable");
                    case NameRecord.sid:
                    case SupBookRecord.sid:
                        // LinkTable can start with either of these
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found SupBook record at " + k);
                        retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords);
                        k += retval.linkTable.RecordCount - 1;
                        continue;
                    case FormatRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found format record at " + k);
                        retval.formats.Add((FormatRecord)rec);
                        retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).IndexCode ? retval.maxformatid : ((FormatRecord)rec).IndexCode;
                        break;
                    case DateWindow1904Record.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found datewindow1904 record at " + k);
                        retval.uses1904datewindowing = ((DateWindow1904Record)rec).Windowing == 1;
                        break;
                    case PaletteRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found palette record at " + k);
                        retval.records.Palettepos = k;
                        break;
                    case WindowOneRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found WindowOneRecord at " + k);
                        retval.windowOne = (WindowOneRecord)rec;
                        break;
                    case WriteAccessRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found WriteAccess at " + k);
                        retval.writeAccess = (WriteAccessRecord)rec;
                        break;
                    case WriteProtectRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found WriteProtect at " + k);
                        retval.writeProtect = (WriteProtectRecord)rec;
                        break;
                    case FileSharingRecord.sid:
                        //if (log.Check(POILogger.DEBUG))
                        //    log.Log(DEBUG, "found FileSharing at " + k);
                        retval.fileShare = (FileSharingRecord)rec;
                        break;
                    case NameCommentRecord.sid:
                        NameCommentRecord ncr = (NameCommentRecord)rec;
                        retval.commentRecords[ncr.NameText] = ncr;
                        break;
                }
                records.Add(rec);
            }
            //What if we dont have any ranges and supbooks
            //        if (retval.records.supbookpos == 0) {
            //            retval.records.supbookpos = retval.records.bspos + 1;
            //            retval.records.namepos    = retval.records.supbookpos + 1;
            //        }

            // Look for other interesting values that
            //  follow the EOFRecord
            for (; k < recs.Count; k++)
            {
                Record rec = (Record)recs[k];
                switch (rec.Sid)
                {
                    case HyperlinkRecord.sid:
                        retval.hyperlinks.Add((HyperlinkRecord)rec);
                        break;
                }
            }

            if (retval.windowOne == null)
            {
                retval.windowOne = (WindowOneRecord)CreateWindowOne();
            }
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "exit Create workbook from existing file function");
            return retval;
        }
Esempio n. 4
0
 /// <summary>
 /// Wraps up your stub Workbook as a stub HSSFWorkbook, ready for passing to HSSFFormulaParser
 /// </summary>
 /// <param name="workbook">The stub workbook.</param>
 /// <returns></returns>
 public static HSSFWorkbook CreateStubHSSFWorkbook(InternalWorkbook workbook)
 {
     return new StubHSSFWorkbook(workbook);
 }
Esempio n. 5
0
            public StubHSSFWorkbook(InternalWorkbook wb)
                : base(wb)
            {

            }
Esempio n. 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HSSFRichTextString"/> class.
        /// </summary>
        /// <param name="book">The workbook.</param>
        /// <param name="record">The record.</param>
        public HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record)
        {
            SetWorkbookReferences(book, record);

            this.str = book.GetSSTString(record.SSTIndex);
        }
Esempio n. 7
0
 /// <summary>
 /// This must be called to Setup the internal work book references whenever
 /// a RichTextString Is Added to a cell
 /// </summary>
 /// <param name="book">The workbook.</param>
 /// <param name="record">The record.</param>
 public void SetWorkbookReferences(InternalWorkbook book, LabelSSTRecord record)
 {
     this.book = book;
     this.record = record;
 }
Esempio n. 8
0
        public HSSFWorkbook(InternalWorkbook book)
            : base((DirectoryNode)null)
        {

            workbook = book;
            _sheets = new List<HSSFSheet>(INITIAL_CAPACITY);
            names = new List<HSSFName>(INITIAL_CAPACITY);
        }
Esempio n. 9
0
        private void CheckWorkbookRecords(InternalWorkbook workbook)
        {
            IList records = workbook.Records;
            Assert.IsTrue(records[0] is BOFRecord);
            Assert.IsTrue(records[records.Count - 1] is EOFRecord);

            CheckRecordOrder(records, workbookRecords);
            //        CheckRecordstogether(records, workbookRecords);
        }
Esempio n. 10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HSSFCellStyle"/> class.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="rec">The record.</param>
 /// <param name="workbook">The workbook.</param>
 public HSSFCellStyle(short index, ExtendedFormatRecord rec, NPOI.HSSF.Model.InternalWorkbook workbook)
 {
     this.workbook = workbook;
     this.index    = index;
     format        = rec;
 }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HSSFCellStyle"/> class.
 /// </summary>
 /// <param name="index">The index.</param>
 /// <param name="rec">The record.</param>
 /// <param name="workbook">The workbook.</param>
 public HSSFCellStyle(short index, ExtendedFormatRecord rec, NPOI.HSSF.Model.InternalWorkbook workbook)
 {
     this.workbook = workbook;
     this.index = index;
     format = rec;
 }
Esempio n. 12
0
        /// <summary>
        /// Get the contents of the format string, by looking up
        /// the DataFormat against the supplied workbook
        /// </summary>
        /// <param name="workbook">The workbook.</param>
        /// <returns></returns>
        public String GetDataFormatString(NPOI.HSSF.Model.InternalWorkbook workbook)
        {
            HSSFDataFormat format = new HSSFDataFormat(workbook);

            return(format.GetFormat(DataFormat));
        }
Esempio n. 13
0
        /**
         * used internally to Set the workbook properties.
         */

        private void SetPropertiesFromWorkbook(InternalWorkbook book)
        {
            this.workbook = book;

            // none currently
        }
Esempio n. 14
0
            /**
     * 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);
            }
        }
Esempio n. 15
0
        /**
         * Creates an empty workbook object with three blank sheets and all the empty
         * fields.  Use this to Create a workbook from scratch.
         */
        public static InternalWorkbook CreateWorkbook()
        {
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "creating new workbook from scratch");
            InternalWorkbook retval = new InternalWorkbook();
            List<Record> records = new List<Record>(30);
            retval.records.Records=records;
            List<FormatRecord> formats = new List<FormatRecord>(8);

            records.Add(CreateBOF());
            records.Add(new InterfaceHdrRecord(CODEPAGE));
            records.Add(CreateMMS());
            records.Add(InterfaceEndRecord.Instance);
            records.Add(CreateWriteAccess());
            records.Add(CreateCodepage());
            records.Add(CreateDSF());
            records.Add(CreateTabId());
            retval.records.Tabpos=records.Count - 1;
            records.Add(CreateFnGroupCount());
            records.Add(CreateWindowProtect());
            records.Add(CreateProtect());
            retval.records.Protpos=records.Count - 1;
            records.Add(CreatePassword());
            records.Add(CreateProtectionRev4());
            records.Add(CreatePasswordRev4());
            retval.windowOne = (WindowOneRecord)CreateWindowOne();
            records.Add(retval.windowOne);
            records.Add(CreateBackup());
            retval.records.Backuppos=records.Count - 1;
            records.Add(CreateHideObj());
            records.Add(CreateDateWindow1904());
            records.Add(CreatePrecision());
            records.Add(CreateRefreshAll());
            records.Add(CreateBookBool());
            records.Add(CreateFont());
            records.Add(CreateFont());
            records.Add(CreateFont());
            records.Add(CreateFont());
            retval.records.Fontpos=records.Count - 1;   // last font record postion
            retval.numfonts = 4;

            // Set up format records
            for (int i = 0; i <= 7; i++)
            {
                Record rec = CreateFormat(i);
                retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).IndexCode ? retval.maxformatid : ((FormatRecord)rec).IndexCode;
                formats.Add((FormatRecord)rec);
                records.Add(rec);
            }
            retval.formats = formats;

            for (int k = 0; k < 21; k++)
            {
                records.Add(CreateExtendedFormat(k));
                retval.numxfs++;
            }
            retval.records.Xfpos=records.Count - 1;
            for (int k = 0; k < 6; k++)
            {
                records.Add(CreateStyle(k));
            }
            records.Add(CreateUseSelFS());

            int nBoundSheets = 1; // now just do 1
            for (int k = 0; k < nBoundSheets; k++)
            {
                BoundSheetRecord bsr =
                        (BoundSheetRecord)CreateBoundSheet(k);

                records.Add(bsr);
                retval.boundsheets.Add(bsr);
                retval.records.Bspos=records.Count - 1;
            }
            //        retval.records.supbookpos = retval.records.bspos + 1;
            //        retval.records.namepos = retval.records.supbookpos + 2;
            records.Add(CreateCountry());
            for (int k = 0; k < nBoundSheets; k++)
            {
                retval.OrCreateLinkTable.CheckExternSheet(k);
            }
            retval.sst = new SSTRecord();
            records.Add(retval.sst);
            records.Add(CreateExtendedSST());

            records.Add(EOFRecord.instance);
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "exit Create new workbook from scratch");
            return retval;
        }
Esempio n. 16
0
        public String ResolveNameXText(int refIndex, int definedNameIndex, InternalWorkbook workbook)
        {
            int extBookIndex = _externSheetRecord.GetExtbookIndexFromRefIndex(refIndex);
            int firstTabIndex = _externSheetRecord.GetFirstSheetIndexFromRefIndex(refIndex);
            if (firstTabIndex == -1)
            {
                // The referenced sheet could not be found
                throw new RuntimeException("Referenced sheet could not be found");
            }

            // Does it exist via the external book block?
            ExternalBookBlock externalBook = _externalBookBlocks[extBookIndex];
            if (externalBook._externalNameRecords.Length > definedNameIndex)
            {
                return _externalBookBlocks[extBookIndex].GetNameText(definedNameIndex);
            }
            else if (firstTabIndex == -2)
            {
                // Workbook scoped name, not actually external after all
                NameRecord nr = GetNameRecord(definedNameIndex);
                int sheetNumber = nr.SheetNumber;

                StringBuilder text = new StringBuilder();
                if (sheetNumber > 0)
                {
                    String sheetName = workbook.GetSheetName(sheetNumber - 1);
                    SheetNameFormatter.AppendFormat(text, sheetName);
                    text.Append("!");
                }
                text.Append(nr.NameText);
                return text.ToString();
            }
            else
            {
                throw new IndexOutOfRangeException(
                        "Ext Book Index relative but beyond the supported length, was " +
                        extBookIndex + " but maximum is " + _externalBookBlocks.Length
                );
            }
        }
Esempio n. 17
0
 /// <summary>
 /// Creates an HSSFSheet representing the given Sheet object.  Should only be
 /// called by HSSFWorkbook when reading in an exisiting file.
 /// </summary>
 /// <param name="workbook">The HSSF Workbook object associated with the _sheet.</param>
 /// <param name="sheet">lowlevel Sheet object this _sheet will represent</param>
 /// <see cref="NPOI.HSSF.UserModel.HSSFWorkbook(NPOI.POIFS.FileSystem.DirectoryNode, bool)"/>
 public HSSFSheet(HSSFWorkbook workbook, InternalSheet sheet)
 {
     this._sheet = sheet;
     rows = new Dictionary<int, NPOI.SS.UserModel.IRow>();
     this._workbook = workbook;
     this.book = _workbook.Workbook;
     SetPropertiesFromSheet(_sheet);
 }
Esempio n. 18
0
 public static HSSFWorkbook Create(InternalWorkbook book)
 {
     return new HSSFWorkbook(book);
 }