Search in sources :

Example 1 with CFHeaderBase

use of org.apache.poi.hssf.record.CFHeaderBase in project poi by apache.

the class CFRecordsAggregate method createCFAggregate.

/**
     * Create CFRecordsAggregate from a list of CF Records
     * @param rs - the stream to read from
     * @return CFRecordsAggregate object
     */
public static CFRecordsAggregate createCFAggregate(RecordStream rs) {
    Record rec = rs.getNext();
    if (rec.getSid() != CFHeaderRecord.sid && rec.getSid() != CFHeader12Record.sid) {
        throw new IllegalStateException("next record sid was " + rec.getSid() + " instead of " + CFHeaderRecord.sid + " or " + CFHeader12Record.sid + " as expected");
    }
    CFHeaderBase header = (CFHeaderBase) rec;
    int nRules = header.getNumberOfConditionalFormats();
    CFRuleBase[] rules = new CFRuleBase[nRules];
    for (int i = 0; i < rules.length; i++) {
        rules[i] = (CFRuleBase) rs.getNext();
    }
    return new CFRecordsAggregate(header, rules);
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) Record(org.apache.poi.hssf.record.Record) CFRuleRecord(org.apache.poi.hssf.record.CFRuleRecord) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) CFHeaderBase(org.apache.poi.hssf.record.CFHeaderBase)

Example 2 with CFHeaderBase

use of org.apache.poi.hssf.record.CFHeaderBase in project poi by apache.

the class TestCFRecordsAggregate method testCFRecordsAggregate.

public void testCFRecordsAggregate() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
    List<Record> recs = new ArrayList<Record>();
    CFHeaderBase header = new CFHeaderRecord();
    CFRuleBase rule1 = CFRuleRecord.create(sheet, "7");
    CFRuleBase rule2 = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "2", "5");
    CFRuleBase rule3 = CFRuleRecord.create(sheet, ComparisonOperator.GE, "100", null);
    header.setNumberOfConditionalFormats(3);
    CellRangeAddress[] cellRanges = { new CellRangeAddress(0, 1, 0, 0), new CellRangeAddress(0, 1, 2, 2) };
    header.setCellRanges(cellRanges);
    recs.add(header);
    recs.add(rule1);
    recs.add(rule2);
    recs.add(rule3);
    CFRecordsAggregate record = CFRecordsAggregate.createCFAggregate(new RecordStream(recs, 0));
    // Serialize
    byte[] serializedRecord = new byte[record.getRecordSize()];
    record.serialize(0, serializedRecord);
    InputStream in = new ByteArrayInputStream(serializedRecord);
    //Parse
    recs = RecordFactory.createRecords(in);
    // Verify
    assertNotNull(recs);
    assertEquals(4, recs.size());
    header = (CFHeaderRecord) recs.get(0);
    rule1 = (CFRuleRecord) recs.get(1);
    assertNotNull(rule1);
    rule2 = (CFRuleRecord) recs.get(2);
    assertNotNull(rule2);
    rule3 = (CFRuleRecord) recs.get(3);
    assertNotNull(rule3);
    cellRanges = header.getCellRanges();
    assertEquals(2, cellRanges.length);
    assertEquals(3, header.getNumberOfConditionalFormats());
    assertFalse(header.getNeedRecalculation());
    record = CFRecordsAggregate.createCFAggregate(new RecordStream(recs, 0));
    record = record.cloneCFAggregate();
    assertNotNull(record.getHeader());
    assertEquals(3, record.getNumberOfRules());
    header = record.getHeader();
    rule1 = record.getRule(0);
    assertNotNull(rule1);
    rule2 = record.getRule(1);
    assertNotNull(rule2);
    rule3 = record.getRule(2);
    assertNotNull(rule3);
    cellRanges = header.getCellRanges();
    assertEquals(2, cellRanges.length);
    assertEquals(3, header.getNumberOfConditionalFormats());
    assertFalse(header.getNeedRecalculation());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) CFHeaderBase(org.apache.poi.hssf.record.CFHeaderBase) CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) RecordStream(org.apache.poi.hssf.model.RecordStream) ByteArrayInputStream(java.io.ByteArrayInputStream) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Record(org.apache.poi.hssf.record.Record) CFRuleRecord(org.apache.poi.hssf.record.CFRuleRecord) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 3 with CFHeaderBase

use of org.apache.poi.hssf.record.CFHeaderBase in project poi by apache.

the class CFRecordsAggregate method createHeader.

private static CFHeaderBase createHeader(CellRangeAddress[] regions, CFRuleBase[] rules) {
    final CFHeaderBase header;
    if (rules.length == 0 || rules[0] instanceof CFRuleRecord) {
        header = new CFHeaderRecord(regions, rules.length);
    } else {
        header = new CFHeader12Record(regions, rules.length);
    }
    // set the "needs recalculate" by default to avoid Excel handling conditional formatting incorrectly
    // see bug 52122 for details
    header.setNeedRecalculation(true);
    return header;
}
Also used : CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) CFRuleRecord(org.apache.poi.hssf.record.CFRuleRecord) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) CFHeaderBase(org.apache.poi.hssf.record.CFHeaderBase)

Aggregations

CFHeaderBase (org.apache.poi.hssf.record.CFHeaderBase)3 CFHeaderRecord (org.apache.poi.hssf.record.CFHeaderRecord)3 CFRuleRecord (org.apache.poi.hssf.record.CFRuleRecord)3 CFHeader12Record (org.apache.poi.hssf.record.CFHeader12Record)2 CFRule12Record (org.apache.poi.hssf.record.CFRule12Record)2 CFRuleBase (org.apache.poi.hssf.record.CFRuleBase)2 Record (org.apache.poi.hssf.record.Record)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 RecordStream (org.apache.poi.hssf.model.RecordStream)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1