?? sheet.java
字號:
public int serialize(int offset, byte [] data) { if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "Sheet.serialize using offsets"); int pos = offset; boolean haveSerializedIndex = false; for (int k = 0; k < records.size(); k++) { Record record = (( Record ) records.get(k)); //Once the rows have been found in the list of records, start //writing out the blocked row information. This includes the DBCell references if (record instanceof RowRecordsAggregate) { pos += ((RowRecordsAggregate)record).serialize(pos, data, cells); // rec.length; } else if (record instanceof ValueRecordsAggregate) { //Do nothing here. The records were serialized during the RowRecordAggregate block serialization } else { pos += record.serialize(pos, data ); // rec.length; } //If the BOF record was just serialized then add the IndexRecord if (record.getSid() == BOFRecord.sid) { //Can there be more than one BOF for a sheet? If not then we can //remove this guard. So be safe it is left here. if (rows != null && !haveSerializedIndex) { haveSerializedIndex = true; pos += serializeIndexRecord(k, pos, data); } } //// uncomment to test record sizes ////// System.out.println( record.getClass().getName() );// byte[] data2 = new byte[record.getRecordSize()];// record.serialize(0, data2 ); // rec.length;// if (LittleEndian.getUShort(data2, 2) != record.getRecordSize() - 4// && record instanceof RowRecordsAggregate == false// && record instanceof ValueRecordsAggregate == false// && record instanceof EscherAggregate == false)// {// throw new RuntimeException("Blah!!! Size off by " + ( LittleEndian.getUShort(data2, 2) - record.getRecordSize() - 4) + " records.");// }//asd: int len = record.serialize(pos + offset, data ); ///// DEBUG BEGIN ///////asd: if (len != record.getRecordSize())//asd: throw new IllegalStateException("Record size does not match serialized bytes. Serialized size = " + len + " but getRecordSize() returns " + record.getRecordSize() + ". Record object is " + record.getClass()); ///// DEBUG END ///////asd: pos += len; // rec.length; } if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "Sheet.serialize returning "); return pos-offset; } private int serializeIndexRecord(final int BOFRecordIndex, final int offset, byte[] data) { IndexRecord index = new IndexRecord(); index.setFirstRow(rows.getFirstRowNum()); index.setLastRowAdd1(rows.getLastRowNum()+1); //Calculate the size of the records from the end of the BOF //and up to the RowRecordsAggregate... int sheetRecSize = 0; for (int j = BOFRecordIndex+1; j < records.size(); j++) { Record tmpRec = (( Record ) records.get(j)); if (tmpRec instanceof RowRecordsAggregate) break; sheetRecSize+= tmpRec.getRecordSize(); } //Add the references to the DBCells in the IndexRecord (one for each block) int blockCount = rows.getRowBlockCount(); //Calculate the size of this IndexRecord int indexRecSize = index.getRecordSizeForBlockCount(blockCount); int rowBlockOffset = 0; int cellBlockOffset = 0; int dbCellOffset = 0; for (int block=0;block<blockCount;block++) { rowBlockOffset += rows.getRowBlockSize(block); cellBlockOffset += cells.getRowCellBlockSize(rows.getStartRowNumberForBlock(block), rows.getEndRowNumberForBlock(block)); //Note: The offsets are relative to the Workbook BOF. Assume that this is //0 for now..... index.addDbcell(offset + indexRecSize + sheetRecSize + dbCellOffset + rowBlockOffset + cellBlockOffset); //Add space required to write the dbcell record(s) (whose references were just added). dbCellOffset += (8 + (rows.getRowCountForBlock(block) * 2)); } return index.serialize(offset, data); } /** * Create a row record. (does not add it to the records contained in this sheet) * * @param row number * @return RowRecord created for the passed in row number * @see org.apache.poi.hssf.record.RowRecord */ public RowRecord createRow(int row) { return RowRecordsAggregate.createRow( row ); } /** * Create a LABELSST Record (does not add it to the records contained in this sheet) * * @param row the row the LabelSST is a member of * @param col the column the LabelSST defines * @param index the index of the string within the SST (use workbook addSSTString method) * @return LabelSSTRecord newly created containing your SST Index, row,col. * @see org.apache.poi.hssf.record.SSTRecord */ //public LabelSSTRecord createLabelSST(short row, short col, int index) public LabelSSTRecord createLabelSST(int row, short col, int index) { log.logFormatted(POILogger.DEBUG, "create labelsst row,col,index %,%,%", new int[] { row, col, index }); LabelSSTRecord rec = new LabelSSTRecord(); rec.setRow(row); rec.setColumn(col); rec.setSSTIndex(index); rec.setXFIndex(( short ) 0x0f); return rec; } /** * Create a NUMBER Record (does not add it to the records contained in this sheet) * * @param row the row the NumberRecord is a member of * @param col the column the NumberRecord defines * @param value for the number record * * @return NumberRecord for that row, col containing that value as added to the sheet */ //public NumberRecord createNumber(short row, short col, double value) public NumberRecord createNumber(int row, short col, double value) { log.logFormatted(POILogger.DEBUG, "create number row,col,value %,%,%", new double[] { row, col, value }); NumberRecord rec = new NumberRecord(); //rec.setRow(( short ) row); rec.setRow(row); rec.setColumn(col); rec.setValue(value); rec.setXFIndex(( short ) 0x0f); return rec; } /** * create a BLANK record (does not add it to the records contained in this sheet) * * @param row - the row the BlankRecord is a member of * @param col - the column the BlankRecord is a member of */ //public BlankRecord createBlank(short row, short col) public BlankRecord createBlank(int row, short col) { //log.logFormatted(POILogger.DEBUG, "create blank row,col %,%", new short[] log.logFormatted(POILogger.DEBUG, "create blank row,col %,%", new int[] { row, col }); BlankRecord rec = new BlankRecord(); //rec.setRow(( short ) row); rec.setRow(row); rec.setColumn(col); rec.setXFIndex(( short ) 0x0f); return rec; } /** * Attempts to parse the formula into PTGs and create a formula record * DOES NOT WORK YET * * @param row - the row for the formula record * @param col - the column of the formula record * @param formula - a String representing the formula. To be parsed to PTGs * @return bogus/useless formula record */ //public FormulaRecord createFormula(short row, short col, String formula) public FormulaRecord createFormula(int row, short col, String formula) { log.logFormatted(POILogger.DEBUG, "create formula row,col,formula %,%,%", //new short[] new int[] { row, col }, formula); FormulaRecord rec = new FormulaRecord(); rec.setRow(row); rec.setColumn(col); rec.setOptions(( short ) 2); rec.setValue(0); rec.setXFIndex(( short ) 0x0f); FormulaParser fp = new FormulaParser(formula,null); //fix - do we need this method? fp.parse(); Ptg[] ptg = fp.getRPNPtg(); int size = 0; for (int k = 0; k < ptg.length; k++) { size += ptg[ k ].getSize(); rec.pushExpressionToken(ptg[ k ]); } rec.setExpressionLength(( short ) size); return rec; } /** * Adds a value record to the sheet's contained binary records * (i.e. LabelSSTRecord or NumberRecord). * <P> * This method is "loc" sensitive. Meaning you need to set LOC to where you * want it to start searching. If you don't know do this: setLoc(getDimsLoc). * When adding several rows you can just start at the last one by leaving loc * at what this sets it to. * * @param row the row to add the cell value to * @param col the cell value record itself. */ //public void addValueRecord(short row, CellValueRecordInterface col) public void addValueRecord(int row, CellValueRecordInterface col) { checkCells(); log.logFormatted(POILogger.DEBUG, "add value record row,loc %,%", new int[] { row, loc }); DimensionsRecord d = ( DimensionsRecord ) records.get(getDimsLoc()); if (col.getColumn() > d.getLastCol()) { d.setLastCol(( short ) (col.getColumn() + 1)); } if (col.getColumn() < d.getFirstCol()) { d.setFirstCol(col.getColumn()); } cells.insertCell(col); /* * for (int k = loc; k < records.size(); k++) * { * Record rec = ( Record ) records.get(k); * * if (rec.getSid() == RowRecord.sid) * { * RowRecord rowrec = ( RowRecord ) rec; * * if (rowrec.getRowNumber() == col.getRow()) * { * records.add(k + 1, col); * loc = k; * if (rowrec.getLastCol() <= col.getColumn()) * { * rowrec.setLastCol((( short ) (col.getColumn() + 1))); * } * break; * } * } * } */ } /** * remove a value record from the records array. * * This method is not loc sensitive, it resets loc to = dimsloc so no worries. * * @param row - the row of the value record you wish to remove * @param col - a record supporting the CellValueRecordInterface. * @see org.apache.poi.hssf.record.CellValueRecordInterface */ //public void removeValueRecord(short row, CellValueRecordInterface col) public void removeValueRecord(int row, CellValueRecordInterface col) { checkCells(); log.logFormatted(POILogger.DEBUG, "remove value record row,dimsloc %,%", new int[]{row, dimsloc} ); loc = dimsloc; cells.removeCell(col); /* * for (int k = loc; k < records.size(); k++) * { * Record rec = ( Record ) records.get(k); * * // checkDimsLoc(rec,k); * if (rec.isValue()) * { * CellValueRecordInterface cell = * ( CellValueRecordInterface ) rec; * * if ((cell.getRow() == col.getRow()) * && (cell.getColumn() == col.getColumn())) * { * records.remove(k); * break; * } * } * } */ } /** * replace a value record from the records array. * * This method is not loc sensitive, it resets loc to = dimsloc so no worries. * * @param newval - a record supporting the CellValueRecordInterface. this will replace * the cell value with the same row and column. If there isn't one, one will * be added. */ public void replaceValueRecord(CellValueRecordInterface newval) { checkCells(); setLoc(dimsloc); if (log.check( POILogger.DEBUG )) log.log(POILogger.DEBUG, "replaceValueRecord "); cells.insertCell(newval); /* * CellValueRecordInterface oldval = getNextValueRecord(); * * while (oldval != null) * { * if (oldval.isEqual(newval)) * { * records.set(( short ) (getLoc() - 1), newval); * return; * } * oldval = getNextValueRecord(); * } * addValueRecord(newval.getRow(), newval); * setLoc(dimsloc); */ } /** * Adds a row record to the sheet * * <P> * This method is "loc" sensitive. Meaning you need to set LOC to where you * want it to start searching. If you don't know do this: setLoc(getDimsLoc). * When adding several rows you can just start at the last one by leaving loc * at what this sets it to. * * @param row the row record to be added * @see #setLoc(int) */ public void addRow(RowRecord row)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -