📄 writablesheetimpl.java
字号:
}
/**
* Adds the specified hyperlink
*
* @param the hyperlink
* @exception WriteException
* @exception RowsExceededException
*/
public void addHyperlink(WritableHyperlink h)
throws WriteException, RowsExceededException
{
// First set the label on the sheet
Cell c = getCell(h.getColumn(), h.getRow());
String contents = null;
if (h.isFile())
{
contents = h.getFile().getPath();
}
else if (h.isURL())
{
contents = h.getURL().toString();
}
else if (h.isLocation())
{
contents = ( (HyperlinkRecord) h).getContents();
}
if (c.getType() == CellType.LABEL)
{
Label l = (Label) c;
l.setString(contents);
l.setCellFormat(WritableWorkbook.HYPERLINK_STYLE);
}
else
{
Label l = new Label(h.getColumn(), h.getRow(), contents,
WritableWorkbook.HYPERLINK_STYLE);
addCell(l);
}
// Set all other cells within range to be empty
for (int i = h.getRow(); i <= h.getLastRow(); i++)
{
for (int j = h.getColumn(); j <= h.getLastColumn(); j++)
{
if (i != h.getRow() && j != h.getColumn())
{
// Set the cell to be empty
if (rows[i] != null)
{
rows[i].removeCell(j);
}
}
}
}
((HyperlinkRecord) h).initialize(this);
hyperlinks.add(h);
}
/**
* Merges the specified cells. Any clashes or intersections between
* merged cells are resolved when the spreadsheet is written out
*
* @param col1 the column number of the top left cell
* @param row1 the row number of the top left cell
* @param col2 the column number of the bottom right cell
* @param row2 the row number of the bottom right cell
* @return the Range object representing the merged cells
* @exception jxl.write..WriteException
* @exception jxl.write.biff.RowsExceededException
*/
public Range mergeCells(int col1, int row1, int col2, int row2)
throws WriteException, RowsExceededException
{
// First check that the cells make sense
if (col2 < col1 || row2 < row1)
{
logger.warn("Cannot merge cells - top left and bottom right "+
"incorrectly specified");
}
// Make sure the spreadsheet is up to size
if (col2 >= numColumns || row2 >= numRows)
{
addCell(new Blank(col2, row2));
}
SheetRangeImpl range = new SheetRangeImpl(this, col1, row1, col2, row2);
mergedCells.add(range);
return range;
}
/**
* Unmerges the specified cells. The Range passed in should be one that
* has been previously returned as a result of the getMergedCells method
*
* @param r the range of cells to unmerge
*/
public void unmergeCells(Range r)
{
mergedCells.unmergeCells(r);
}
/**
* Sets the header for this page
*
* @param l the print header to print on the left side
* @param c the print header to print in the centre
* @param r the print header to print on the right hand side
*/
public void setHeader(String l, String c, String r)
{
sheetWriter.setHeader(new HeaderRecord(l, c, r));
}
/**
* Sets the footer for this page
*
* @param l the print header to print on the left side
* @param c the print header to print in the centre
* @param r the print header to print on the right hand side
*/
public void setFooter(String l, String c, String r)
{
sheetWriter.setFooter(new FooterRecord(l, c, r));
}
/**
* Sets the page setup details
*
* @param p the page orientation
* @deprecated use the SheetSettings bean
*/
public void setPageSetup(PageOrientation p)
{
settings.setOrientation(p);
}
/**
* Sets the page setup details
*
* @param p the page orientation
* @param hm the header margin, in inches
* @param fm the footer margin, in inches
* @deprecated use the SheetSettings bean
*/
public void setPageSetup(PageOrientation p, double hm, double fm)
{
settings.setOrientation(p);
settings.setHeaderMargin(hm);
settings.setFooterMargin(fm);
}
/**
* Sets the page setup details
*
* @param p the page orientation
* @param ps the paper size
* @param hm the header margin, in inches
* @param fm the footer margin, in inches
* @deprecated use the SheetSettings bean
*/
public void setPageSetup(PageOrientation p, PaperSize ps,
double hm, double fm)
{
settings.setPaperSize(ps);
settings.setOrientation(p);
settings.setHeaderMargin(hm);
settings.setFooterMargin(fm);
}
/**
* Gets the settings for this sheet
*
* @return the page settings bean
*/
public SheetSettings getSettings()
{
return settings;
}
/**
* Gets the workbook settings
*/
WorkbookSettings getWorkbookSettings()
{
return workbookSettings;
}
/**
* Forces a page break at the specified row
*
* @param row the row to break at
*/
public void addRowPageBreak(int row)
{
// First check that the row is not already present
Iterator i = rowBreaks.iterator();
boolean found = false;
while (i.hasNext() && !found)
{
if (( (Integer) i.next()).intValue() == row)
{
found = true;
}
}
if (!found)
{
rowBreaks.add(new Integer(row));
}
}
/**
* Accessor for the charts. Used when copying
*
* @return the charts on this sheet
*/
private Chart[] getCharts()
{
return sheetWriter.getCharts();
}
/**
* Accessor for the drawings. Used when copying
*
* @return the drawings on this sheet
*/
private Drawing[] getDrawings()
{
Drawing[] dr = new Drawing[drawings.size()];
System.arraycopy(drawings.toArray(), 0, dr, 0, dr.length);
return dr;
}
/**
* 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,
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 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());
}
else if (rr.isCollapsed())
{
cv.setHidden(true);
}
else
{
cv.setDimension(rr.getRowHeight());
}
return cv;
}
catch (RowsExceededException e)
{
// Simple return the default
cv.setDimension(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.setHidden(cir.getHidden());
cv.setFormat(cir.getCellFormat());
}
else
{
cv.setDimension(settings.getDefaultColumnWidth());
}
return cv;
}
/**
* Adds an image to this sheet
*
* @param image the image to add
*/
public void addImage(WritableImage image)
{
java.io.File imageFile = image.getImageFile();
String fileName = imageFile.getName();
int fileTypeIndex = fileName.lastIndexOf('.');
String fileType = fileTypeIndex != -1 ?
fileName.substring(fileTypeIndex+1) : "";
boolean supported = false;
for (int i = 0 ; i < imageTypes.length && !supported ; i++)
{
if (fileType.equalsIgnoreCase(imageTypes[i]))
{
supported = true;
}
}
if (supported)
{
workbook.addDrawing(image);
drawings.add(image);
}
else
{
System.err.print("Warning: image type " + fileType + " not supported. Supported types are " + imageTypes[0]);
for (int i = 1 ; i < imageTypes.length ; i++)
{
System.err.print(", ");
System.err.print(imageTypes[i]);
}
System.err.println();
}
}
/**
* Gets the number of images on this sheet
*
* @return the number of images on this sheet
*/
public int getNumberOfImages()
{
return drawings.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) drawings.get(i);
}
/**
* Removes the specified image from this sheet. The image passed in
* must be the same isntance as that retrieved from a getImage call
*
* @param wi the image to remove
*/
public void removeImage(WritableImage wi)
{
boolean removed = drawings.remove(wi);
workbook.removeDrawing(wi);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -