fixed setting named styles to HSSFCells. see Bugzilla 50912

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1080689 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2011-03-11 17:33:36 +00:00
parent 31681260ed
commit ab33e375d3
4 changed files with 69 additions and 9 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta2" date="2011-??-??">
<action dev="poi-developers" type="fix">50912 - fixed setting named styles to HSSFCells</action>
<action dev="poi-developers" type="fix">50779 - fixed RecordFormatException when reading unicode strings with photenic data</action>
<action dev="poi-developers" type="fix">50718 - More helpful error message when you try to create a CellReference with #REF!</action>
<action dev="poi-developers" type="fix">50784 - XSSFColors return by XSSFFont now have theme information applied to them</action>

View File

@ -907,8 +907,15 @@ public class HSSFCell implements Cell {
// Verify it really does belong to our workbook
style.verifyBelongsToWorkbook(_book);
short styleIndex;
if(style.getUserStyleName() != null) {
styleIndex = applyUserCellStyle(style);
} else {
styleIndex = style.getIndex();
}
// Change our cell record to use this style
_record.setXFIndex(style.getIndex());
_record.setXFIndex(styleIndex);
}
/**
@ -1245,4 +1252,49 @@ public class HSSFCell implements Cell {
notifyArrayFormulaChanging(msg);
}
/**
* 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;
}
}

View File

@ -69,12 +69,14 @@ public final class HSSFCellStyle implements CellStyle {
* cases there'll be a fully defined parent.
*/
public HSSFCellStyle getParentStyle() {
if(_format.getParentIndex() == 0) {
short parentIndex = _format.getParentIndex();
// parentIndex equal 0xFFF indicates no inheritance from a cell style XF (See 2.4.353 XF)
if(parentIndex == 0 || parentIndex == 0xFFF) {
return null;
}
return new HSSFCellStyle(
_format.getParentIndex(),
_workbook.getExFormatAt(_format.getParentIndex()),
parentIndex,
_workbook.getExFormatAt(parentIndex),
_workbook
);
}

View File

@ -17,17 +17,16 @@
package org.apache.poi.hssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.TempFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import junit.framework.TestCase;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.util.TempFile;
/**
* Class to test cell styling functionality
*
@ -326,5 +325,11 @@ public final class TestCellStyle extends TestCase {
assertEquals(null, cs3.getUserStyleName());
assertEquals("style1", cs2.getParentStyle().getUserStyleName());
assertEquals("style2", cs3.getParentStyle().getUserStyleName());
// now apply a named style to a new cell
HSSFCell c4 = s.getRow(0).createCell(1);
c4.setCellStyle(cs2);
assertEquals("style1", c4.getCellStyle().getParentStyle().getUserStyleName());
}
}