📄 sheet.java
字号:
// records.add(retval.createIndex()); records.add(retval.createCalcMode()); records.add(retval.createCalcCount() ); records.add( retval.createRefMode() ); records.add( retval.createIteration() ); records.add( retval.createDelta() ); records.add( retval.createSaveRecalc() ); records.add( retval.createPrintHeaders() ); retval.printGridlines = (PrintGridlinesRecord) retval.createPrintGridlines(); records.add( retval.printGridlines ); retval.gridset = (GridsetRecord) retval.createGridset(); records.add( retval.gridset ); records.add( retval.createGuts() ); retval.defaultrowheight = (DefaultRowHeightRecord) retval.createDefaultRowHeight(); records.add( retval.defaultrowheight ); records.add( retval.createWSBool() ); retval.header = (HeaderRecord) retval.createHeader(); records.add( retval.header ); retval.footer = (FooterRecord) retval.createFooter(); records.add( retval.footer ); records.add( retval.createHCenter() ); records.add( retval.createVCenter() ); retval.printSetup = (PrintSetupRecord) retval.createPrintSetup(); records.add( retval.printSetup ); retval.defaultcolwidth = (DefaultColWidthRecord) retval.createDefaultColWidth(); records.add( retval.defaultcolwidth); retval.dims = ( DimensionsRecord ) retval.createDimensions(); retval.dimsloc = 19; records.add(retval.dims); records.add(retval.windowTwo = retval.createWindowTwo()); retval.setLoc(records.size() - 1); retval.selection = (SelectionRecord) retval.createSelection(); records.add(retval.selection); retval.protect = (ProtectRecord) retval.createProtect(); records.add(retval.protect); records.add(retval.createEOF()); retval.records = records; log.log(log.DEBUG, "Sheet createsheet from scratch exit"); return retval; } private void checkCells() { if (cells == null) { cells = new ValueRecordsAggregate(); records.add(getDimsLoc() + 1, cells); } } private void checkRows() { if (rows == null) { rows = new RowRecordsAggregate(); records.add(getDimsLoc() + 1, rows); } } //public int addMergedRegion(short rowFrom, short colFrom, short rowTo, public int addMergedRegion(int rowFrom, short colFrom, int rowTo, short colTo) { if (merged == null || merged.getNumAreas() == 1027) { merged = ( MergeCellsRecord ) createMergedCells(); mergedRecords.add(merged); records.add(records.size() - 1, merged); } merged.addArea(rowFrom, colFrom, rowTo, colTo); return numMergedRegions++; } public void removeMergedRegion(int index) { //safety checks if (index >= numMergedRegions || mergedRecords.size() == 0) return; int pos = 0; int startNumRegions = 0; //optimisation for current record if (numMergedRegions - index < merged.getNumAreas()) { pos = mergedRecords.size() - 1; startNumRegions = numMergedRegions - merged.getNumAreas(); } else { for (int n = 0; n < mergedRecords.size(); n++) { MergeCellsRecord record = (MergeCellsRecord) mergedRecords.get(n); if (startNumRegions + record.getNumAreas() > index) { pos = n; break; } startNumRegions += record.getNumAreas(); } } MergeCellsRecord rec = (MergeCellsRecord) mergedRecords.get(pos); rec.removeAreaAt(index - startNumRegions); numMergedRegions--; if (rec.getNumAreas() == 0) { mergedRecords.remove(pos); //get rid of the record from the sheet records.remove(merged); if (merged == rec) { //pull up the LAST record for operations when we finally //support continue records for mergedRegions if (mergedRecords.size() > 0) { merged = (MergeCellsRecord) mergedRecords.get(mergedRecords.size() - 1); } else { merged = null; } } } } public MergeCellsRecord.MergedRegion getMergedRegionAt(int index) { //safety checks if (index >= numMergedRegions || mergedRecords.size() == 0) return null; int pos = 0; int startNumRegions = 0; //optimisation for current record if (numMergedRegions - index < merged.getNumAreas()) { pos = mergedRecords.size() - 1; startNumRegions = numMergedRegions - merged.getNumAreas(); } else { for (int n = 0; n < mergedRecords.size(); n++) { MergeCellsRecord record = (MergeCellsRecord) mergedRecords.get(n); if (startNumRegions + record.getNumAreas() > index) { pos = n; break; } startNumRegions += record.getNumAreas(); } } return ((MergeCellsRecord) mergedRecords.get(pos)).getAreaAt(index - startNumRegions); } public int getNumMergedRegions() { return numMergedRegions; } /** * This is basically a kludge to deal with the now obsolete Label records. If * you have to read in a sheet that contains Label records, be aware that the rest * of the API doesn't deal with them, the low level structure only provides read-only * semi-immutable structures (the sets are there for interface conformance with NO * impelmentation). In short, you need to call this function passing it a reference * to the Workbook object. All labels will be converted to LabelSST records and their * contained strings will be written to the Shared String tabel (SSTRecord) within * the Workbook. * * @param wb sheet's matching low level Workbook structure containing the SSTRecord. * @see org.apache.poi.hssf.record.LabelRecord * @see org.apache.poi.hssf.record.LabelSSTRecord * @see org.apache.poi.hssf.record.SSTRecord */ public void convertLabelRecords(Workbook wb) { log.log(log.DEBUG, "convertLabelRecords called"); if (containsLabels) { for (int k = 0; k < records.size(); k++) { Record rec = ( Record ) records.get(k); if (rec.getSid() == LabelRecord.sid) { LabelRecord oldrec = ( LabelRecord ) rec; records.remove(k); LabelSSTRecord newrec = new LabelSSTRecord(); int stringid = wb.addSSTString(oldrec.getValue()); newrec.setRow(oldrec.getRow()); newrec.setColumn(oldrec.getColumn()); newrec.setXFIndex(oldrec.getXFIndex()); newrec.setSSTIndex(stringid); records.add(k, newrec); } } } log.log(log.DEBUG, "convertLabelRecords exit"); } /** * Returns the number of low level binary records in this sheet. This adjusts things for the so called * AgregateRecords. * * @see org.apache.poi.hssf.record.Record */ public int getNumRecords() { checkCells(); checkRows(); log.log(log.DEBUG, "Sheet.getNumRecords"); log.logFormatted(log.DEBUG, "returning % + % + % - 2 = %", new int[] { records.size(), cells.getPhysicalNumberOfCells(), rows.getPhysicalNumberOfRows(), records.size() + cells.getPhysicalNumberOfCells() + rows.getPhysicalNumberOfRows() - 2 }); return records.size() + cells.getPhysicalNumberOfCells() + rows.getPhysicalNumberOfRows() - 2; } /** * Per an earlier reported bug in working with Andy Khan's excel read library. This * sets the values in the sheet's DimensionsRecord object to be correct. Excel doesn't * really care, but we want to play nice with other libraries. * * @see org.apache.poi.hssf.record.DimensionsRecord */ //public void setDimensions(short firstrow, short firstcol, short lastrow, public void setDimensions(int firstrow, short firstcol, int lastrow, short lastcol) { log.log(log.DEBUG, "Sheet.setDimensions"); log.log(log.DEBUG, (new StringBuffer("firstrow")).append(firstrow) .append("firstcol").append(firstcol).append("lastrow") .append(lastrow).append("lastcol").append(lastcol) .toString()); dims.setFirstCol(firstcol); dims.setFirstRow(firstrow); dims.setLastCol(lastcol); dims.setLastRow(lastrow); log.log(log.DEBUG, "Sheet.setDimensions exiting"); } /** * set the locator for where we should look for the next value record. The * algorythm will actually start here and find the correct location so you * can set this to 0 and watch performance go down the tubes but it will work. * After a value is set this is automatically advanced. Its also set by the * create method. So you probably shouldn't mess with this unless you have * a compelling reason why or the help for the method you're calling says so. * Check the other methods for whether they care about * the loc pointer. Many of the "modify" and "remove" methods re-initialize this * to "dimsloc" which is the location of the Dimensions Record and presumably the * start of the value section (at or around 19 dec). * * @param loc the record number to start at * */ public void setLoc(int loc) { valueRecIterator = null; log.log(log.DEBUG, "sheet.setLoc(): " + loc); this.loc = loc; } /** * Returns the location pointer to the first record to look for when adding rows/values * */ public int getLoc() { log.log(log.DEBUG, "sheet.getLoc():" + loc); return loc; } /** * Set the preoffset when using DBCELL records (currently unused) - this is * the position of this sheet within the whole file. * * @param offset the offset of the sheet's BOF within the file. */ public void setPreOffset(int offset) { this.preoffset = offset; } /** * get the preoffset when using DBCELL records (currently unused) - this is * the position of this sheet within the whole file. * * @return offset the offset of the sheet's BOF within the file. */ public int getPreOffset() { return preoffset; } /** * Serializes all records in the sheet into one big byte array. Use this to write * the sheet out. * * @return byte[] array containing the binary representation of the records in this sheet * */ public byte [] serialize() { log.log(log.DEBUG, "Sheet.serialize"); // addDBCellRecords(); byte[] retval = null; // ArrayList bytes = new ArrayList(4096); int arraysize = getSize(); int pos = 0; // for (int k = 0; k < records.size(); k++) // { // bytes.add((( Record ) records.get(k)).serialize()); // // } // for (int k = 0; k < bytes.size(); k++) // { // arraysize += (( byte [] ) bytes.get(k)).length; // log.debug((new StringBuffer("arraysize=")).append(arraysize) // .toString()); // } retval = new byte[ arraysize ]; for (int k = 0; k < records.size(); k++) { // byte[] rec = (( byte [] ) bytes.get(k)); // System.arraycopy(rec, 0, retval, pos, rec.length); pos += (( Record ) records.get(k)).serialize(pos, retval); // rec.length; } log.log(log.DEBUG, "Sheet.serialize returning " + retval); return retval; } /** * Serializes all records in the sheet into one big byte array. Use this to write * the sheet out. * * @param offset to begin write at * @param data array containing the binary representation of the records in this sheet * */ public int serialize(int offset, byte [] data) { log.log(log.DEBUG, "Sheet.serialize using offsets"); // addDBCellRecords(); // ArrayList bytes = new ArrayList(4096); // int arraysize = getSize(); // 0; int pos = 0; // for (int k = 0; k < records.size(); k++) // { // bytes.add((( Record ) records.get(k)).serialize()); // // } // for (int k = 0; k < bytes.size(); k++) // { // arraysize += (( byte [] ) bytes.get(k)).length; // log.debug((new StringBuffer("arraysize=")).append(arraysize) // .toString()); // } for (int k = 0; k < records.size(); k++) {// byte[] rec = (( byte [] ) bytes.get(k));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -