Search in sources :

Example 1 with BoundSheetRecord

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);
}
Also used : ExternSheetRecord(org.apache.poi.hssf.record.ExternSheetRecord) ArrayList(java.util.ArrayList) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) Record(org.apache.poi.hssf.record.Record) SupBookRecord(org.apache.poi.hssf.record.SupBookRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) ExternSheetRecord(org.apache.poi.hssf.record.ExternSheetRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 2 with BoundSheetRecord

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();
    }
}
Also used : BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 3 with BoundSheetRecord

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);
}
Also used : EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) FeatHdrRecord(org.apache.poi.hssf.record.FeatHdrRecord) SelectionRecord(org.apache.poi.hssf.record.SelectionRecord) ContinueRecord(org.apache.poi.hssf.record.ContinueRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) DVALRecord(org.apache.poi.hssf.record.DVALRecord) DVRecord(org.apache.poi.hssf.record.DVRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 4 with BoundSheetRecord

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);
}
Also used : BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Example 5 with BoundSheetRecord

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 &gt; 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);
}
Also used : UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord)

Aggregations

BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)9 EOFRecord (org.apache.poi.hssf.record.EOFRecord)5 ExternSheetRecord (org.apache.poi.hssf.record.ExternSheetRecord)4 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)3 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)3 EscherDgRecord (org.apache.poi.ddf.EscherDgRecord)3 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)3 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)3 EscherRecord (org.apache.poi.ddf.EscherRecord)3 EscherSpRecord (org.apache.poi.ddf.EscherSpRecord)3 EscherSplitMenuColorsRecord (org.apache.poi.ddf.EscherSplitMenuColorsRecord)3 BOFRecord (org.apache.poi.hssf.record.BOFRecord)3 BackupRecord (org.apache.poi.hssf.record.BackupRecord)3 BookBoolRecord (org.apache.poi.hssf.record.BookBoolRecord)3 CodepageRecord (org.apache.poi.hssf.record.CodepageRecord)3 CountryRecord (org.apache.poi.hssf.record.CountryRecord)3 DSFRecord (org.apache.poi.hssf.record.DSFRecord)3 DateWindow1904Record (org.apache.poi.hssf.record.DateWindow1904Record)3 DrawingGroupRecord (org.apache.poi.hssf.record.DrawingGroupRecord)3 ExtSSTRecord (org.apache.poi.hssf.record.ExtSSTRecord)3