columninforecordsaggregate.java
来自「EXCEL read and write」· Java 代码 · 共 524 行 · 第 1/2 页
JAVA
524 行
if (idx == -1) { return; } // If it is already expanded do nothing. if (!isColumnGroupCollapsed(idx)) { return; } // Find the start/end of the group. int startIdx = findStartOfColumnOutlineGroup(idx); int endIdx = findEndOfColumnOutlineGroup(idx); // expand: // colapsed 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 uncollapse contained groups ColumnInfoRecord columnInfo = getColInfo(endIdx); if (!isColumnGroupHiddenByParent(idx)) { int outlineLevel = columnInfo.getOutlineLevel(); for (int i = startIdx; i <= endIdx; i++) { ColumnInfoRecord ci = getColInfo(i); if (outlineLevel == ci.getOutlineLevel()) ci.setHidden(false); } } // Write collapse flag (stored in a single col info record after this outline group) setColumn(columnInfo.getLastColumn() + 1, null, null, null, null, Boolean.FALSE); } private static ColumnInfoRecord copyColInfo(ColumnInfoRecord ci) { return (ColumnInfoRecord) ci.clone(); } public void setColumn(int targetColumnIx, Short xfIndex, Integer width, Integer level, Boolean hidden, Boolean collapsed) { ColumnInfoRecord ci = null; int k = 0; for (k = 0; k < records.size(); k++) { ColumnInfoRecord tci = (ColumnInfoRecord) records.get(k); if (tci.containsColumn(targetColumnIx)) { ci = tci; break; } if (tci.getFirstColumn() > targetColumnIx) { // call column infos after k are for later columns break; // exit now so k will be the correct insert pos } } if (ci == null) { // okay so there ISN'T a column info record that covers this column so lets create one! ColumnInfoRecord nci = new ColumnInfoRecord(); nci.setFirstColumn(targetColumnIx); nci.setLastColumn(targetColumnIx); setColumnInfoFields( nci, xfIndex, width, level, hidden, collapsed ); insertColumn(k, nci); attemptMergeColInfoRecords(k); return; } boolean styleChanged = xfIndex != null && ci.getXFIndex() != xfIndex.shortValue(); boolean widthChanged = width != null && ci.getColumnWidth() != width.shortValue(); boolean levelChanged = level != null && ci.getOutlineLevel() != level.intValue(); boolean hiddenChanged = hidden != null && ci.getHidden() != hidden.booleanValue(); boolean collapsedChanged = collapsed != null && ci.getCollapsed() != collapsed.booleanValue(); boolean columnChanged = styleChanged || widthChanged || levelChanged || hiddenChanged || collapsedChanged; if (!columnChanged) { // do nothing...nothing changed. return; } if (ci.getFirstColumn() == targetColumnIx && ci.getLastColumn() == targetColumnIx) { // ColumnInfo ci for a single column, the target column setColumnInfoFields(ci, xfIndex, width, level, hidden, collapsed); attemptMergeColInfoRecords(k); return; } if (ci.getFirstColumn() == targetColumnIx || ci.getLastColumn() == targetColumnIx) { // The target column is at either end of the multi-column ColumnInfo ci // we'll just divide the info and create a new one if (ci.getFirstColumn() == targetColumnIx) { ci.setFirstColumn(targetColumnIx + 1); } else { ci.setLastColumn(targetColumnIx - 1); k++; // adjust insert pos to insert after } ColumnInfoRecord nci = copyColInfo(ci); nci.setFirstColumn(targetColumnIx); nci.setLastColumn(targetColumnIx); setColumnInfoFields( nci, xfIndex, width, level, hidden, collapsed ); insertColumn(k, nci); attemptMergeColInfoRecords(k); } else { //split to 3 records ColumnInfoRecord ciStart = ci; ColumnInfoRecord ciMid = copyColInfo(ci); ColumnInfoRecord ciEnd = copyColInfo(ci); int lastcolumn = ci.getLastColumn(); ciStart.setLastColumn(targetColumnIx - 1); ciMid.setFirstColumn(targetColumnIx); ciMid.setLastColumn(targetColumnIx); setColumnInfoFields(ciMid, xfIndex, width, level, hidden, collapsed); insertColumn(++k, ciMid); ciEnd.setFirstColumn(targetColumnIx+1); ciEnd.setLastColumn(lastcolumn); insertColumn(++k, ciEnd); // no need to attemptMergeColInfoRecords because we // know both on each side are different } } /** * Sets all non null fields into the <code>ci</code> parameter. */ private static void setColumnInfoFields(ColumnInfoRecord ci, Short xfStyle, Integer width, Integer level, Boolean hidden, Boolean collapsed) { if (xfStyle != null) { ci.setXFIndex(xfStyle.shortValue()); } if (width != null) { ci.setColumnWidth(width.intValue()); } if (level != null) { ci.setOutlineLevel( level.shortValue() ); } if (hidden != null) { ci.setHidden( hidden.booleanValue() ); } if (collapsed != null) { ci.setCollapsed( collapsed.booleanValue() ); } } private int findColInfoIdx(int columnIx, int fromColInfoIdx) { if (columnIx < 0) { throw new IllegalArgumentException( "column parameter out of range: " + columnIx ); } if (fromColInfoIdx < 0) { throw new IllegalArgumentException( "fromIdx parameter out of range: " + fromColInfoIdx ); } for (int k = fromColInfoIdx; k < records.size(); k++) { ColumnInfoRecord ci = getColInfo(k); if (ci.containsColumn(columnIx)) { return k; } if (ci.getFirstColumn() > columnIx) { break; } } return -1; } /** * Attempts to merge the col info record at the specified index * with either or both of its neighbours */ private void attemptMergeColInfoRecords(int colInfoIx) { int nRecords = records.size(); if (colInfoIx < 0 || colInfoIx >= nRecords) { throw new IllegalArgumentException("colInfoIx " + colInfoIx + " is out of range (0.." + (nRecords-1) + ")"); } ColumnInfoRecord currentCol = getColInfo(colInfoIx); int nextIx = colInfoIx+1; if (nextIx < nRecords) { if (mergeColInfoRecords(currentCol, getColInfo(nextIx))) { records.remove(nextIx); } } if (colInfoIx > 0) { if (mergeColInfoRecords(getColInfo(colInfoIx - 1), currentCol)) { records.remove(colInfoIx); } } } /** * merges two column info records (if they are adjacent and have the same formatting, etc) * @return <code>false</code> if the two column records could not be merged */ private static boolean mergeColInfoRecords(ColumnInfoRecord ciA, ColumnInfoRecord ciB) { if (ciA.isAdjacentBefore(ciB) && ciA.formatMatches(ciB)) { ciA.setLastColumn(ciB.getLastColumn()); return true; } return false; } /** * Creates an outline group for the specified columns, by setting the level * field for each col info record in the range. {@link ColumnInfoRecord}s * may be created, split or merged as a result of this operation. * * @param fromColumnIx * group from this column (inclusive) * @param toColumnIx * group to this column (inclusive) * @param indent * if <code>true</code> the group will be indented by one * level, if <code>false</code> indenting will be decreased by * one level. */ public void groupColumnRange(int fromColumnIx, int toColumnIx, boolean indent) { int colInfoSearchStartIdx = 0; // optimization to speed up the search for col infos for (int i = fromColumnIx; i <= toColumnIx; i++) { int level = 1; int colInfoIdx = findColInfoIdx(i, colInfoSearchStartIdx); if (colInfoIdx != -1) { level = getColInfo(colInfoIdx).getOutlineLevel(); if (indent) { level++; } else { level--; } level = Math.max(0, level); level = Math.min(7, level); colInfoSearchStartIdx = Math.max(0, colInfoIdx - 1); // -1 just in case this column is collapsed later. } setColumn(i, null, null, new Integer(level), null, null); } } /** * Finds the <tt>ColumnInfoRecord</tt> which contains the specified columnIndex * @param columnIndex index of the column (not the index of the ColumnInfoRecord) * @return <code>null</code> if no column info found for the specified column */ public ColumnInfoRecord findColumnInfo(int columnIndex) { int nInfos = records.size(); for(int i=0; i< nInfos; i++) { ColumnInfoRecord ci = getColInfo(i); if (ci.containsColumn(columnIndex)) { return ci; } } return null; } public int getMaxOutlineLevel() { int result = 0; int count=records.size(); for (int i=0; i<count; i++) { ColumnInfoRecord columnInfoRecord = getColInfo(i); result = Math.max(columnInfoRecord.getOutlineLevel(), result); } return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?