📄 writablesheetimpl.java
字号:
* Check all the merged cells for borders. Although in an OO sense the
* logic should belong in this class, in order to reduce the bloated
* nature of the source code for this object this logic has been delegated
* to the SheetWriter
*/
void checkMergedBorders()
{
sheetWriter.setWriteData(rows,
rowBreaks,
columnBreaks,
hyperlinks,
mergedCells,
columnFormats );
sheetWriter.setDimensions(getRows(), getColumns());
sheetWriter.checkMergedBorders();
}
/**
* Accessor for the workspace options
*
* @return the workspace options
*/
private WorkspaceInformationRecord getWorkspaceOptions()
{
return sheetWriter.getWorkspaceOptions();
}
/**
* Rationalizes the sheets xf index mapping
* @param xfMapping the index mapping for XFRecords
* @param fontMapping the index mapping for fonts
* @param formatMapping the index mapping for formats
*/
void rationalize(IndexMapping xfMapping,
IndexMapping fontMapping,
IndexMapping formatMapping)
{
// Rationalize the column formats
for (Iterator i = columnFormats.iterator() ; i.hasNext() ;)
{
ColumnInfoRecord cir = (ColumnInfoRecord) i.next();
cir.rationalize(xfMapping);
}
// Rationalize the row formats
for (int i = 0; i < rows.length ; i++)
{
if (rows[i] != null)
{
rows[i].rationalize(xfMapping);
}
}
// Rationalize any data that appears on the charts
Chart[] charts = getCharts();
for (int c = 0; c < charts.length; c++)
{
charts[c].rationalize(xfMapping, fontMapping, formatMapping);
}
}
/**
* Accessor for the workbook
* @return the workbook
*/
WritableWorkbookImpl getWorkbook()
{
return workbook;
}
/**
* Gets the column format for the specified column
*
* @param col the column number
* @return the column format, or NULL if the column has no specific format
* @deprecated Use getColumnView instead
*/
public CellFormat getColumnFormat(int col)
{
return getColumnView(col).getFormat();
}
/**
* Gets the column width for the specified column
*
* @param col the column number
* @return the column width, or the default width if the column has no
* specified format
* @deprecated Use getColumnView instead
*/
public int getColumnWidth(int col)
{
return getColumnView(col).getDimension();
}
/**
* Gets the column width for the specified column
*
* @param row the column number
* @return the row height, or the default height if the column has no
* specified format
* @deprecated Use getRowView instead
*/
public int getRowHeight(int row)
{
return getRowView(row).getDimension();
}
/**
* Accessor for the chart only method
*
* @return TRUE if this is a chart only, FALSE otherwise
*/
boolean isChartOnly()
{
return chartOnly;
}
/**
* Gets the row view for the specified row
*
* @param col the row number
* @return the row format, or the default format if no override is
specified
*/
public CellView getRowView(int row)
{
CellView cv = new CellView();
try
{
RowRecord rr = getRowRecord(row);
if (rr == null || rr.isDefaultHeight())
{
cv.setDimension(settings.getDefaultRowHeight());
cv.setSize(settings.getDefaultRowHeight());
}
else if (rr.isCollapsed())
{
cv.setHidden(true);
}
else
{
cv.setDimension(rr.getRowHeight());
cv.setSize(rr.getRowHeight());
}
return cv;
}
catch (RowsExceededException e)
{
// Simple return the default
cv.setDimension(settings.getDefaultRowHeight());
cv.setSize(settings.getDefaultRowHeight());
return cv;
}
}
/**
* Gets the column width for the specified column
*
* @param col the column number
* @return the column format, or the default format if no override is
specified
*/
public CellView getColumnView(int col)
{
ColumnInfoRecord cir = getColumnInfo(col);
CellView cv = new CellView();
if (cir != null)
{
cv.setDimension(cir.getWidth()/256);
cv.setSize(cir.getWidth());
cv.setHidden(cir.getHidden());
cv.setFormat(cir.getCellFormat());
}
else
{
cv.setDimension(settings.getDefaultColumnWidth()/256);
cv.setSize(settings.getDefaultColumnWidth());
}
return cv;
}
/**
* Adds an image to this sheet
*
* @param image the image to add
*/
public void addImage(WritableImage image)
{
boolean supported = false;
java.io.File imageFile = image.getImageFile();
String fileType = "?";
if (imageFile != null)
{
String fileName = imageFile.getName();
int fileTypeIndex = fileName.lastIndexOf('.');
fileType = fileTypeIndex != -1 ?
fileName.substring(fileTypeIndex+1) : "";
for (int i = 0 ; i < imageTypes.length && !supported ; i++)
{
if (fileType.equalsIgnoreCase(imageTypes[i]))
{
supported = true;
}
}
}
else
{
supported = true;
}
if (supported)
{
workbook.addDrawing(image);
drawings.add(image);
images.add(image);
}
else
{
StringBuffer message = new StringBuffer("Image type ");
message.append(fileType);
message.append(" not supported. Supported types are ");
message.append(imageTypes[0]);
for (int i = 1 ; i < imageTypes.length ; i++)
{
message.append(", ");
message.append(imageTypes[i]);
}
logger.warn(message.toString());
}
}
/**
* Gets the number of images on this sheet
*
* @return the number of images on this sheet
*/
public int getNumberOfImages()
{
return images.size();
}
/**
* Accessor for a particular image on this sheet
*
* @param i the 0-based image index number
* @return the image with the specified index number
*/
public WritableImage getImage(int i)
{
return (WritableImage) images.get(i);
}
/**
* Accessor for a particular image on this sheet
*
* @param i the 0-based image index number
* @return the image with the specified index number
*/
public Image getDrawing(int i)
{
return (Image) images.get(i);
}
/**
* Removes the specified image from this sheet. The image passed in
* must be the same instance as that retrieved from a getImage call
*
* @param wi the image to remove
*/
public void removeImage(WritableImage wi)
{
drawings.remove(wi);
images.remove(wi);
drawingsModified = true;
workbook.removeDrawing(wi);
}
/**
* Validates the sheet name
*/
private String validateName(String n)
{
if (n.length() > maxSheetNameLength)
{
logger.warn("Sheet name " + n + " too long - truncating");
n = n.substring(0, maxSheetNameLength);
}
if (n.charAt(0) == '\'')
{
logger.warn("Sheet naming cannot start with \' - removing");
n = n.substring(1);
}
for (int i = 0 ; i < illegalSheetNameCharacters.length ; i++)
{
String newname = n.replace(illegalSheetNameCharacters[i], '@');
if (n != newname)
{
logger.warn(illegalSheetNameCharacters[i] +
" is not a valid character within a sheet name - replacing");
}
n = newname;
}
return n;
}
/**
* Adds a drawing to the list - typically used for comments
*
* @param the drawing to add
*/
void addDrawing(DrawingGroupObject o)
{
drawings.add(o);
Assert.verify(!(o instanceof Drawing));
}
/**
* Removes a drawing to the list - typically used for comments
*
* @param the drawing to add
*/
void removeDrawing(DrawingGroupObject o)
{
int origSize = drawings.size();
drawings.remove(o);
int newSize = drawings.size();
drawingsModified = true;
Assert.verify(newSize == origSize -1);
}
/**
* Accessor for the page breaks on this sheet
*
* @return the page breaks on this sheet
*/
public int[] getRowPageBreaks()
{
int[] rb = new int[rowBreaks.size()];
int pos = 0;
for (Iterator i = rowBreaks.iterator(); i.hasNext() ; pos++)
{
rb[pos] = ( (Integer) i.next()).intValue();
}
return rb;
}
/**
* Accessor for the page breaks on this sheet
*
* @return the page breaks on this sheet
*/
public int[] getColumnPageBreaks()
{
int[] rb = new int[columnBreaks.size()];
int pos = 0;
for (Iterator i = columnBreaks.iterator(); i.hasNext() ; pos++)
{
rb[pos] = ( (Integer) i.next()).intValue();
}
return rb;
}
/**
* Flags the added cell as having data validation
*
* @param cell the cell with data validation
*/
void addValidationCell(CellValue cv)
{
validatedCells.add(cv);
}
/**
* Accessor for the combo box object used for list data validations on this
* sheet
*
* @return the combo box
*/
ComboBox getComboBox()
{
return comboBox;
}
/**
* Sets the combo box object used for list validations on this sheet
*
* @param cb the combo box
*/
void setComboBox(ComboBox cb)
{
comboBox = cb;
}
/**
* Gets the data validation. Retrieved by CellValue when copying sheets
*/
public DataValidation getDataValidation()
{
return dataValidation;
}
/**
* Performs the column autosizing
*/
private void autosizeColumns()
{
Iterator i = autosizedColumns.iterator();
while (i.hasNext())
{
Integer col = (Integer) i.next();
autosizeColumn(col.intValue());
}
}
/**
* Autosizes the specified column
*
* @param col the column to autosize
*/
private void autosizeColumn(int col)
{
int maxWidth = 0;
ColumnInfoRecord cir = getColumnInfo(col);
Font columnFont = cir.getCellFormat().getFont();
Font defaultFont = WritableWorkbook.NORMAL_STYLE.getFont();
for (int i = 0 ; i < numRows; i++)
{
Cell cell = null;
if (rows[i] != null)
{
cell = rows[i].getCell(col);
}
if (cell != null)
{
String contents = cell.getContents();
Font font = cell.getCellFormat().getFont();
Font activeFont = font.equals(defaultFont) ? columnFont : font;
int pointSize = activeFont.getPointSize();
int numChars = contents.length();
if (activeFont.isItalic() ||
activeFont.getBoldWeight() > 400) // magic value for normal bold
{
numChars += 2;
}
int points = numChars * pointSize;
maxWidth = Math.max(maxWidth, points * 256);
}
}
cir.setWidth((int) (maxWidth / defaultFont.getPointSize()));
}
/**
* Imports a sheet from a different workbook
*
* @param s the sheet to import
*/
void importSheet(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.importSheet();
dataValidation = si.getDataValidation();
comboBox = si.getComboBox();
plsRecord = si.getPLSRecord();
chartOnly = si.isChartOnly();
buttonPropertySet = si.getButtonPropertySet();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -