📄 writablesheetimpl.java
字号:
RowRecord rowrec = getRowRecord(row);
rowrec.addCell(cv);
// Adjust the max rows and max columns accordingly
numRows = Math.max(row+1, numRows);
numColumns = Math.max(numColumns, rowrec.getMaxColumn());
// Indicate this cell is now part of a worksheet, so that it can't be
// added anywhere else
cv.setCellDetails(formatRecords, sharedStrings, this);
}
/**
* Gets the row record at the specified row number, growing the
* array as needs dictate
*
* @param row the row number we are interested in
* @return the row record at the specified row
* @exception RowsExceededException
*/
RowRecord getRowRecord(int row) throws RowsExceededException
{
if (row >= numRowsPerSheet)
{
throw new RowsExceededException();
}
// Grow the array of rows if needs be
// Thanks to Brendan for spotting the flaw in merely adding on the
// grow size
if (row >= rows.length)
{
RowRecord[] oldRows = rows;
rows = new RowRecord[Math.max(oldRows.length + rowGrowSize, row+1)];
System.arraycopy(oldRows, 0, rows, 0, oldRows.length);
oldRows = null;
}
RowRecord rowrec = rows[row];
if (rowrec == null)
{
rowrec = new RowRecord(row);
rows[row] = rowrec;
}
return rowrec;
}
/**
* Gets the row record for the specified row
*
* @param r the row
* @return the row record
*/
RowRecord getRowInfo(int r)
{
if (r < 0 || r > rows.length)
{
return null;
}
return rows[r];
}
/**
* Gets the column info record for the specified column
*
* @param c the column
* @return the column record
*/
ColumnInfoRecord getColumnInfo(int c)
{
Iterator i = columnFormats.iterator();
ColumnInfoRecord cir = null;
boolean stop = false;
while (i.hasNext() && !stop)
{
cir = (ColumnInfoRecord) i.next();
if (cir.getColumn() >= c)
{
stop = true;
}
}
if (!stop)
{
return null;
}
return cir.getColumn() == c ? cir : null;
}
/**
* Sets the name of this worksheet
*
* @param n the name of this sheet
*/
public void setName(String n)
{
name = n;
}
/**
* Sets the hidden status of this sheet
*
* @param h the hiden flag
* @deprecated Use the settings bean instead
*/
public void setHidden(boolean h)
{
settings.setHidden(h);
}
/**
* Indicates whether or not this sheet is protected
*
* @param prot protected flag
* @deprecated Use the settings bean instead
*/
public void setProtected(boolean prot)
{
settings.setProtected(prot);
}
/**
* Sets this sheet as selected
* @deprecated Use the settings bean
*/
public void setSelected()
{
settings.setSelected();
}
/**
* Retrieves the hidden status of this sheet
*
* @return TRUE if hidden, FALSE otherwise
* @deprecated Use the sheet settings bean instead
*/
public boolean isHidden()
{
return settings.isHidden();
}
/**
* Sets the width (in characters) for a particular column in this sheet
*
* @param col the column whose width to set
* @param width the width of the column in characters
*/
public void setColumnView(int col, int width)
{
CellView cv = new CellView();
cv.setSize(width * 256);
setColumnView(col, cv);
}
/**
* Sets the width (in characters) and format options for a
* particular column in this sheet
*
* @param col the column to set
* @param width the width in characters
* @param format the formt details for the column
*/
public void setColumnView(int col, int width, CellFormat format)
{
CellView cv = new CellView();
cv.setSize(width * 256);
cv.setFormat(format);
setColumnView(col, cv);
}
/**
* Sets the view for this column
*
* @param col the column on which to set the view
* @param view the view to set
*/
public void setColumnView(int col, CellView view)
{
XFRecord xfr = (XFRecord) view.getFormat();
if (xfr == null)
{
Styles styles = getWorkbook().getStyles();
xfr = styles.getNormalStyle();
}
try
{
if (!xfr.isInitialized())
{
formatRecords.addStyle(xfr);
}
int width = view.depUsed() ? view.getDimension() * 256 : view.getSize();
if (view.isAutosize())
{
autosizedColumns.add(new Integer(col));
}
ColumnInfoRecord cir = new ColumnInfoRecord(col,
width,
xfr);
if (view.isHidden())
{
cir.setHidden(true);
}
if (!columnFormats.contains(cir))
{
columnFormats.add(cir);
}
else
{
columnFormats.remove(cir);
columnFormats.add(cir);
}
}
catch (NumFormatRecordsException e)
{
logger.warn("Maximum number of format records exceeded. Using " +
"default format.");
ColumnInfoRecord cir = new ColumnInfoRecord
(col, view.getDimension()*256, WritableWorkbook.NORMAL_STYLE);
if (!columnFormats.contains(cir))
{
columnFormats.add(cir);
}
}
}
/**
* Sets the height of the specified row, as well as its collapse status
*
* @param row the row to be formatted
* @param height the row height in points
* @exception RowsExceededException
*/
public void setRowView(int row, int height) throws RowsExceededException
{
setRowView(row, height, false);
}
/**
* Sets the height of the specified row, as well as its collapse status
*
* @param row the row to be formatted
* @param collapsed indicates whether the row is collapsed
* @exception jxl.write.biff.RowsExceededException
*/
public void setRowView(int row, boolean collapsed)
throws RowsExceededException
{
RowRecord rowrec = getRowRecord(row);
rowrec.setCollapsed(collapsed);
}
/**
* Sets the height of the specified row, as well as its collapse status
*
* @param row the row to be formatted
* @param height the row height in 1/20th of a point
* @param collapsed indicates whether the row is collapsed
* @param zeroHeight indicates that the row has zero height
* @exception RowsExceededException
*/
public void setRowView(int row, int height,
boolean collapsed)
throws RowsExceededException
{
RowRecord rowrec = getRowRecord(row);
rowrec.setRowHeight(height);
rowrec.setCollapsed(collapsed);
}
/**
* Writes out this sheet. This functionality is delegated off to the
* SheetWriter class in order to reduce the bloated nature of this source
* file
*
* @exception IOException
*/
public void write() throws IOException
{
boolean dmod = drawingsModified;
if (workbook.getDrawingGroup() != null)
{
dmod |= workbook.getDrawingGroup().hasDrawingsOmitted();
}
if (autosizedColumns.size() > 0)
{
autosizeColumns();
}
sheetWriter.setWriteData(rows,
rowBreaks,
columnBreaks,
hyperlinks,
mergedCells,
columnFormats );
sheetWriter.setDimensions(getRows(), getColumns());
sheetWriter.setSettings(settings);
sheetWriter.setPLS(plsRecord);
sheetWriter.setDrawings(drawings, dmod);
sheetWriter.setButtonPropertySet(buttonPropertySet);
sheetWriter.setDataValidation(dataValidation, validatedCells);
sheetWriter.write();
}
/**
* Copies the cell contents from the specified sheet into this one
*
* @param s the sheet to copy
*/
private void copyCells(Sheet s)
{
// Copy the cells
int cells = s.getRows();
Cell[] row = null;
Cell cell = null;
for (int i = 0; i < cells; i++)
{
row = s.getRow(i);
for (int j = 0; j < row.length; j++)
{
cell = row[j];
CellType ct = cell.getType();
// Encase the calls to addCell in a try-catch block
// These should not generate any errors, because we are
// copying from an existing spreadsheet. In the event of
// errors, catch the exception and then bomb out with an
// assertion
try
{
if (ct == CellType.LABEL)
{
Label l = new Label((LabelCell) cell);
addCell(l);
}
else if (ct == CellType.NUMBER)
{
Number n = new Number((NumberCell) cell);
addCell(n);
}
else if (ct == CellType.DATE)
{
DateTime dt = new DateTime((DateCell) cell);
addCell(dt);
}
else if (ct == CellType.BOOLEAN)
{
Boolean b = new Boolean((BooleanCell) cell);
addCell(b);
}
else if (ct == CellType.NUMBER_FORMULA)
{
ReadNumberFormulaRecord fr = new ReadNumberFormulaRecord
((FormulaData) cell);
addCell(fr);
}
else if (ct == CellType.STRING_FORMULA)
{
ReadStringFormulaRecord fr = new ReadStringFormulaRecord
((FormulaData) cell);
addCell(fr);
}
else if( ct == CellType.BOOLEAN_FORMULA)
{
ReadBooleanFormulaRecord fr = new ReadBooleanFormulaRecord
((FormulaData) cell);
addCell(fr);
}
else if (ct == CellType.DATE_FORMULA)
{
ReadDateFormulaRecord fr = new ReadDateFormulaRecord
((FormulaData) cell);
addCell(fr);
}
else if(ct == CellType.FORMULA_ERROR)
{
ReadErrorFormulaRecord fr = new ReadErrorFormulaRecord
((FormulaData) cell);
addCell(fr);
}
else if (ct == CellType.EMPTY)
{
if (cell.getCellFormat() != null)
{
// It is a blank cell, rather than an empty cell, so
// it may have formatting information, so
// it must be copied
Blank b = new Blank(cell);
addCell(b);
}
}
}
catch (WriteException e)
{
Assert.verify(false);
}
}
}
}
/**
* Copies the cell contents from the specified sheet into this one. This
* differs from the previous method in that it uses deep copies to copy
* each cell
* Used for creating duplicates of WritableSheets
*
* @param s the sheet to copy
*/
private void copyCells(WritableSheet s)
{
// Copy the cells
int cells = s.getRows();
Cell[] row = null;
Cell cell = null;
for (int i = 0; i < cells; i++)
{
row = s.getRow(i);
for (int j = 0; j < row.length; j++)
{
cell = row[j];
try
{
WritableCell wc = ( (WritableCell) cell).copyTo(cell.getColumn(),
cell.getRow());
addCell(wc);
}
catch (WriteException e)
{
Assert.verify(false);
}
}
}
}
/**
* Copies the specified sheet, row by row and cell by cell
*
* @param s the sheet to copy
*/
void copy(Sheet s)
{
// Copy the settings
settings = new SheetSettings(s.getSettings());
SheetCopier si = new SheetCopier(s, this);
si.setColumnFormats(columnFormats);
si.setFormatRecords(formatRecords);
si.setHyperlinks(hyperlinks);
si.setMergedCells(mergedCells);
si.setRowBreaks(rowBreaks);
si.setColumnBreaks(columnBreaks);
si.setSheetWriter(sheetWriter);
si.setDrawings(drawings);
si.setImages(images);
si.copySheet();
dataValidation = si.getDataValidation();
comboBox = si.getComboBox();
plsRecord = si.getPLSRecord();
chartOnly = si.isChartOnly();
buttonPropertySet = si.getButtonPropertySet();
}
/**
* Copies the specified sheet, row by row and cell by cell
*
* @param s the sheet to copy
*/
void copy(WritableSheet s)
{
settings = new SheetSettings(s.getSettings());
copyCells(s);
// Copy the column formats
Iterator cfit = ( (WritableSheetImpl) s).columnFormats.iterator();
while (cfit.hasNext())
{
ColumnInfoRecord cv = new ColumnInfoRecord
((ColumnInfoRecord) cfit.next());
columnFormats.add(cv);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -