📄 writableworkbookimpl.java~
字号:
{
wsheet = (WritableSheetImpl) getSheet(0);
wsheet.getSettings().setSelected(true);
}
// Write out the sheets
for (int i = 0; i < getNumberOfSheets(); i++)
{
// first go back and modify the offset we wrote out for the
// boundsheet record
outputFile.setData
(IntegerHelper.getFourBytes(outputFile.getPos()),
boundsheetPos[i] + 4);
wsheet = (WritableSheetImpl) getSheet(i);
wsheet.write();
}
}
/**
* Produces a writable copy of the workbook passed in by
* creating copies of each sheet in the specified workbook and adding
* them to its own record
*
* @param w the workbook to copy
*/
private void copyWorkbook(Workbook w)
{
int numSheets = w.getNumberOfSheets();
wbProtected = w.isProtected();
Sheet s = null;
WritableSheetImpl ws = null;
for (int i = 0 ; i < numSheets; i++)
{
s = w.getSheet(i);
ws = (WritableSheetImpl) createSheet(s.getName(),i, false);
ws.copy(s);
}
}
/**
* Copies the specified sheet and places it at the index
* specified by the parameter
*
* @param s the index of the sheet to copy
* @param name the name of the new sheet
* @param index the position of the new sheet
*/
public void copySheet(int s, String name, int index)
{
WritableSheet sheet = getSheet(s);
WritableSheetImpl ws = (WritableSheetImpl) createSheet(name, index);
ws.copy(sheet);
}
/**
* Copies the specified sheet and places it at the index
* specified by the parameter
*
* @param s the name of the sheet to copy
* @param name the name of the new sheet
* @param index the position of the new sheet
*/
public void copySheet(String s, String name, int index)
{
WritableSheet sheet = getSheet(s);
WritableSheetImpl ws = (WritableSheetImpl) createSheet(name, index);
ws.copy(sheet);
}
/**
* Indicates whether or not this workbook is protected
*
* @param prot protected flag
*/
public void setProtected(boolean prot)
{
wbProtected = prot;
}
/**
* Rationalizes the cell formats, and then passes the resultant XF index
* mappings to each sheet in turn
*/
private void rationalize()
{
IndexMapping fontMapping = formatRecords.rationalizeFonts();
IndexMapping formatMapping = formatRecords.rationalizeDisplayFormats();
IndexMapping xfMapping = formatRecords.rationalize(fontMapping,
formatMapping);
WritableSheetImpl wsi = null;
for (int i = 0; i < sheets.size(); i++)
{
wsi = (WritableSheetImpl) sheets.get(i);
wsi.rationalize(xfMapping, fontMapping, formatMapping);
}
}
/**
* Gets the internal sheet index for a sheet name
*
* @param name the sheet name
* @return the internal sheet index
*/
private int getInternalSheetIndex(String name)
{
int index = -1;
String[] names = getSheetNames();
for (int i = 0 ; i < names.length; i++)
{
if (name.equals(names[i]))
{
index = i;
break;
}
}
return index;
}
/**
* Gets the name of the external sheet specified by the index
*
* @param index the external sheet index
* @return the name of the external sheet
*/
public String getExternalSheetName(int index)
{
int supbookIndex = externSheet.getSupbookIndex(index);
SupbookRecord sr = (SupbookRecord) supbooks.get(supbookIndex);
int firstTab = externSheet.getFirstTabIndex(index);
if (sr.getType() == SupbookRecord.INTERNAL)
{
// It's an internal reference - get the name from the sheets list
WritableSheet ws = getSheet(firstTab);
return ws.getName();
}
else if (sr.getType() == SupbookRecord.EXTERNAL)
{
String name = sr.getFileName() + sr.getSheetName(firstTab);
return name;
}
// An unknown supbook - return unkown
return "[UNKNOWN]";
}
/**
* Gets the name of the last external sheet specified by the index
*
* @param index the external sheet index
* @return the name of the external sheet
*/
public String getLastExternalSheetName(int index)
{
int supbookIndex = externSheet.getSupbookIndex(index);
SupbookRecord sr = (SupbookRecord) supbooks.get(supbookIndex);
int lastTab = externSheet.getLastTabIndex(index);
if (sr.getType() == SupbookRecord.INTERNAL)
{
// It's an internal reference - get the name from the sheets list
WritableSheet ws = getSheet(lastTab);
return ws.getName();
}
else if (sr.getType() == SupbookRecord.EXTERNAL)
{
Assert.verify(false);
}
// An unknown supbook - return unkown
return "[UNKNOWN]";
}
/**
* Parsing of formulas is only supported for a subset of the available
* biff version, so we need to test to see if this version is acceptable
*
* @return the BOF record, which
*/
public jxl.read.biff.BOFRecord getWorkbookBof()
{
return null;
}
/**
* Gets the index of the external sheet for the name
*
* @param sheetName
* @return the sheet index of the external sheet index
*/
public int getExternalSheetIndex(int index)
{
if (externSheet == null)
{
return index;
}
Assert.verify(externSheet != null);
int firstTab = externSheet.getFirstTabIndex(index);
return firstTab;
}
/**
* Gets the index of the external sheet for the name
*
* @param sheetName
* @return the sheet index of the external sheet index
*/
public int getLastExternalSheetIndex(int index)
{
if (externSheet == null)
{
return index;
}
Assert.verify(externSheet != null);
int lastTab = externSheet.getLastTabIndex(index);
return lastTab;
}
/**
* Gets the external sheet index for the sheet name
*
* @param sheetName
* @return the sheet index or -1 if the sheet could not be found
*/
public int getExternalSheetIndex(String sheetName)
{
if (externSheet == null)
{
externSheet = new ExternalSheetRecord();
supbooks = new ArrayList();
supbooks.add(new SupbookRecord(getNumberOfSheets(), settings));
}
// Iterate through the sheets records
boolean found = false;
Iterator i = sheets.iterator();
int sheetpos = 0;
WritableSheetImpl s = null;
while (i.hasNext() && !found)
{
s = (WritableSheetImpl) i.next();
if (s.getName().equals(sheetName))
{
found = true;
}
else
{
sheetpos++;
}
}
if (found)
{
// Check that the supbook record at position zero is internal and
// contains all the sheets
SupbookRecord supbook = (SupbookRecord) supbooks.get(0);
if (supbook.getType() != SupbookRecord.INTERNAL ||
supbook.getNumberOfSheets() != getNumberOfSheets())
{
logger.warn("Cannot find sheet " + sheetName + " in supbook record");
}
return externSheet.getIndex(0, sheetpos);
}
// Check for square brackets
int closeSquareBracketsIndex = sheetName.lastIndexOf(']');
int openSquareBracketsIndex = sheetName.lastIndexOf('[');
if (closeSquareBracketsIndex == -1 ||
openSquareBracketsIndex == -1)
{
return -1;
}
String worksheetName = sheetName.substring(closeSquareBracketsIndex+1);
String workbookName = sheetName.substring(openSquareBracketsIndex+1,
closeSquareBracketsIndex);
String path = sheetName.substring(0, openSquareBracketsIndex);
String fileName = path + workbookName;
boolean supbookFound = false;
SupbookRecord externalSupbook = null;
int supbookIndex = -1;
for (int ind = 0; ind < supbooks.size() && !supbookFound ; ind++)
{
externalSupbook = (SupbookRecord) supbooks.get(ind);
if (externalSupbook.getType() == SupbookRecord.EXTERNAL &&
externalSupbook.getFileName().equals(fileName))
{
supbookFound = true;
supbookIndex = ind;
}
}
if (!supbookFound)
{
externalSupbook = new SupbookRecord(fileName, settings);
supbookIndex = supbooks.size();
supbooks.add(externalSupbook);
}
int sheetIndex = externalSupbook.getSheetIndex(worksheetName);
return externSheet.getIndex(supbookIndex, sheetIndex);
}
/**
* Gets the last external sheet index for the sheet name
* @param sheetName
* @return the sheet index or -1 if the sheet could not be found
*/
public int getLastExternalSheetIndex(String sheetName)
{
if (externSheet == null)
{
externSheet = new ExternalSheetRecord();
supbooks = new ArrayList();
supbooks.add(new SupbookRecord(getNumberOfSheets(), settings));
}
// Iterate through the sheets records
boolean found = false;
Iterator i = sheets.iterator();
int sheetpos = 0;
WritableSheetImpl s = null;
while (i.hasNext() && !found)
{
s = (WritableSheetImpl) i.next();
if (s.getName().equals(sheetName))
{
found = true;
}
else
{
sheetpos++;
}
}
if (!found)
{
return -1;
}
// Check that the supbook record at position zero is internal and contains
// all the sheets
SupbookRecord supbook = (SupbookRecord) supbooks.get(0);
Assert.verify(supbook.getType() == SupbookRecord.INTERNAL &&
supbook.getNumberOfSheets() == getNumberOfSheets());
return externSheet.getIndex(0, sheetpos);
}
/**
* Sets the RGB value for the specified colour for this workbook
*
* @param c the colour whose RGB value is to be overwritten
* @param r the red portion to set (0-255)
* @param g the green portion to set (0-255)
* @param b the blue portion to set (0-255)
*/
public void setColourRGB(Colour c, int r, int g, int b)
{
formatRecords.setColourRGB(c,r,g,b);
}
/**
* Accessor for the RGB value for the specified colour
*
* @return the RGB for the specified colour
*/
public RGB getColourRGB(Colour c)
{
return formatRecords.getColourRGB(c);
}
/**
* Gets the name at the specified index
*
* @param index the index into the name table
* @return the name of the cell
*/
public String getName(int index)
{
Assert.verify(index >= 0 && index < names.size());
NameRecord n = (NameRecord) names.get(index);
return n.getName();
}
/**
* Gets the index of the name record for the name
*
* @param name
* @return the index in the name table
*/
public int getNameIndex(String name)
{
NameRecord nr = (NameRecord) nameRecords.get(name);
return nr != null ? nr.getIndex() : -1;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -