Fix bug #47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@999096 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c225333112
commit
c1a78f0f01
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.7-beta3" date="2010-??-??">
|
<release version="3.7-beta3" date="2010-??-??">
|
||||||
|
<action dev="poi-developers" type="add">47582 - XSSFCellStyle support for creating a style in one workbook based on a style from a different one</action>
|
||||||
<action dev="poi-developers" type="fix">49931 - Avoid concurrency problems when re-ordering multiple HSSF header records for a PageSettingsBlock</action>
|
<action dev="poi-developers" type="fix">49931 - Avoid concurrency problems when re-ordering multiple HSSF header records for a PageSettingsBlock</action>
|
||||||
<action dev="poi-developers" type="fix">49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships</action>
|
<action dev="poi-developers" type="fix">49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships</action>
|
||||||
<action dev="poi-developers" type="fix">48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better</action>
|
<action dev="poi-developers" type="fix">48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better</action>
|
||||||
|
@ -183,6 +183,8 @@ public class StylesTable extends POIXMLDocumentPart {
|
|||||||
* registration is requested.
|
* registration is requested.
|
||||||
* This allows people to create several fonts
|
* This allows people to create several fonts
|
||||||
* then customise them later.
|
* then customise them later.
|
||||||
|
* Note - End Users probably want to call
|
||||||
|
* {@link XSSFFont#registerTo(StylesTable)}
|
||||||
*/
|
*/
|
||||||
public int putFont(XSSFFont font, boolean forceRegistration) {
|
public int putFont(XSSFFont font, boolean forceRegistration) {
|
||||||
int idx = -1;
|
int idx = -1;
|
||||||
@ -193,8 +195,10 @@ public class StylesTable extends POIXMLDocumentPart {
|
|||||||
if (idx != -1) {
|
if (idx != -1) {
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
idx = fonts.size();
|
||||||
fonts.add(font);
|
fonts.add(font);
|
||||||
return fonts.size() - 1;
|
return idx;
|
||||||
}
|
}
|
||||||
public int putFont(XSSFFont font) {
|
public int putFont(XSSFFont font) {
|
||||||
return putFont(font, false);
|
return putFont(font, false);
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
|
import org.apache.poi.POIXMLException;
|
||||||
import org.apache.poi.ss.usermodel.BorderStyle;
|
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||||
@ -31,11 +32,13 @@ import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
|
|||||||
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
|
||||||
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
|
||||||
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
|
||||||
|
import org.apache.xmlbeans.XmlException;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
|
||||||
@ -132,8 +135,44 @@ public class XSSFCellStyle implements CellStyle {
|
|||||||
public void cloneStyleFrom(CellStyle source) {
|
public void cloneStyleFrom(CellStyle source) {
|
||||||
if(source instanceof XSSFCellStyle) {
|
if(source instanceof XSSFCellStyle) {
|
||||||
XSSFCellStyle src = (XSSFCellStyle)source;
|
XSSFCellStyle src = (XSSFCellStyle)source;
|
||||||
_cellXf.set(src.getCoreXf());
|
|
||||||
_cellStyleXf.set(src.getStyleXf());
|
// Is it on our Workbook?
|
||||||
|
if(src._stylesSource == _stylesSource) {
|
||||||
|
// Nice and easy
|
||||||
|
_cellXf.set(src.getCoreXf());
|
||||||
|
_cellStyleXf.set(src.getStyleXf());
|
||||||
|
} else {
|
||||||
|
// Copy the style
|
||||||
|
try {
|
||||||
|
_cellXf = CTXf.Factory.parse(
|
||||||
|
src.getCoreXf().toString()
|
||||||
|
);
|
||||||
|
} catch(XmlException e) {
|
||||||
|
throw new POIXMLException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy the format
|
||||||
|
String fmt = src.getDataFormatString();
|
||||||
|
setDataFormat(
|
||||||
|
(new XSSFDataFormat(_stylesSource)).getFormat(fmt)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Copy the font
|
||||||
|
try {
|
||||||
|
CTFont ctFont = CTFont.Factory.parse(
|
||||||
|
src.getFont().getCTFont().toString()
|
||||||
|
);
|
||||||
|
XSSFFont font = new XSSFFont(ctFont);
|
||||||
|
font.registerTo(_stylesSource);
|
||||||
|
setFont(font);
|
||||||
|
} catch(XmlException e) {
|
||||||
|
throw new POIXMLException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear out cached details
|
||||||
|
_font = null;
|
||||||
|
_cellAlignment = null;
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
|
throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
|
||||||
}
|
}
|
||||||
|
@ -19,16 +19,25 @@ package org.apache.poi.xssf.usermodel;
|
|||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
||||||
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
||||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
||||||
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||||
import org.apache.poi.xssf.model.StylesTable;
|
import org.apache.poi.xssf.model.StylesTable;
|
||||||
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
|
||||||
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
|
||||||
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
|
||||||
|
|
||||||
|
|
||||||
public class TestXSSFCellStyle extends TestCase {
|
public class TestXSSFCellStyle extends TestCase {
|
||||||
@ -586,12 +595,85 @@ public class TestXSSFCellStyle extends TestCase {
|
|||||||
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
|
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
|
||||||
*/
|
*/
|
||||||
public void testCloneStyleSameWB() {
|
public void testCloneStyleSameWB() {
|
||||||
// TODO
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
assertEquals(1, wb.getNumberOfFonts());
|
||||||
|
|
||||||
|
XSSFFont fnt = wb.createFont();
|
||||||
|
fnt.setFontName("TestingFont");
|
||||||
|
assertEquals(2, wb.getNumberOfFonts());
|
||||||
|
|
||||||
|
XSSFCellStyle orig = wb.createCellStyle();
|
||||||
|
orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
|
||||||
|
orig.setFont(fnt);
|
||||||
|
orig.setDataFormat((short)18);
|
||||||
|
|
||||||
|
assertTrue(HSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
|
||||||
|
assertTrue(fnt == orig.getFont());
|
||||||
|
assertTrue(18 == orig.getDataFormat());
|
||||||
|
|
||||||
|
XSSFCellStyle clone = wb.createCellStyle();
|
||||||
|
assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
|
||||||
|
assertFalse(fnt == clone.getFont());
|
||||||
|
assertFalse(18 == clone.getDataFormat());
|
||||||
|
|
||||||
|
clone.cloneStyleFrom(orig);
|
||||||
|
assertTrue(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
|
||||||
|
assertTrue(fnt == clone.getFont());
|
||||||
|
assertTrue(18 == clone.getDataFormat());
|
||||||
|
assertEquals(2, wb.getNumberOfFonts());
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
|
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
|
||||||
*/
|
*/
|
||||||
public void testCloneStyleDiffWB() {
|
public void testCloneStyleDiffWB() {
|
||||||
// TODO
|
XSSFWorkbook wbOrig = new XSSFWorkbook();
|
||||||
}
|
assertEquals(1, wbOrig.getNumberOfFonts());
|
||||||
|
assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
XSSFFont fnt = wbOrig.createFont();
|
||||||
|
fnt.setFontName("TestingFont");
|
||||||
|
assertEquals(2, wbOrig.getNumberOfFonts());
|
||||||
|
assertEquals(0, wbOrig.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
XSSFDataFormat fmt = wbOrig.createDataFormat();
|
||||||
|
fmt.getFormat("MadeUpOne");
|
||||||
|
fmt.getFormat("MadeUpTwo");
|
||||||
|
|
||||||
|
XSSFCellStyle orig = wbOrig.createCellStyle();
|
||||||
|
orig.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
|
||||||
|
orig.setFont(fnt);
|
||||||
|
orig.setDataFormat(fmt.getFormat("Test##"));
|
||||||
|
|
||||||
|
assertTrue(XSSFCellStyle.ALIGN_RIGHT == orig.getAlignment());
|
||||||
|
assertTrue(fnt == orig.getFont());
|
||||||
|
assertTrue(fmt.getFormat("Test##") == orig.getDataFormat());
|
||||||
|
|
||||||
|
assertEquals(2, wbOrig.getNumberOfFonts());
|
||||||
|
assertEquals(3, wbOrig.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
|
||||||
|
// Now a style on another workbook
|
||||||
|
XSSFWorkbook wbClone = new XSSFWorkbook();
|
||||||
|
assertEquals(1, wbClone.getNumberOfFonts());
|
||||||
|
assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
XSSFDataFormat fmtClone = wbClone.createDataFormat();
|
||||||
|
XSSFCellStyle clone = wbClone.createCellStyle();
|
||||||
|
|
||||||
|
assertEquals(1, wbClone.getNumberOfFonts());
|
||||||
|
assertEquals(0, wbClone.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
assertFalse(HSSFCellStyle.ALIGN_RIGHT == clone.getAlignment());
|
||||||
|
assertFalse("TestingFont" == clone.getFont().getFontName());
|
||||||
|
|
||||||
|
clone.cloneStyleFrom(orig);
|
||||||
|
|
||||||
|
assertEquals(2, wbClone.getNumberOfFonts());
|
||||||
|
assertEquals(1, wbClone.getStylesSource().getNumberFormats().size());
|
||||||
|
|
||||||
|
assertEquals(HSSFCellStyle.ALIGN_RIGHT, clone.getAlignment());
|
||||||
|
assertEquals("TestingFont", clone.getFont().getFontName());
|
||||||
|
assertEquals(fmtClone.getFormat("Test##"), clone.getDataFormat());
|
||||||
|
assertFalse(fmtClone.getFormat("Test##") == fmt.getFormat("Test##"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user