📄 workbookparser.cs
字号:
sb.Append(']');
sb.Append(sr.getSheetName(firstTab));
return sb.ToString();
}
// An unknown supbook - return unkown
return "[UNKNOWN]";
}
/// <summary> Gets the name of the external sheet specified by the index
///
/// </summary>
/// <param name="index">the external sheet index
/// </param>
/// <returns> the name of the external sheet
/// </returns>
public virtual string getLastExternalSheetName(int index)
{
// For biff7, the whole external reference thing works differently
// Hopefully for our purposes sheet references will all be local
if (workbookBof.isBiff7())
{
BoundsheetRecord br = (BoundsheetRecord) boundsheets[index];
return br.Name;
}
int supbookIndex = externSheet.getSupbookIndex(index);
SupbookRecord sr = (SupbookRecord) supbooks[supbookIndex];
int lastTab = externSheet.getLastTabIndex(index);
if (sr.Type == SupbookRecord.INTERNAL)
{
// It's an internal reference - get the name from the boundsheets list
BoundsheetRecord br = (BoundsheetRecord) boundsheets[lastTab];
return br.Name;
}
else if (sr.Type == SupbookRecord.EXTERNAL)
{
// External reference - get the sheet name from the supbook record
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append('[');
sb.Append(sr.FileName);
sb.Append(']');
sb.Append(sr.getSheetName(lastTab));
return sb.ToString();
}
// An unknown supbook - return unkown
return "[UNKNOWN]";
}
/// <summary> Closes this workbook, and frees makes any memory allocated available
/// for garbage collection
/// </summary>
public override void close()
{
if (lastSheet != null)
{
lastSheet.clear();
}
excelFile.clear();
if (!settings.GCDisabled)
{
System.GC.Collect();
}
}
/// <summary> Adds the sheet to the end of the array
///
/// </summary>
/// <param name="s">the sheet to add
/// </param>
public void addSheet(Sheet s)
{
sheets.Add(s);
}
/// <summary> Does the hard work of building up the object graph from the excel bytes
///
/// </summary>
/// <exception cref=""> BiffException
/// </exception>
/// <exception cref=""> PasswordException if the workbook is password protected
/// </exception>
protected internal override void parse()
{
Record r = null;
BOFRecord bof = new BOFRecord(excelFile.next());
workbookBof = bof;
bofs++;
if (!bof.isBiff8() && !bof.isBiff7())
{
throw new BiffException(BiffException.unrecognizedBiffVersion);
}
if (!bof.isWorkbookGlobals())
{
throw new BiffException(BiffException.expectedGlobals);
}
ArrayList continueRecords = new ArrayList();
nameTable = new ArrayList();
// Skip to the first worksheet
while (bofs == 1)
{
r = excelFile.next();
if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.SST)
{
continueRecords.Clear();
Record nextrec = excelFile.peek();
while (nextrec.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.CONTINUE)
{
continueRecords.Add(excelFile.next());
nextrec = excelFile.peek();
}
// cast the array
System.Object[] rec = continueRecords.ToArray();
Record[] records = new Record[rec.Length];
Array.Copy(rec, 0, records, 0, rec.Length);
sharedStrings = new SSTRecord(r, records, settings);
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.FILEPASS)
{
throw new PasswordException();
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.NAME)
{
NameRecord nr = null;
if (bof.isBiff8())
{
nr = new NameRecord(r, settings, namedRecords.Count);
}
else
{
nr = new NameRecord(r, settings, namedRecords.Count, NameRecord.biff7);
}
namedRecords[nr.Name] = nr;
nameTable.Add(nr);
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.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.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PALETTE)
{
Microsoft.Fawvw.Components.NExcel.Biff.PaletteRecord palette = new Microsoft.Fawvw.Components.NExcel.Biff.PaletteRecord(r);
formattingRecords.Palette = palette;
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.NINETEENFOUR)
{
NineteenFourRecord nr = new NineteenFourRecord(r);
nineteenFour = nr.is1904();
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.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)
{
// This should not happen. Bomb out
// Assert.verify(false, e.getMessage());
Assert.verify(false, "This should not happen. 64");
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.XF)
{
XFRecord xfr = null;
if (bof.isBiff8())
{
xfr = new XFRecord(r, XFRecord.biff8);
}
else
{
xfr = new XFRecord(r, XFRecord.biff7);
}
try
{
formattingRecords.addStyle(xfr);
}
catch (NumFormatRecordsException e)
{
// This should not happen. Bomb out
// Assert.verify(false, e.getMessage());
Assert.verify(false, "This should not happen. 59");
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOUNDSHEET)
{
BoundsheetRecord br = null;
if (bof.isBiff8())
{
br = new BoundsheetRecord(r);
}
else
{
br = new BoundsheetRecord(r, BoundsheetRecord.biff7);
}
if (br.isSheet() || br.Chart)
{
boundsheets.Add(br);
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.EXTERNSHEET)
{
if (bof.isBiff8())
{
externSheet = new ExternalSheetRecord(r, settings);
}
else
{
externSheet = new ExternalSheetRecord(r, settings, ExternalSheetRecord.biff7);
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.CODEPAGE)
{
CodepageRecord cr = new CodepageRecord(r);
settings.CharacterSet = cr.CharacterSet;
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.SUPBOOK)
{
SupbookRecord sr = new SupbookRecord(r, settings);
supbooks.Add(sr);
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.PROTECT)
{
ProtectRecord pr = new ProtectRecord(r);
wbProtected = pr.IsProtected();
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.MSODRAWINGGROUP)
{
msoDrawingGroup = new MsoDrawingGroupRecord(r);
if (drawingGroup == null)
{
drawingGroup = new DrawingGroup(DrawingGroup.READ);
}
drawingGroup.add(msoDrawingGroup);
Record nextrec = excelFile.peek();
while (nextrec.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.CONTINUE)
{
drawingGroup.add(excelFile.next());
nextrec = excelFile.peek();
}
}
else if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.EOF)
{
bofs--;
}
}
bof = null;
if (excelFile.hasNext())
{
r = excelFile.next();
if (r.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOF)
{
bof = new BOFRecord(r);
}
}
// Only get sheets for which there is a corresponding Boundsheet record
while (bof != null && NumberOfSheets < boundsheets.Count)
{
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[NumberOfSheets];
s.setName(br.Name);
s.Hidden = 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[NumberOfSheets];
s.setName(br.Name);
s.Hidden = br.isHidden();
addSheet(s);
}
else
{
logger.warn("BOF is unrecognized");
while (excelFile.hasNext() && r.Type != Microsoft.Fawvw.Components.NExcel.Biff.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.Type == Microsoft.Fawvw.Components.NExcel.Biff.Type.BOF)
{
bof = new BOFRecord(r);
}
}
}
}
/// <summary> 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
///
/// </summary>
/// <param name="name">the name of the cell/range to search for
/// </param>
/// <returns> the cell in the top left of the range if found, NULL
/// otherwise
/// </returns>
public override Cell findCellByName(string name)
{
NameRecord nr = (NameRecord) namedRecords[name];
if (nr == null)
{
return null;
}
NameRecord.NameRange[] ranges = nr.Ranges;
// Go and retrieve the first cell in the first range
Sheet s = getSheet(ranges[0].ExternalSheet);
Cell cell = s.getCell(ranges[0].FirstColumn, ranges[0].FirstRow);
return cell;
}
/// <summary> 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
///
/// </summary>
/// <param name="name">the name to find
/// </param>
/// <returns> the range of cells
/// </returns>
public override Range[] findByName(string name)
{
NameRecord nr = (NameRecord) namedRecords[name];
if (nr == null)
{
return null;
}
NameRecord.NameRange[] ranges = nr.Ranges;
Range[] cellRanges = new Range[ranges.Length];
for (int i = 0; i < ranges.Length; i++)
{
cellRanges[i] = new RangeImpl(this, getExternalSheetIndex(ranges[i].ExternalSheet), ranges[i].FirstColumn, ranges[i].FirstRow, getLastExternalSheetIndex(ranges[i].ExternalSheet), ranges[i].LastColumn, ranges[i].LastRow);
}
return cellRanges;
}
/// <summary> Accessor/implementation method for the external sheet reference
///
/// </summary>
/// <param name="sheetName">the sheet name to look for
/// </param>
/// <returns> the external sheet index
/// </returns>
public virtual int getExternalSheetIndex(string sheetName)
{
return 0;
}
/// <summary> Accessor/implementation method for the external sheet reference
///
/// </summary>
/// <param name="sheetName">the sheet name to look for
/// </param>
/// <returns> the external sheet index
/// </returns>
public virtual int getLastExternalSheetIndex(string sheetName)
{
return 0;
}
/// <summary> Gets the name at the specified index
///
/// </summary>
/// <param name="index">the index into the name table
/// </param>
/// <returns> the name of the cell
/// </returns>
public virtual string getName(int index)
{
Assert.verify(index >= 0 && index < nameTable.Count);
return ((NameRecord) nameTable[index]).Name;
}
/// <summary> Gets the index of the name record for the name
///
/// </summary>
/// <param name="name">the name to search for
/// </param>
/// <returns> the index in the name table
/// </returns>
public virtual int getNameIndex(string name)
{
NameRecord nr = (NameRecord) namedRecords[name];
return nr != null?nr.Index:0;
}
static WorkbookParser()
{
logger = Logger.getLogger(typeof(WorkbookParser));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -