Search in sources :

Example 11 with ExtendedFormatRecord

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

the class HSSFCell method getCellStyle.

/**
     * get the style for the cell.  This is a reference to a cell style contained in the workbook
     * object.
     * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getCellStyleAt(int)
     */
public HSSFCellStyle getCellStyle() {
    short styleIndex = _record.getXFIndex();
    ExtendedFormatRecord xf = _book.getWorkbook().getExFormatAt(styleIndex);
    return new HSSFCellStyle(styleIndex, xf, _book);
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord)

Example 12 with ExtendedFormatRecord

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

the class HSSFCell method applyUserCellStyle.

/**
     * Applying a user-defined style (UDS) is special. Excel does not directly reference user-defined styles, but
     * instead create a 'proxy' ExtendedFormatRecord referencing the UDS as parent.
     *
     * The proceudre to apply a UDS is as follows:
     *
     * 1. search for a ExtendedFormatRecord with parentIndex == style.getIndex()
     *    and xfType ==  ExtendedFormatRecord.XF_CELL.
     * 2. if not found then create a new ExtendedFormatRecord and copy all attributes from the user-defined style
     *    and set the parentIndex to be style.getIndex()
     * 3. return the index of the ExtendedFormatRecord, this will be assigned to the parent cell record
     *
     * @param style  the user style to apply
     *
     * @return  the index of a ExtendedFormatRecord record that will be referenced by the cell
     */
private short applyUserCellStyle(HSSFCellStyle style) {
    if (style.getUserStyleName() == null) {
        throw new IllegalArgumentException("Expected user-defined style");
    }
    InternalWorkbook iwb = _book.getWorkbook();
    short userXf = -1;
    int numfmt = iwb.getNumExFormats();
    for (short i = 0; i < numfmt; i++) {
        ExtendedFormatRecord xf = iwb.getExFormatAt(i);
        if (xf.getXFType() == ExtendedFormatRecord.XF_CELL && xf.getParentIndex() == style.getIndex()) {
            userXf = i;
            break;
        }
    }
    short styleIndex;
    if (userXf == -1) {
        ExtendedFormatRecord xfr = iwb.createCellXF();
        xfr.cloneStyleFrom(iwb.getExFormatAt(style.getIndex()));
        xfr.setIndentionOptions((short) 0);
        xfr.setXFType(ExtendedFormatRecord.XF_CELL);
        xfr.setParentIndex(style.getIndex());
        styleIndex = (short) numfmt;
    } else {
        styleIndex = userXf;
    }
    return styleIndex;
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 13 with ExtendedFormatRecord

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

the class HSSFOptimiser method optimiseCellStyles.

/**
    * Goes through the Wokrbook, optimising the cell styles
    *  by removing duplicate ones, and ones that aren't used.
    * For best results, optimise the fonts via a call to
    *  {@link #optimiseFonts(HSSFWorkbook)} first.
    * @param workbook The workbook in which to optimise the cell styles
    */
public static void optimiseCellStyles(HSSFWorkbook workbook) {
    // Where each style has ended up, and if we need to
    //  delete the record for it. Start off with no change
    short[] newPos = new short[workbook.getWorkbook().getNumExFormats()];
    boolean[] isUsed = new boolean[newPos.length];
    boolean[] zapRecords = new boolean[newPos.length];
    for (int i = 0; i < newPos.length; i++) {
        isUsed[i] = false;
        newPos[i] = (short) i;
        zapRecords[i] = false;
    }
    // Get each style record, so we can do deletes
    //  without getting confused
    ExtendedFormatRecord[] xfrs = new ExtendedFormatRecord[newPos.length];
    for (int i = 0; i < newPos.length; i++) {
        xfrs[i] = workbook.getWorkbook().getExFormatAt(i);
    }
    // Only work on user added ones, which come after 20
    for (int i = 21; 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++) {
            ExtendedFormatRecord xfCheck = workbook.getWorkbook().getExFormatAt(j);
            if (xfCheck.equals(xfrs[i])) {
                earlierDuplicate = j;
            }
        }
        // If we got a duplicate, mark it as such
        if (earlierDuplicate != -1) {
            newPos[i] = (short) earlierDuplicate;
            zapRecords[i] = true;
        }
        // If we got a duplicate, mark the one we're keeping as used
        if (earlierDuplicate != -1) {
            isUsed[earlierDuplicate] = true;
        }
    }
    //  styles aren't actually being used (don't touch built-in ones)
    for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
        HSSFSheet s = workbook.getSheetAt(sheetNum);
        for (Row row : s) {
            for (Cell cellI : row) {
                HSSFCell cell = (HSSFCell) cellI;
                short oldXf = cell.getCellValueRecord().getXFIndex();
                isUsed[oldXf] = true;
            }
        }
    }
    // Mark any that aren't used as needing zapping
    for (int i = 21; i < isUsed.length; i++) {
        if (!isUsed[i]) {
            // Un-used style, can be removed
            zapRecords[i] = true;
            newPos[i] = 0;
        }
    }
    // Only work on user added ones, which come after 20
    for (int i = 21; 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 style records
    // removing by index, because removing by object may delete
    // styles we did not intend to (the ones that _were_ duplicated and not the duplicates)
    int max = newPos.length;
    // to adjust index after deletion
    int removed = 0;
    for (int i = 21; i < max; i++) {
        if (zapRecords[i + removed]) {
            workbook.getWorkbook().removeExFormatRecord(i);
            i--;
            max--;
            removed++;
        }
    }
    // Finally, update the cells to point at their new extended format records
    for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
        HSSFSheet s = workbook.getSheetAt(sheetNum);
        for (Row row : s) {
            for (Cell cellI : row) {
                HSSFCell cell = (HSSFCell) cellI;
                short oldXf = cell.getCellValueRecord().getXFIndex();
                HSSFCellStyle newStyle = workbook.getCellStyleAt(newPos[oldXf]);
                cell.setCellStyle(newStyle);
            }
        }
    }
}
Also used : ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

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