use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method createWorkbook.
/**
* read support for low level
* API. Pass in an array of Record objects, A Workbook
* object is constructed and passed back with all of its initialization set
* to the passed in records and references to those records held. Unlike Sheet
* workbook does not use an offset (its assumed to be 0) since its first in a file.
* If you need an offset then construct a new array with a 0 offset or write your
* own ;-p.
*
* @param recs an array of Record objects
* @return Workbook object
*/
public static InternalWorkbook createWorkbook(List<Record> recs) {
LOG.log(DEBUG, "Workbook (readfile) created with reclen=", recs.size());
InternalWorkbook retval = new InternalWorkbook();
List<Record> records = new ArrayList<Record>(recs.size() / 3);
retval.records.setRecords(records);
boolean eofPassed = false;
for (int k = 0; k < recs.size(); k++) {
Record rec = recs.get(k);
String logObj;
switch(rec.getSid()) {
case EOFRecord.sid:
logObj = "workbook eof";
break;
case BoundSheetRecord.sid:
logObj = "boundsheet";
retval.boundsheets.add((BoundSheetRecord) rec);
retval.records.setBspos(k);
break;
case SSTRecord.sid:
logObj = "sst";
retval.sst = (SSTRecord) rec;
break;
case FontRecord.sid:
logObj = "font";
retval.records.setFontpos(k);
retval.numfonts++;
break;
case ExtendedFormatRecord.sid:
logObj = "XF";
retval.records.setXfpos(k);
retval.numxfs++;
break;
case TabIdRecord.sid:
logObj = "tabid";
retval.records.setTabpos(k);
break;
case ProtectRecord.sid:
logObj = "protect";
retval.records.setProtpos(k);
break;
case BackupRecord.sid:
logObj = "backup";
retval.records.setBackuppos(k);
break;
case ExternSheetRecord.sid:
throw new RecordFormatException("Extern sheet is part of LinkTable");
case NameRecord.sid:
case SupBookRecord.sid:
// LinkTable can start with either of these
LOG.log(DEBUG, "found SupBook record at " + k);
retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords);
k += retval.linkTable.getRecordCount() - 1;
continue;
case FormatRecord.sid:
logObj = "format";
FormatRecord fr = (FormatRecord) rec;
retval.formats.add(fr);
retval.maxformatid = retval.maxformatid >= fr.getIndexCode() ? retval.maxformatid : fr.getIndexCode();
break;
case DateWindow1904Record.sid:
logObj = "datewindow1904";
retval.uses1904datewindowing = ((DateWindow1904Record) rec).getWindowing() == 1;
break;
case PaletteRecord.sid:
logObj = "palette";
retval.records.setPalettepos(k);
break;
case WindowOneRecord.sid:
logObj = "WindowOneRecord";
retval.windowOne = (WindowOneRecord) rec;
break;
case WriteAccessRecord.sid:
logObj = "WriteAccess";
retval.writeAccess = (WriteAccessRecord) rec;
break;
case WriteProtectRecord.sid:
logObj = "WriteProtect";
retval.writeProtect = (WriteProtectRecord) rec;
break;
case FileSharingRecord.sid:
logObj = "FileSharing";
retval.fileShare = (FileSharingRecord) rec;
break;
case NameCommentRecord.sid:
logObj = "NameComment";
final NameCommentRecord ncr = (NameCommentRecord) rec;
retval.commentRecords.put(ncr.getNameText(), ncr);
break;
case HyperlinkRecord.sid:
// Look for other interesting values that follow the EOFRecord
logObj = "Hyperlink";
retval.hyperlinks.add((HyperlinkRecord) rec);
break;
default:
logObj = "(sid=" + rec.getSid() + ")";
break;
}
if (!eofPassed) {
records.add(rec);
}
LOG.log(DEBUG, "found " + logObj + " record at " + k);
if (rec.getSid() == EOFRecord.sid) {
eofPassed = true;
}
}
if (retval.windowOne == null) {
retval.windowOne = createWindowOne();
}
LOG.log(DEBUG, "exit create workbook from existing file function");
return retval;
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method createWorkbook.
/**
* Creates an empty workbook object with three blank sheets and all the empty
* fields. Use this to create a workbook from scratch.
*
* @return an empty workbook object
*/
public static InternalWorkbook createWorkbook() {
LOG.log(DEBUG, "creating new workbook from scratch");
InternalWorkbook retval = new InternalWorkbook();
List<Record> records = new ArrayList<Record>(30);
retval.records.setRecords(records);
List<FormatRecord> formats = retval.formats;
records.add(createBOF());
records.add(new InterfaceHdrRecord(CODEPAGE));
records.add(createMMS());
records.add(InterfaceEndRecord.instance);
records.add(createWriteAccess());
records.add(createCodepage());
records.add(createDSF());
records.add(createTabId());
retval.records.setTabpos(records.size() - 1);
records.add(createFnGroupCount());
records.add(createWindowProtect());
records.add(createProtect());
retval.records.setProtpos(records.size() - 1);
records.add(createPassword());
records.add(createProtectionRev4());
records.add(createPasswordRev4());
retval.windowOne = createWindowOne();
records.add(retval.windowOne);
records.add(createBackup());
retval.records.setBackuppos(records.size() - 1);
records.add(createHideObj());
records.add(createDateWindow1904());
records.add(createPrecision());
records.add(createRefreshAll());
records.add(createBookBool());
records.add(createFont());
records.add(createFont());
records.add(createFont());
records.add(createFont());
// last font record position
retval.records.setFontpos(records.size() - 1);
retval.numfonts = 4;
// set up format records
for (int i = 0; i <= 7; i++) {
FormatRecord rec = createFormat(i);
retval.maxformatid = retval.maxformatid >= rec.getIndexCode() ? retval.maxformatid : rec.getIndexCode();
formats.add(rec);
records.add(rec);
}
for (int k = 0; k < 21; k++) {
records.add(InternalWorkbook.createExtendedFormat(k));
retval.numxfs++;
}
retval.records.setXfpos(records.size() - 1);
for (int k = 0; k < 6; k++) {
records.add(InternalWorkbook.createStyle(k));
}
records.add(InternalWorkbook.createUseSelFS());
// now just do 1
int nBoundSheets = 1;
for (int k = 0; k < nBoundSheets; k++) {
BoundSheetRecord bsr = createBoundSheet(k);
records.add(bsr);
retval.boundsheets.add(bsr);
retval.records.setBspos(records.size() - 1);
}
records.add(InternalWorkbook.createCountry());
for (int k = 0; k < nBoundSheets; k++) {
retval.getOrCreateLinkTable().checkExternSheet(k);
}
retval.sst = new SSTRecord();
records.add(retval.sst);
records.add(InternalWorkbook.createExtendedSST());
records.add(EOFRecord.instance);
LOG.log(DEBUG, "exit create new workbook from scratch");
return retval;
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method serialize.
/**
* Serializes all records int the worksheet section into a big byte array. Use
* this to write the Workbook out.
* @param offset of the data to be written
* @param data array of bytes to write this to
* @return the length of serialized bytes
*/
public int serialize(int offset, byte[] data) {
LOG.log(DEBUG, "Serializing Workbook with offsets");
int pos = 0;
SSTRecord lSST = null;
int sstPos = 0;
boolean wroteBoundSheets = false;
for (Record record : records) {
int len = 0;
if (record instanceof SSTRecord) {
lSST = (SSTRecord) record;
sstPos = pos;
}
if (record.getSid() == ExtSSTRecord.sid && lSST != null) {
record = lSST.createExtSSTRecord(sstPos + offset);
}
if (record instanceof BoundSheetRecord) {
if (!wroteBoundSheets) {
for (BoundSheetRecord bsr : boundsheets) {
len += bsr.serialize(pos + offset + len, data);
}
wroteBoundSheets = true;
}
} else {
len = record.serialize(pos + offset, data);
}
pos += len;
}
LOG.log(DEBUG, "Exiting serialize workbook");
return pos;
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method doesContainsSheetName.
/**
* Determines whether a workbook contains the provided sheet name. For the purpose of
* comparison, long names are truncated to 31 chars.
*
* @param name the name to test (case insensitive match)
* @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check.
* @return true if the sheet contains the name, false otherwise.
*/
public boolean doesContainsSheetName(String name, int excludeSheetIdx) {
String aName = name;
if (aName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
aName = aName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
}
int i = 0;
for (BoundSheetRecord boundSheetRecord : boundsheets) {
if (excludeSheetIdx == i++) {
continue;
}
String bName = boundSheetRecord.getSheetname();
if (bName.length() > MAX_SENSITIVE_SHEET_NAME_LEN) {
bName = bName.substring(0, MAX_SENSITIVE_SHEET_NAME_LEN);
}
if (aName.equalsIgnoreCase(bName)) {
return true;
}
}
return false;
}
Aggregations