workbookparser.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 1,075 行 · 第 1/2 页
JAVA
1,075 行
nameTable.add(nr);
}
else if (r.getType() == Type.FONT)
{
FontRecord fr = null;
if (bof.isBiff8())
{
fr = new FontRecord(r, settings);
}
else
{
fr = new FontRecord(r, settings, FontRecord.biff7);
}
fonts.addFont(fr);
}
else if (r.getType() == Type.PALETTE)
{
PaletteRecord palette = new PaletteRecord(r);
formattingRecords.setPalette(palette);
}
else if (r.getType() == Type.NINETEENFOUR)
{
NineteenFourRecord nr = new NineteenFourRecord(r);
nineteenFour = nr.is1904();
}
else if (r.getType() == Type.FORMAT)
{
FormatRecord fr = null;
if (bof.isBiff8())
{
fr = new FormatRecord(r, settings, FormatRecord.biff8);
}
else
{
fr = new FormatRecord(r, settings, FormatRecord.biff7);
}
try
{
formattingRecords.addFormat(fr);
}
catch (NumFormatRecordsException e)
{
e.printStackTrace();
// This should not happen. Bomb out
Assert.verify(false, e.getMessage());
}
}
else if (r.getType() == Type.XF)
{
XFRecord xfr = null;
if (bof.isBiff8())
{
xfr = new XFRecord(r, settings, XFRecord.biff8);
}
else
{
xfr = new XFRecord(r, settings, XFRecord.biff7);
}
try
{
formattingRecords.addStyle(xfr);
}
catch (NumFormatRecordsException e)
{
// This should not happen. Bomb out
Assert.verify(false, e.getMessage());
}
}
else if (r.getType() == Type.BOUNDSHEET)
{
BoundsheetRecord br = null;
if (bof.isBiff8())
{
br = new BoundsheetRecord(r);
}
else
{
br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
}
if (br.isSheet())
{
boundsheets.add(br);
}
else if (br.isChart() && !settings.getDrawingsDisabled())
{
boundsheets.add(br);
}
}
else if (r.getType() == Type.EXTERNSHEET)
{
if (bof.isBiff8())
{
externSheet = new ExternalSheetRecord(r, settings);
}
else
{
externSheet = new ExternalSheetRecord(r, settings,
ExternalSheetRecord.biff7);
}
}
else if (r.getType() == Type.CODEPAGE)
{
CodepageRecord cr = new CodepageRecord(r);
settings.setCharacterSet(cr.getCharacterSet());
}
else if (r.getType() == Type.SUPBOOK)
{
Record nextrec = excelFile.peek();
while (nextrec.getType() == Type.CONTINUE)
{
r.addContinueRecord(excelFile.next());
nextrec = excelFile.peek();
}
SupbookRecord sr = new SupbookRecord(r, settings);
supbooks.add(sr);
}
else if (r.getType() == Type.PROTECT)
{
ProtectRecord pr = new ProtectRecord(r);
wbProtected = pr.isProtected();
}
else if (r.getType() == Type.OBJPROJ)
{
containsMacros = true;
}
else if (r.getType() == Type.COUNTRY)
{
countryRecord = new CountryRecord(r);
}
else if (r.getType() == Type.MSODRAWINGGROUP)
{
if (!settings.getDrawingsDisabled())
{
msoDrawingGroup = new MsoDrawingGroupRecord(r);
if (drawingGroup == null)
{
drawingGroup = new DrawingGroup(Origin.READ);
}
drawingGroup.add(msoDrawingGroup);
Record nextrec = excelFile.peek();
while (nextrec.getType() == Type.CONTINUE)
{
drawingGroup.add(excelFile.next());
nextrec = excelFile.peek();
}
}
}
else if (r.getType() == Type.BUTTONPROPERTYSET)
{
buttonPropertySet = new ButtonPropertySetRecord(r);
}
else if (r.getType() == Type.EOF)
{
bofs--;
}
}
bof = null;
if (excelFile.hasNext())
{
r = excelFile.next();
if (r.getType() == Type.BOF)
{
bof = new BOFRecord(r);
}
}
// Only get sheets for which there is a corresponding Boundsheet record
while (bof != null && getNumberOfSheets() < boundsheets.size())
{
if (!bof.isBiff8() && !bof.isBiff7())
{
throw new BiffException(BiffException.unrecognizedBiffVersion);
}
if (bof.isWorksheet())
{
// Read the sheet in
SheetImpl s = new SheetImpl(excelFile,
sharedStrings,
formattingRecords,
bof,
workbookBof,
nineteenFour,
this);
BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
(getNumberOfSheets());
s.setName(br.getName());
s.setHidden(br.isHidden());
addSheet(s);
}
else if (bof.isChart())
{
// Read the sheet in
SheetImpl s = new SheetImpl(excelFile,
sharedStrings,
formattingRecords,
bof,
workbookBof,
nineteenFour,
this);
BoundsheetRecord br = (BoundsheetRecord) boundsheets.get
(getNumberOfSheets());
s.setName(br.getName());
s.setHidden(br.isHidden());
addSheet(s);
}
else
{
logger.warn("BOF is unrecognized");
while (excelFile.hasNext() && r.getType() != Type.EOF)
{
r = excelFile.next();
}
}
// The next record will normally be a BOF or empty padding until
// the end of the block is reached. In exceptionally unlucky cases,
// the last EOF will coincide with a block division, so we have to
// check there is more data to retrieve.
// Thanks to liamg for spotting this
bof = null;
if (excelFile.hasNext())
{
r = excelFile.next();
if (r.getType() == Type.BOF)
{
bof = new BOFRecord(r);
}
}
}
}
/**
* Accessor for the formattingRecords, used by the WritableWorkbook
* when creating a copy of this
*
* @return the formatting records
*/
public FormattingRecords getFormattingRecords()
{
return formattingRecords;
}
/**
* Accessor for the externSheet, used by the WritableWorkbook
* when creating a copy of this
*
* @return the external sheet record
*/
public ExternalSheetRecord getExternalSheetRecord()
{
return externSheet;
}
/**
* Accessor for the MsoDrawingGroup, used by the WritableWorkbook
* when creating a copy of this
*
* @return the Mso Drawing Group record
*/
public MsoDrawingGroupRecord getMsoDrawingGroupRecord()
{
return msoDrawingGroup;
}
/**
* Accessor for the supbook records, used by the WritableWorkbook
* when creating a copy of this
*
* @return the supbook records
*/
public SupbookRecord[] getSupbookRecords()
{
SupbookRecord[] sr = new SupbookRecord[supbooks.size()];
return (SupbookRecord[]) supbooks.toArray(sr);
}
/**
* Accessor for the name records. Used by the WritableWorkbook when
* creating a copy of this
*
* @return the array of names
*/
public NameRecord[] getNameRecords()
{
NameRecord[] na = new NameRecord[nameTable.size()];
return (NameRecord[]) nameTable.toArray(na);
}
/**
* Accessor for the fonts, used by the WritableWorkbook
* when creating a copy of this
* @return the fonts used in this workbook
*/
public Fonts getFonts()
{
return fonts;
}
/**
* Gets the named cell from this workbook. If the name refers to a
* range of cells, then the cell on the top left is returned. If
* the name cannot be found, null is returned
*
* @param name the name of the cell/range to search for
* @return the cell in the top left of the range if found, NULL
* otherwise
*/
public Cell findCellByName(String name)
{
NameRecord nr = (NameRecord) namedRecords.get(name);
if (nr == null)
{
return null;
}
NameRecord.NameRange[] ranges = nr.getRanges();
// Go and retrieve the first cell in the first range
Sheet s = getSheet(ranges[0].getExternalSheet());
Cell cell = s.getCell(ranges[0].getFirstColumn(), ranges[0].getFirstRow());
return cell;
}
/**
* Gets the named range from this workbook. The Range object returns
* contains all the cells from the top left to the bottom right
* of the range.
* If the named range comprises an adjacent range,
* the Range[] will contain one object; for non-adjacent
* ranges, it is necessary to return an array of length greater than
* one.
* If the named range contains a single cell, the top left and
* bottom right cell will be the same cell
*
* @param name the name to find
* @return the range of cells
*/
public Range[] findByName(String name)
{
NameRecord nr = (NameRecord) namedRecords.get(name);
if (nr == null)
{
return null;
}
NameRecord.NameRange[] ranges = nr.getRanges();
Range[] cellRanges = new Range[ranges.length];
for (int i = 0; i < ranges.length; i++)
{
cellRanges[i] = new RangeImpl
(this,
getExternalSheetIndex(ranges[i].getExternalSheet()),
ranges[i].getFirstColumn(),
ranges[i].getFirstRow(),
getLastExternalSheetIndex(ranges[i].getExternalSheet()),
ranges[i].getLastColumn(),
ranges[i].getLastRow());
}
return cellRanges;
}
/**
* Gets the named ranges
*
* @return the list of named cells within the workbook
*/
public String[] getRangeNames()
{
Object[] keys = namedRecords.keySet().toArray();
String[] names = new String[keys.length];
System.arraycopy(keys, 0, names, 0, keys.length);
return names;
}
/**
* Method used when parsing formulas to make sure we are trying
* to parse a supported biff version
*
* @return the BOF record
*/
public BOFRecord getWorkbookBof()
{
return workbookBof;
}
/**
* Determines whether the sheet is protected
*
* @return whether or not the sheet is protected
*/
public boolean isProtected()
{
return wbProtected;
}
/**
* Accessor for the settings
*
* @return the workbook settings
*/
public WorkbookSettings getSettings()
{
return settings;
}
/**
* Accessor/implementation method for the external sheet reference
*
* @param sheetName the sheet name to look for
* @return the external sheet index
*/
public int getExternalSheetIndex(String sheetName)
{
return 0;
}
/**
* Accessor/implementation method for the external sheet reference
*
* @param sheetName the sheet name to look for
* @return the external sheet index
*/
public int getLastExternalSheetIndex(String sheetName)
{
return 0;
}
/**
* 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 < nameTable.size());
return ((NameRecord) nameTable.get(index)).getName();
}
/**
* Gets the index of the name record for the name
*
* @param name the name to search for
* @return the index in the name table
*/
public int getNameIndex(String name)
{
NameRecord nr = (NameRecord) namedRecords.get(name);
return nr != null ? nr.getIndex() : 0;
}
/**
* Accessor for the drawing group
*
* @return the drawing group
*/
public DrawingGroup getDrawingGroup()
{
return drawingGroup;
}
/**
* Accessor for the CompoundFile. For this feature to return non-null
* value, the propertySets feature in WorkbookSettings must be enabled
* and the workbook must contain additional property sets. This
* method is used during the workbook copy
*
* @return the base compound file if it contains additional data items
* and property sets are enabled
*/
public CompoundFile getCompoundFile()
{
return excelFile.getCompoundFile();
}
/**
* Accessor for the containsMacros
*
* @return TRUE if this workbook contains macros, FALSE otherwise
*/
public boolean containsMacros()
{
return containsMacros;
}
/**
* Accessor for the button property set, used during copying
*
* @return the button property set
*/
public ButtonPropertySetRecord getButtonPropertySet()
{
return buttonPropertySet;
}
/**
* Accessor for the country record, using during copying
*
* @return the country record read in
*/
public CountryRecord getCountryRecord()
{
return countryRecord;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?