📄 sheetcopier.java
字号:
for (int i = 0; i < cf.length ; i++)
{
conditionalFormats.add(cf[i]);
}
}
// Get the autofilter
autoFilter = fromSheet.getAutoFilter();
// Copy the workspace options
sheetWriter.setWorkspaceOptions(fromSheet.getWorkspaceOptions());
// Set a flag to indicate if it contains a chart only
if (fromSheet.getSheetBof().isChart())
{
chartOnly = true;
sheetWriter.setChartOnly();
}
// Copy the environment specific print record
if (fromSheet.getPLS() != null)
{
if (fromSheet.getWorkbookBof().isBiff7())
{
logger.warn("Cannot copy Biff7 print settings record - ignoring");
}
else
{
plsRecord = new PLSRecord(fromSheet.getPLS());
}
}
// Copy the button property set
if (fromSheet.getButtonPropertySet() != null)
{
buttonPropertySet = new ButtonPropertySetRecord
(fromSheet.getButtonPropertySet());
}
// Copy the outline levels
maxRowOutlineLevel = fromSheet.getMaxRowOutlineLevel();
maxColumnOutlineLevel = fromSheet.getMaxColumnOutlineLevel();
}
/**
* Copies a sheet from a read-only version to the writable version.
* Performs shallow copies
*/
public void copyWritableSheet()
{
shallowCopyCells();
/*
// Copy the column formats
Iterator cfit = fromWritableSheet.columnFormats.iterator();
while (cfit.hasNext())
{
ColumnInfoRecord cv = new ColumnInfoRecord
((ColumnInfoRecord) cfit.next());
columnFormats.add(cv);
}
// Copy the merged cells
Range[] merged = fromWritableSheet.getMergedCells();
for (int i = 0; i < merged.length; i++)
{
mergedCells.add(new SheetRangeImpl((SheetRangeImpl)merged[i], this));
}
// Copy the row properties
try
{
RowRecord[] copyRows = fromWritableSheet.rows;
RowRecord row = null;
for (int i = 0; i < copyRows.length ; i++)
{
row = copyRows[i];
if (row != null &&
(!row.isDefaultHeight() ||
row.isCollapsed()))
{
RowRecord rr = getRowRecord(i);
rr.setRowDetails(row.getRowHeight(),
row.matchesDefaultFontHeight(),
row.isCollapsed(),
row.getStyle());
}
}
}
catch (RowsExceededException e)
{
// Handle the rows exceeded exception - this cannot occur since
// the sheet we are copying from will have a valid number of rows
Assert.verify(false);
}
// Copy the horizontal page breaks
rowBreaks = new ArrayList(fromWritableSheet.rowBreaks);
// Copy the vertical page breaks
columnBreaks = new ArrayList(fromWritableSheet.columnBreaks);
// Copy the data validations
DataValidation rdv = fromWritableSheet.dataValidation;
if (rdv != null)
{
dataValidation = new DataValidation(rdv,
workbook,
workbook,
workbookSettings);
}
// Copy the charts
sheetWriter.setCharts(fromWritableSheet.getCharts());
// Copy the drawings
DrawingGroupObject[] dr = si.getDrawings();
for (int i = 0 ; i < dr.length ; i++)
{
if (dr[i] instanceof jxl.biff.drawing.Drawing)
{
WritableImage wi = new WritableImage(dr[i],
workbook.getDrawingGroup());
drawings.add(wi);
images.add(wi);
}
// Not necessary to copy the comments, as they will be handled by
// the deep copy of the individual cells
}
// Copy the workspace options
sheetWriter.setWorkspaceOptions(fromWritableSheet.getWorkspaceOptions());
// Copy the environment specific print record
if (fromWritableSheet.plsRecord != null)
{
plsRecord = new PLSRecord(fromWritableSheet.plsRecord);
}
// Copy the button property set
if (fromWritableSheet.buttonPropertySet != null)
{
buttonPropertySet = new ButtonPropertySetRecord
(fromWritableSheet.buttonPropertySet);
}
*/
}
/**
* Imports a sheet from a different workbook, doing a deep copy
*/
public void importSheet()
{
xfRecords = new HashMap();
fonts = new HashMap();
formats = new HashMap();
deepCopyCells();
// Copy the column info records
jxl.read.biff.ColumnInfoRecord[] readCirs = fromSheet.getColumnInfos();
for (int i = 0 ; i < readCirs.length; i++)
{
jxl.read.biff.ColumnInfoRecord rcir = readCirs[i];
for (int j = rcir.getStartColumn(); j <= rcir.getEndColumn() ; j++)
{
ColumnInfoRecord cir = new ColumnInfoRecord(rcir, j);
int xfIndex = cir.getXfIndex();
XFRecord cf = (WritableCellFormat) xfRecords.get(new Integer(xfIndex));
if (cf == null)
{
CellFormat readFormat = fromSheet.getColumnView(j).getFormat();
WritableCellFormat wcf = copyCellFormat(readFormat);
}
cir.setCellFormat(cf);
cir.setHidden(rcir.getHidden());
columnFormats.add(cir);
}
}
// Copy the hyperlinks
Hyperlink[] hls = fromSheet.getHyperlinks();
for (int i = 0 ; i < hls.length; i++)
{
WritableHyperlink hr = new WritableHyperlink
(hls[i], toSheet);
hyperlinks.add(hr);
}
// Copy the merged cells
Range[] merged = fromSheet.getMergedCells();
for (int i = 0; i < merged.length; i++)
{
mergedCells.add(new SheetRangeImpl((SheetRangeImpl)merged[i], toSheet));
}
// Copy the row properties
try
{
jxl.read.biff.RowRecord[] rowprops = fromSheet.getRowProperties();
for (int i = 0; i < rowprops.length; i++)
{
RowRecord rr = toSheet.getRowRecord(rowprops[i].getRowNumber());
XFRecord format = null;
jxl.read.biff.RowRecord rowrec = rowprops[i];
if (rowrec.hasDefaultFormat())
{
format = (WritableCellFormat)
xfRecords.get(new Integer(rowrec.getXFIndex()));
if (format == null)
{
int rownum = rowrec.getRowNumber();
CellFormat readFormat = fromSheet.getRowView(rownum).getFormat();
WritableCellFormat wcf = copyCellFormat(readFormat);
}
}
rr.setRowDetails(rowrec.getRowHeight(),
rowrec.matchesDefaultFontHeight(),
rowrec.isCollapsed(),
rowrec.getOutlineLevel(),
rowrec.getGroupStart(),
format);
numRows = Math.max(numRows, rowprops[i].getRowNumber() + 1);
}
}
catch (RowsExceededException e)
{
// Handle the rows exceeded exception - this cannot occur since
// the sheet we are copying from will have a valid number of rows
Assert.verify(false);
}
// Copy the headers and footers
// sheetWriter.setHeader(new HeaderRecord(si.getHeader()));
// sheetWriter.setFooter(new FooterRecord(si.getFooter()));
// Copy the page breaks
int[] rowbreaks = fromSheet.getRowPageBreaks();
if (rowbreaks != null)
{
for (int i = 0; i < rowbreaks.length; i++)
{
rowBreaks.add(new Integer(rowbreaks[i]));
}
}
int[] columnbreaks = fromSheet.getColumnPageBreaks();
if (columnbreaks != null)
{
for (int i = 0; i < columnbreaks.length; i++)
{
columnBreaks.add(new Integer(columnbreaks[i]));
}
}
// Copy the charts
Chart[] fromCharts = fromSheet.getCharts();
if (fromCharts != null && fromCharts.length > 0)
{
logger.warn("Importing of charts is not supported");
/*
sheetWriter.setCharts(fromSheet.getCharts());
IndexMapping xfMapping = new IndexMapping(200);
for (Iterator i = xfRecords.keySet().iterator(); i.hasNext();)
{
Integer key = (Integer) i.next();
XFRecord xfmapping = (XFRecord) xfRecords.get(key);
xfMapping.setMapping(key.intValue(), xfmapping.getXFIndex());
}
IndexMapping fontMapping = new IndexMapping(200);
for (Iterator i = fonts.keySet().iterator(); i.hasNext();)
{
Integer key = (Integer) i.next();
Integer fontmap = (Integer) fonts.get(key);
fontMapping.setMapping(key.intValue(), fontmap.intValue());
}
IndexMapping formatMapping = new IndexMapping(200);
for (Iterator i = formats.keySet().iterator(); i.hasNext();)
{
Integer key = (Integer) i.next();
Integer formatmap = (Integer) formats.get(key);
formatMapping.setMapping(key.intValue(), formatmap.intValue());
}
// Now reuse the rationalization feature on each chart to
// handle the new fonts
for (int i = 0; i < fromCharts.length ; i++)
{
fromCharts[i].rationalize(xfMapping, fontMapping, formatMapping);
}
*/
}
// Copy the drawings
DrawingGroupObject[] dr = fromSheet.getDrawings();
// Make sure the destination workbook has a drawing group
// created in it
if (dr.length > 0 &&
toSheet.getWorkbook().getDrawingGroup() == null)
{
toSheet.getWorkbook().createDrawingGroup();
}
for (int i = 0 ; i < dr.length ; i++)
{
if (dr[i] instanceof jxl.biff.drawing.Drawing)
{
WritableImage wi = new WritableImage
(dr[i].getX(), dr[i].getY(),
dr[i].getWidth(), dr[i].getHeight(),
dr[i].getImageData());
toSheet.getWorkbook().addDrawing(wi);
drawings.add(wi);
images.add(wi);
}
else if (dr[i] instanceof jxl.biff.drawing.Comment)
{
jxl.biff.drawing.Comment c =
new jxl.biff.drawing.Comment(dr[i],
toSheet.getWorkbook().getDrawingGroup(),
workbookSettings);
drawings.add(c);
// Set up the reference on the cell value
CellValue cv = (CellValue) toSheet.getWritableCell(c.getColumn(),
c.getRow());
Assert.verify(cv.getCellFeatures() != null);
cv.getWritableCellFeatures().setCommentDrawing(c);
}
else if (dr[i] instanceof jxl.biff.drawing.Button)
{
jxl.biff.drawing.Button b =
new jxl.biff.drawing.Button
(dr[i],
toSheet.getWorkbook().getDrawingGroup(),
workbookSettings);
drawings.add(b);
}
else if (dr[i] instanceof jxl.biff.drawing.ComboBox)
{
jxl.biff.drawing.ComboBox cb =
new jxl.biff.drawing.ComboBox
(dr[i],
toSheet.getWorkbook().getDrawingGroup(),
workbookSettings);
drawings.add(cb);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -