📄 rowrecord.java~
字号:
/*********************************************************************
*
* Copyright (C) 2002 Andrew Khan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
package jxl.write.biff;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import common.Logger;
import jxl.CellType;
import jxl.biff.CellReferenceHelper;
import jxl.biff.IndexMapping;
import jxl.biff.IntegerHelper;
import jxl.biff.Type;
import jxl.biff.WritableRecordData;
import jxl.biff.XFRecord;
import jxl.write.Number;
/**
* Contains all the cells for a given row in a sheet
*/
class RowRecord extends WritableRecordData
{
/**
* The logger
*/
private static final Logger logger = Logger.getLogger(RowRecord.class);
/**
* The binary data
*/
private byte[] data;
/**
* The cells which comprise this row
*/
private CellValue[] cells;
/**
* The height of this row in 1/20ths of a point
*/
private int rowHeight;
/**
* Flag to indicate whether this row is outline collapsed or not
*/
private boolean collapsed;
/**
* The number of this row within the worksheet
*/
private int rowNumber;
/**
* The number of columns in this row. This is the largest column value + 1
*/
private int numColumns;
/**
* The xfIndex for this row
*/
private int xfIndex;
/**
* The style for this row
*/
private XFRecord style;
/**
* Flag indicating that this row record has an default format
*/
private boolean defaultFormat;
/**
* Flag indicating whether this row matches the default font height
*/
private boolean matchesDefFontHeight;
/**
* The amount to grow the cells array by
*/
private static final int growSize = 10;
/**
* The maximum integer value that can be squeezed into 30 bits
*/
private static final int maxRKValue = 0x1fffffff;
/**
* The minimum integer value that can be squeezed into 30 bits
*/
private static final int minRKValue = -0x20000000;
/**
* Indicates that the row is default height
*/
private static int defaultHeightIndicator = 0xff;
/**
* The maximum number of columns
*/
private static int maxColumns = 256;
/**
* The outline level of the row
*/
private int outlineLevel;
/**
* Is this the icon indicator row of a group?
*/
private boolean groupStart;
/**
* Constructs an empty row which has the specified row number
*
* @param rn the row number of this row
*/
public RowRecord(int rn)
{
super(Type.ROW);
rowNumber = rn;
cells = new CellValue[0];
numColumns = 0;
rowHeight = defaultHeightIndicator;
collapsed = false;
matchesDefFontHeight = true;
}
/**
* Sets the height of this row
*
* @param h the row height
*/
public void setRowHeight(int h)
{
if (h == 0)
{
setCollapsed(true);
matchesDefFontHeight = false;
}
else
{
rowHeight = h;
matchesDefFontHeight = false;
}
}
/**
* Sets the row details based upon the readable row record passed in
* Called when copying spreadsheets
*
* @param height the height of the row record in 1/20ths of a point
* @param mdfh matches the default font height
* @param col the collapsed status of the row
* @param ol the outline level
* @param gs the group start
* @param xf the xfrecord for the row (NULL if no default is set)
*/
void setRowDetails(int height,
boolean mdfh,
boolean col,
int ol,
boolean gs,
XFRecord xfr)
{
rowHeight = height;
collapsed = col;
matchesDefFontHeight = mdfh;
outlineLevel = ol;
groupStart = gs;
if (xfr != null)
{
defaultFormat = true;
style = xfr;
xfIndex = style.getXFIndex();
}
}
/**
* Sets the collapsed status of this row
*
* @param c the collapsed flag
*/
public void setCollapsed(boolean c)
{
collapsed = c;
}
/**
* Gets the row number of this row
*
* @return the row number
*/
public int getRowNumber()
{
return rowNumber;
}
/**
* Adds a cell to this row, growing the array of cells as required
*
* @param cv the cell to add
*/
public void addCell(CellValue cv)
{
int col = cv.getColumn();
if (col >= maxColumns)
{
logger.warn("Could not add cell at " +
CellReferenceHelper.getCellReference(cv.getRow(),
cv.getColumn()) +
" because it exceeds the maximum column limit");
return;
}
// Grow the array if needs be
// Thanks to Brendan for spotting the flaw in merely adding on the
// grow size
if (col >= cells.length)
{
CellValue[] oldCells = cells;
cells = new CellValue[Math.max(oldCells.length + growSize, col+1)];
System.arraycopy(oldCells, 0, cells, 0, oldCells.length);
oldCells = null;
}
cells[col] = cv;
numColumns = Math.max(col+1, numColumns);
}
/**
* Removes a cell from this row
*
* @param col the column at which to remove the cell
*/
public void removeCell(int col)
{
// Grow the array if needs be
if (col >= numColumns)
{
return;
}
cells[col] = null;
}
/**
* Writes out the row information data (but not the individual cells)
*
* @exception IOException
* @param outputFile the output file
*/
public void write(File outputFile) throws IOException
{
outputFile.write(this);
}
/**
* Writes out all the cells in this row. If more than three integer
* values occur consecutively, then a MulRK record is used to group the
* numbers
*
* @exception IOException
* @param outputFile the output file
*/
public void writeCells(File outputFile)
throws IOException
{
// This is the list for integer values
ArrayList integerValues = new ArrayList();
boolean integerValue = false;
// Write out all the records
for (int i = 0; i < numColumns; i++)
{
integerValue = false;
if (cells[i] != null)
{
// See if this cell is a 30-bit integer value (without additional
// cell features)
if (cells[i].getType() == CellType.NUMBER)
{
Number nc = (Number) cells[i];
if (nc.getValue() == (int) nc.getValue() &&
nc.getValue() < maxRKValue &&
nc.getValue() > minRKValue &&
nc.getCellFeatures() == null)
{
integerValue = true;
}
}
if (integerValue)
{
// This cell is an integer, add it to the list
integerValues.add(cells[i]);
}
else
{
// This cell is not an integer. Write out whatever integers we
// have, and then write out this cell
writeIntegerValues(integerValues, outputFile);
outputFile.write(cells[i]);
// If the cell is a string formula, write out the string record
// immediately afterwards
if (cells[i].getType() == CellType.STRING_FORMULA)
{
StringRecord sr = new StringRecord(cells[i].getContents());
outputFile.write(sr);
}
}
}
else
{
// Cell does not exist. Write out the list of integers that
// we have
writeIntegerValues(integerValues, outputFile);
}
}
// All done. Write out any remaining integer values
writeIntegerValues(integerValues, outputFile);
}
/**
* Writes out the list of integer values. If there are more than three,
* a MulRK record is used, otherwise a sequence of Numbers is used
*
* @exception IOException
* @param outputFile the output file
* @param integerValues the array of integer values
*/
private void writeIntegerValues(ArrayList integerValues, File outputFile)
throws IOException
{
if (integerValues.size() == 0)
{
return;
}
if (integerValues.size() >= 3 )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -