Search in sources :

Example 6 with ExtendedFormatRecord

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

the class HSSFWorkbook method createCellStyle.

/**
     * Create a new Cell style and add it to the workbook's style table.
     * You can define up to 4000 unique styles in a .xls workbook.
     *
     * @return the new Cell Style object
     * @throws IllegalStateException if the number of cell styles exceeded the limit for this type of Workbook.
     */
@Override
public HSSFCellStyle createCellStyle() {
    if (workbook.getNumExFormats() == MAX_STYLES) {
        throw new IllegalStateException("The maximum number of cell styles was exceeded. " + "You can define up to 4000 styles in a .xls workbook");
    }
    ExtendedFormatRecord xfr = workbook.createCellXF();
    short index = (short) (getNumCellStyles() - 1);
    return new HSSFCellStyle(index, xfr, this);
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord)

Example 7 with ExtendedFormatRecord

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

the class HSSFOptimiser method optimiseFonts.

/**
	 * Goes through the Workbook, optimising the fonts by
	 *  removing duplicate ones.
	 * For now, only works on fonts used in {@link HSSFCellStyle}
	 *  and {@link HSSFRichTextString}. Any other font uses
	 *  (eg charts, pictures) may well end up broken!
	 * This can be a slow operation, especially if you have
	 *  lots of cells, cell styles or rich text strings
	 * @param workbook The workbook in which to optimise the fonts
	 */
public static void optimiseFonts(HSSFWorkbook workbook) {
    // Where each font has ended up, and if we need to
    //  delete the record for it. Start off with no change
    short[] newPos = new short[workbook.getWorkbook().getNumberOfFontRecords() + 1];
    boolean[] zapRecords = new boolean[newPos.length];
    for (int i = 0; i < newPos.length; i++) {
        newPos[i] = (short) i;
        zapRecords[i] = false;
    }
    // Get each font record, so we can do deletes
    //  without getting confused
    FontRecord[] frecs = new FontRecord[newPos.length];
    for (int i = 0; i < newPos.length; i++) {
        // There is no 4!
        if (i == 4)
            continue;
        frecs[i] = workbook.getWorkbook().getFontRecordAt(i);
    }
    // Note - don't change built in fonts (those before 5)
    for (int i = 5; i < newPos.length; i++) {
        // Check this one for being a duplicate
        //  of an earlier one
        int earlierDuplicate = -1;
        for (int j = 0; j < i && earlierDuplicate == -1; j++) {
            if (j == 4)
                continue;
            FontRecord frCheck = workbook.getWorkbook().getFontRecordAt(j);
            if (frCheck.sameProperties(frecs[i])) {
                earlierDuplicate = j;
            }
        }
        // If we got a duplicate, mark it as such
        if (earlierDuplicate != -1) {
            newPos[i] = (short) earlierDuplicate;
            zapRecords[i] = true;
        }
    }
    // Only need to worry about user fonts
    for (int i = 5; i < newPos.length; i++) {
        // Find the number deleted to that
        //  point, and adjust
        short preDeletePos = newPos[i];
        short newPosition = preDeletePos;
        for (int j = 0; j < preDeletePos; j++) {
            if (zapRecords[j])
                newPosition--;
        }
        // Update the new position
        newPos[i] = newPosition;
    }
    // Zap the un-needed user font records
    for (int i = 5; i < newPos.length; i++) {
        if (zapRecords[i]) {
            workbook.getWorkbook().removeFontRecord(frecs[i]);
        }
    }
    // Tell HSSFWorkbook that it needs to
    //  re-start its HSSFFontCache
    workbook.resetFontCache();
    //  new locations of the fonts
    for (int i = 0; i < workbook.getWorkbook().getNumExFormats(); i++) {
        ExtendedFormatRecord xfr = workbook.getWorkbook().getExFormatAt(i);
        xfr.setFontIndex(newPos[xfr.getFontIndex()]);
    }
    // Update the rich text strings to point at
    //  the new locations of the fonts
    // Remember that one underlying unicode string
    //  may be shared by multiple RichTextStrings!
    HashSet<UnicodeString> doneUnicodeStrings = new HashSet<UnicodeString>();
    for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
        HSSFSheet s = workbook.getSheetAt(sheetNum);
        for (Row row : s) {
            for (Cell cell : row) {
                if (cell.getCellTypeEnum() == CellType.STRING) {
                    HSSFRichTextString rtr = (HSSFRichTextString) cell.getRichStringCellValue();
                    UnicodeString u = rtr.getRawUnicodeString();
                    // Have we done this string already?
                    if (!doneUnicodeStrings.contains(u)) {
                        // Update for each new position
                        for (short i = 5; i < newPos.length; i++) {
                            if (i != newPos[i]) {
                                u.swapFontUse(i, newPos[i]);
                            }
                        }
                        // Mark as done
                        doneUnicodeStrings.add(u);
                    }
                }
            }
        }
    }
}
Also used : FontRecord(org.apache.poi.hssf.record.FontRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) HashSet(java.util.HashSet)

Example 8 with ExtendedFormatRecord

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

the class InternalWorkbook method createStyleRecord.

/**
     * Creates a new StyleRecord, for the given Extended
     *  Format index, and adds it onto the end of the
     *  records collection
     *
     * @param xfIndex the extended format index
     *
     * @return a new StyleRecord
     */
public StyleRecord createStyleRecord(int xfIndex) {
    // Style records always follow after
    //  the ExtendedFormat records
    StyleRecord newSR = new StyleRecord();
    newSR.setXFIndex(xfIndex);
    // Find the spot
    int addAt = -1;
    for (int i = records.getXfpos(); i < records.size() && addAt == -1; i++) {
        Record r = records.get(i);
        if (r instanceof ExtendedFormatRecord || r instanceof StyleRecord) {
        // Keep going
        } else {
            addAt = i;
        }
    }
    if (addAt == -1) {
        throw new IllegalStateException("No XF Records found!");
    }
    records.add(addAt, newSR);
    return newSR;
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) StyleRecord(org.apache.poi.hssf.record.StyleRecord) WindowProtectRecord(org.apache.poi.hssf.record.WindowProtectRecord) PasswordRev4Record(org.apache.poi.hssf.record.PasswordRev4Record) DateWindow1904Record(org.apache.poi.hssf.record.DateWindow1904Record) FormatRecord(org.apache.poi.hssf.record.FormatRecord) BookBoolRecord(org.apache.poi.hssf.record.BookBoolRecord) StyleRecord(org.apache.poi.hssf.record.StyleRecord) RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) EscherOptRecord(org.apache.poi.ddf.EscherOptRecord) HideObjRecord(org.apache.poi.hssf.record.HideObjRecord) Record(org.apache.poi.hssf.record.Record) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord) HyperlinkRecord(org.apache.poi.hssf.record.HyperlinkRecord) ProtectionRev4Record(org.apache.poi.hssf.record.ProtectionRev4Record) EOFRecord(org.apache.poi.hssf.record.EOFRecord) MMSRecord(org.apache.poi.hssf.record.MMSRecord) PrecisionRecord(org.apache.poi.hssf.record.PrecisionRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) CountryRecord(org.apache.poi.hssf.record.CountryRecord) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) NameCommentRecord(org.apache.poi.hssf.record.NameCommentRecord) FnGroupCountRecord(org.apache.poi.hssf.record.FnGroupCountRecord) DrawingGroupRecord(org.apache.poi.hssf.record.DrawingGroupRecord) BackupRecord(org.apache.poi.hssf.record.BackupRecord) EscherSplitMenuColorsRecord(org.apache.poi.ddf.EscherSplitMenuColorsRecord) ExternSheetRecord(org.apache.poi.hssf.record.ExternSheetRecord) WriteAccessRecord(org.apache.poi.hssf.record.WriteAccessRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) CodepageRecord(org.apache.poi.hssf.record.CodepageRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) FontRecord(org.apache.poi.hssf.record.FontRecord) UseSelFSRecord(org.apache.poi.hssf.record.UseSelFSRecord) WindowOneRecord(org.apache.poi.hssf.record.WindowOneRecord) WriteProtectRecord(org.apache.poi.hssf.record.WriteProtectRecord) ProtectRecord(org.apache.poi.hssf.record.ProtectRecord) InterfaceHdrRecord(org.apache.poi.hssf.record.InterfaceHdrRecord) DSFRecord(org.apache.poi.hssf.record.DSFRecord) InterfaceEndRecord(org.apache.poi.hssf.record.InterfaceEndRecord) SupBookRecord(org.apache.poi.hssf.record.SupBookRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) TabIdRecord(org.apache.poi.hssf.record.TabIdRecord) PasswordRecord(org.apache.poi.hssf.record.PasswordRecord) PaletteRecord(org.apache.poi.hssf.record.PaletteRecord) EscherDgRecord(org.apache.poi.ddf.EscherDgRecord) ExtSSTRecord(org.apache.poi.hssf.record.ExtSSTRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) FileSharingRecord(org.apache.poi.hssf.record.FileSharingRecord) RefreshAllRecord(org.apache.poi.hssf.record.RefreshAllRecord)

Example 9 with ExtendedFormatRecord

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

the class InternalWorkbook method createExtendedFormat.

/**
     * creates an default cell type ExtendedFormatRecord object.
     * @return ExtendedFormatRecord with initial defaults (cell-type)
     */
private static ExtendedFormatRecord createExtendedFormat() {
    ExtendedFormatRecord retval = new ExtendedFormatRecord();
    retval.setFontIndex((short) 0);
    retval.setFormatIndex((short) 0x0);
    retval.setCellOptions((short) 0x1);
    retval.setAlignmentOptions((short) 0x20);
    retval.setIndentionOptions((short) 0);
    retval.setBorderOptions((short) 0);
    retval.setPaletteOptions((short) 0);
    retval.setAdtlPaletteOptions((short) 0);
    retval.setFillPaletteOptions((short) 0x20c0);
    retval.setTopBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
    retval.setBottomBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
    retval.setLeftBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
    retval.setRightBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
    return retval;
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord)

Example 10 with ExtendedFormatRecord

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

the class FormatTrackingHSSFListener method processRecordInternally.

/**
	 * Process the record ourselves, but do not pass it on to the child
	 * Listener.
	 *
	 * @param record the record to be processed
	 */
public void processRecordInternally(Record record) {
    if (record instanceof FormatRecord) {
        FormatRecord fr = (FormatRecord) record;
        _customFormatRecords.put(Integer.valueOf(fr.getIndexCode()), fr);
    }
    if (record instanceof ExtendedFormatRecord) {
        ExtendedFormatRecord xr = (ExtendedFormatRecord) record;
        _xfRecords.add(xr);
    }
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) FormatRecord(org.apache.poi.hssf.record.FormatRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord)

Aggregations

ExtendedFormatRecord (org.apache.poi.hssf.record.ExtendedFormatRecord)13 FontRecord (org.apache.poi.hssf.record.FontRecord)2 FormatRecord (org.apache.poi.hssf.record.FormatRecord)2 HashSet (java.util.HashSet)1 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)1 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)1 EscherDgRecord (org.apache.poi.ddf.EscherDgRecord)1 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)1 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 EscherSpRecord (org.apache.poi.ddf.EscherSpRecord)1 EscherSplitMenuColorsRecord (org.apache.poi.ddf.EscherSplitMenuColorsRecord)1 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)1 BOFRecord (org.apache.poi.hssf.record.BOFRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 BookBoolRecord (org.apache.poi.hssf.record.BookBoolRecord)1 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)1 CodepageRecord (org.apache.poi.hssf.record.CodepageRecord)1 CountryRecord (org.apache.poi.hssf.record.CountryRecord)1 DSFRecord (org.apache.poi.hssf.record.DSFRecord)1