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);
}
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);
}
}
}
}
}
}
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;
}
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;
}
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);
}
}
Aggregations