use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class EventWorkbookBuilder method createStubWorkbook.
/**
* Creates a stub Workbook from the supplied records,
* suitable for use with the {@link HSSFFormulaParser}
* @param externs The ExternSheetRecords in your file
* @param bounds The BoundSheetRecords in your file
* @param sst The SSTRecord in your file.
* @return A stub Workbook suitable for use with {@link HSSFFormulaParser}
*/
public static InternalWorkbook createStubWorkbook(ExternSheetRecord[] externs, BoundSheetRecord[] bounds, SSTRecord sst) {
List<Record> wbRecords = new ArrayList<Record>();
// Core Workbook records go first
if (bounds != null) {
for (BoundSheetRecord bound : bounds) {
wbRecords.add(bound);
}
}
if (sst != null) {
wbRecords.add(sst);
}
// preceded by a SupBookRecord
if (externs != null) {
wbRecords.add(SupBookRecord.createInternalReferences((short) externs.length));
for (ExternSheetRecord extern : externs) {
wbRecords.add(extern);
}
}
// Finally we need an EoF record
wbRecords.add(EOFRecord.instance);
return InternalWorkbook.createWorkbook(wbRecords);
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method checkSheets.
/**
* if we're trying to address one more sheet than we have, go ahead and add it! if we're
* trying to address >1 more than we have throw an exception!
*/
private void checkSheets(int sheetnum) {
if ((boundsheets.size()) <= sheetnum) {
// if we're short one add another..
if ((boundsheets.size() + 1) <= sheetnum) {
throw new RuntimeException("Sheet number out of bounds!");
}
BoundSheetRecord bsr = createBoundSheet(sheetnum);
records.add(records.getBspos() + 1, bsr);
records.setBspos(records.getBspos() + 1);
boundsheets.add(bsr);
getOrCreateLinkTable().checkExternSheet(sheetnum);
fixTabIdRecord();
}
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class TestHSSFEventFactory method testWithPasswordProtectedWorkbooks.
public void testWithPasswordProtectedWorkbooks() throws Exception {
HSSFRequest req = new HSSFRequest();
MockHSSFListener mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
// Without a password, can't be read
Biff8EncryptionKey.setCurrentUserPassword(null);
POIFSFileSystem fs = new POIFSFileSystem(openSample("xor-encryption-abc.xls"));
HSSFEventFactory factory = new HSSFEventFactory();
try {
factory.processWorkbookEvents(req, fs);
fail("Shouldn't be able to process protected workbook without the password");
} catch (EncryptedDocumentException e) {
}
// With the password, is properly processed
Biff8EncryptionKey.setCurrentUserPassword("abc");
req = new HSSFRequest();
mockListen = new MockHSSFListener();
req.addListenerForAllRecords(mockListen);
factory.processWorkbookEvents(req, fs);
// Check we got the sheet and the contents
Record[] recs = mockListen.getRecords();
assertTrue(recs.length > 50);
// Has one sheet, with values 1,2,3 in column A rows 1-3
boolean hasSheet = false, hasA1 = false, hasA2 = false, hasA3 = false;
for (Record r : recs) {
if (r instanceof BoundSheetRecord) {
BoundSheetRecord bsr = (BoundSheetRecord) r;
assertEquals("Sheet1", bsr.getSheetname());
hasSheet = true;
}
if (r instanceof NumberRecord) {
NumberRecord nr = (NumberRecord) r;
if (nr.getColumn() == 0 && nr.getRow() == 0) {
assertEquals(1, (int) nr.getValue());
hasA1 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 1) {
assertEquals(2, (int) nr.getValue());
hasA2 = true;
}
if (nr.getColumn() == 0 && nr.getRow() == 2) {
assertEquals(3, (int) nr.getValue());
hasA3 = true;
}
}
}
assertTrue("Sheet record not found", hasSheet);
assertTrue("Numeric record for A1 not found", hasA1);
assertTrue("Numeric record for A2 not found", hasA2);
assertTrue("Numeric record for A3 not found", hasA3);
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method setSheetHidden.
/**
* Hide or unhide a sheet.
*
* @param sheetnum The sheet number
* @param visibility the sheet visibility to set (visible, hidden, very hidden)
* @since 3.16 beta 2
*/
public void setSheetHidden(int sheetnum, SheetVisibility visibility) {
BoundSheetRecord bsr = getBoundSheetRec(sheetnum);
bsr.setHidden(visibility == SheetVisibility.HIDDEN);
bsr.setVeryHidden(visibility == SheetVisibility.VERY_HIDDEN);
}
use of org.apache.poi.hssf.record.BoundSheetRecord in project poi by apache.
the class InternalWorkbook method setSheetName.
/**
* sets the name for a given sheet. If the boundsheet record doesn't exist and
* its only one more than we have, go ahead and create it. If it's > 1 more than
* we have, except
*
* @param sheetnum the sheet number (0 based)
* @param sheetname the name for the sheet
*/
public void setSheetName(int sheetnum, final String sheetname) {
checkSheets(sheetnum);
// YK: Mimic Excel and silently truncate sheet names longer than 31 characters
String sn = (sheetname.length() > 31) ? sheetname.substring(0, 31) : sheetname;
BoundSheetRecord sheet = boundsheets.get(sheetnum);
sheet.setSheetname(sn);
}
Aggregations