rowrecordsaggregate.java
来自「EXCEL read and write」· Java 代码 · 共 515 行 · 第 1/2 页
JAVA
515 行
_valuesAgg.visitCellsForRow(row, stv); int rowCellSize = stv.getPosition(); pos += rowCellSize; // Add the offset to the first cell for the row into the // DBCellRecord. cellRecord.addCellOffset((short) cellRefOffset); cellRefOffset = rowCellSize; } } // Calculate Offset from the start of a DBCellRecord to the first Row cellRecord.setRowOffset(pos); rv.visitRecord(cellRecord); } for (int i=0; i< _unknownRecords.size(); i++) { // Potentially breaking the file here since we don't know exactly where to write these records rv.visitRecord((Record) _unknownRecords.get(i)); } } public Iterator getIterator() { return _rowRecords.values().iterator(); } public Iterator getAllRecordsIterator() { List result = new ArrayList(_rowRecords.size() * 2); result.addAll(_rowRecords.values()); Iterator vi = _valuesAgg.getIterator(); while (vi.hasNext()) { result.add(vi.next()); } return result.iterator(); } public int findStartOfRowOutlineGroup(int row) { // Find the start of the group. RowRecord rowRecord = this.getRow( row ); int level = rowRecord.getOutlineLevel(); int currentRow = row; while (this.getRow( currentRow ) != null) { rowRecord = this.getRow( currentRow ); if (rowRecord.getOutlineLevel() < level) return currentRow + 1; currentRow--; } return currentRow + 1; } public int findEndOfRowOutlineGroup( int row ) { int level = getRow( row ).getOutlineLevel(); int currentRow; for (currentRow = row; currentRow < this.getLastRowNum(); currentRow++) { if (getRow(currentRow) == null || getRow(currentRow).getOutlineLevel() < level) { break; } } return currentRow-1; } public int writeHidden( RowRecord rowRecord, int row, boolean hidden ) { int level = rowRecord.getOutlineLevel(); while (rowRecord != null && this.getRow(row).getOutlineLevel() >= level) { rowRecord.setZeroHeight( hidden ); row++; rowRecord = this.getRow( row ); } return row - 1; } public void collapseRow( int rowNumber ) { // Find the start of the group. int startRow = findStartOfRowOutlineGroup( rowNumber ); RowRecord rowRecord = getRow( startRow ); // Hide all the columns until the end of the group int lastRow = writeHidden( rowRecord, startRow, true ); // Write collapse field if (getRow(lastRow + 1) != null) { getRow(lastRow + 1).setColapsed( true ); } else { RowRecord row = createRow( lastRow + 1); row.setColapsed( true ); insertRow( row ); } } /** * Create a row record. * * @param row number * @return RowRecord created for the passed in row number * @see org.apache.poi.hssf.record.RowRecord */ public static RowRecord createRow(int rowNumber) { return new RowRecord(rowNumber); } public boolean isRowGroupCollapsed( int row ) { int collapseRow = findEndOfRowOutlineGroup( row ) + 1; if (getRow(collapseRow) == null) return false; else return getRow( collapseRow ).getColapsed(); } public void expandRow( int rowNumber ) { int idx = rowNumber; if (idx == -1) return; // If it is already expanded do nothing. if (!isRowGroupCollapsed(idx)) return; // Find the start of the group. int startIdx = findStartOfRowOutlineGroup( idx ); RowRecord row = getRow( startIdx ); // Find the end of the group. int endIdx = findEndOfRowOutlineGroup( idx ); // expand: // collapsed bit must be unset // hidden bit gets unset _if_ surrounding groups are expanded you can determine // this by looking at the hidden bit of the enclosing group. You will have // to look at the start and the end of the current group to determine which // is the enclosing group // hidden bit only is altered for this outline level. ie. don't un-collapse contained groups if ( !isRowGroupHiddenByParent( idx ) ) { for ( int i = startIdx; i <= endIdx; i++ ) { if ( row.getOutlineLevel() == getRow( i ).getOutlineLevel() ) getRow( i ).setZeroHeight( false ); else if (!isRowGroupCollapsed(i)) getRow( i ).setZeroHeight( false ); } } // Write collapse field getRow( endIdx + 1 ).setColapsed( false ); } public boolean isRowGroupHiddenByParent( int row ) { // Look out outline details of end int endLevel; boolean endHidden; int endOfOutlineGroupIdx = findEndOfRowOutlineGroup( row ); if (getRow( endOfOutlineGroupIdx + 1 ) == null) { endLevel = 0; endHidden = false; } else { endLevel = getRow( endOfOutlineGroupIdx + 1).getOutlineLevel(); endHidden = getRow( endOfOutlineGroupIdx + 1).getZeroHeight(); } // Look out outline details of start int startLevel; boolean startHidden; int startOfOutlineGroupIdx = findStartOfRowOutlineGroup( row ); if (startOfOutlineGroupIdx - 1 < 0 || getRow(startOfOutlineGroupIdx - 1) == null) { startLevel = 0; startHidden = false; } else { startLevel = getRow( startOfOutlineGroupIdx - 1).getOutlineLevel(); startHidden = getRow( startOfOutlineGroupIdx - 1 ).getZeroHeight(); } if (endLevel > startLevel) { return endHidden; } else { return startHidden; } } public CellValueRecordInterface[] getValueRecords() { return _valuesAgg.getValueRecords(); } public IndexRecord createIndexRecord(int indexRecordOffset, int sizeOfInitialSheetRecords) { IndexRecord result = new IndexRecord(); result.setFirstRow(_firstrow); result.setLastRowAdd1(_lastrow + 1); // Calculate the size of the records from the end of the BOF // and up to the RowRecordsAggregate... // Add the references to the DBCells in the IndexRecord (one for each block) // Note: The offsets are relative to the Workbook BOF. Assume that this is // 0 for now..... int blockCount = getRowBlockCount(); // Calculate the size of this IndexRecord int indexRecSize = IndexRecord.getRecordSizeForBlockCount(blockCount); int currentOffset = indexRecordOffset + indexRecSize + sizeOfInitialSheetRecords; for (int block = 0; block < blockCount; block++) { // each row-block has a DBCELL record. // The offset of each DBCELL record needs to be updated in the INDEX record // account for row records in this row-block currentOffset += getRowBlockSize(block); // account for cell value records after those currentOffset += _valuesAgg.getRowCellBlockSize( getStartRowNumberForBlock(block), getEndRowNumberForBlock(block)); // currentOffset is now the location of the DBCELL record for this row-block result.addDbcell(currentOffset); // Add space required to write the DBCELL record (whose reference was just added). currentOffset += (8 + (getRowCountForBlock(block) * 2)); } return result; } public void insertCell(CellValueRecordInterface cvRec) { _valuesAgg.insertCell(cvRec); } public void removeCell(CellValueRecordInterface cvRec) { _valuesAgg.removeCell(cvRec); } public FormulaRecordAggregate createFormula(int row, int col) { FormulaRecord fr = new FormulaRecord(); fr.setRow(row); fr.setColumn((short) col); return new FormulaRecordAggregate(fr, null, _sharedValueManager); } public void updateFormulasAfterRowShift(FormulaShifter formulaShifter, int currentExternSheetIndex) { _valuesAgg.updateFormulasAfterRowShift(formulaShifter, currentExternSheetIndex); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?