applied patch #45492 submitted by Gisella Bronzetti,also performed major cleanup of StylesTable and its components, the goal was to ensure that StylesTable works as a cache of styling components, not just a regular store of fill patterns and cell styles.

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@701797 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-10-05 13:56:28 +00:00
parent 0d051eb3c2
commit cdfa36cada
18 changed files with 2222 additions and 768 deletions

View File

@ -68,6 +68,11 @@ public class CreateCell {
cell.setCellValue(new Date()); cell.setCellValue(new Date());
cell.setCellStyle(style); cell.setCellStyle(style);
//hyperlink
row.createCell(7).setCellFormula("SUM(A1:B1)");
cell.setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
// Write the output to a file // Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx"); FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
wb.write(fileOut); wb.write(fileOut);

View File

@ -32,34 +32,72 @@ import java.io.FileOutputStream;
public class WorkingWithFonts { public class WorkingWithFonts {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook(); Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet"); Sheet sheet = wb.createSheet("Fonts");
// Create a row and put some cells in it. Rows are 0 based. Font font0 = wb.createFont();
Row row = sheet.createRow((short) 1); font0.setColor(IndexedColors.BROWN.getIndex());
CellStyle style0 = wb.createCellStyle();
style0.setFont(font0);
// Create a new font and alter it. Font font1 = wb.createFont();
Font font = wb.createFont(); font1.setFontHeightInPoints((short)14);
font.setFontHeightInPoints((short)24); font1.setFontName("Courier New");
font.setFontName("Courier New"); font1.setColor(IndexedColors.RED.getIndex());
CellStyle style1 = wb.createCellStyle();
style1.setFont(font1);
font.setColor(IndexedColors.RED.getIndex()); Font font2 = wb.createFont();
font2.setFontHeightInPoints((short)16);
font2.setFontName("Arial");
font2.setColor(IndexedColors.GREEN.getIndex());
CellStyle style2 = wb.createCellStyle();
style2.setFont(font2);
font.setItalic(true); Font font3 = wb.createFont();
font.setStrikeout(true); font3.setFontHeightInPoints((short)18);
font3.setFontName("Times New Roman");
font3.setColor(IndexedColors.LAVENDER.getIndex());
CellStyle style3 = wb.createCellStyle();
style3.setFont(font3);
// Fonts are set into a style so create a new one to use. Font font4 = wb.createFont();
CellStyle style = wb.createCellStyle(); font4.setFontHeightInPoints((short)18);
style.setFont(font); font4.setFontName("Wingdings");
font4.setColor(IndexedColors.GOLD.getIndex());
CellStyle style4 = wb.createCellStyle();
style4.setFont(font4);
// Create a cell and put a value in it. Font font5 = wb.createFont();
Cell cell = row.createCell((short) 1); font5.setFontName("Symbol");
cell.setCellValue(1974); CellStyle style5 = wb.createCellStyle();
//cell.setCellValue(new XSSFRichTextString("This is a test of fonts")); style5.setFont(font5);
cell.setCellStyle(style);
Cell cell0 = sheet.createRow(0).createCell(1);
cell0.setCellValue("Default");
cell0.setCellStyle(style0);
Cell cell1 = sheet.createRow(1).createCell(1);
cell1.setCellValue("Courier");
cell1.setCellStyle(style1);
Cell cell2 = sheet.createRow(2).createCell(1);
cell2.setCellValue("Arial");
cell2.setCellStyle(style2);
Cell cell3 = sheet.createRow(3).createCell(1);
cell3.setCellValue("Times New Roman");
cell3.setCellStyle(style3);
Cell cell4 = sheet.createRow(4).createCell(1);
cell4.setCellValue("Wingdings");
cell4.setCellStyle(style4);
Cell cell5 = sheet.createRow(5).createCell(1);
cell5.setCellValue("Symbol");
cell5.setCellStyle(style5);
// Write the output to a file // Write the output to a file
FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx");
FileOutputStream fileOut = new FileOutputStream("fonts.xlsx");
wb.write(fileOut); wb.write(fileOut);
fileOut.close(); fileOut.close();
} }

View File

@ -276,14 +276,6 @@ public interface CellStyle {
*/ */
short getFontIndex(); short getFontIndex();
/**
* gets the font for this style
* @param parentWorkbook The HSSFWorkbook that this style belongs to
* @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndex()
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short)
*/
Font getFont(Workbook parentWorkbook);
/** /**
* set the cell's using this style to be hidden * set the cell's using this style to be hidden
* @param hidden - whether the cell using this style should be hidden * @param hidden - whether the cell using this style should be hidden

View File

@ -270,6 +270,17 @@ public interface Font {
*/ */
void setCharSet(byte charset); void setCharSet(byte charset);
String toString(); /**
* get the index within the XSSFWorkbook (sequence within the collection of Font objects)
*
* @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which)
*/
public short getIndex();
public void setBoldweight(short boldweight);
public short getBoldweight();
} }

View File

@ -66,9 +66,9 @@ import org.openxml4j.opc.PackageRelationship;
*/ */
public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSSFModel { public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSSFModel {
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>(); private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
private final List<CTFont> fonts = new ArrayList<CTFont>(); private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
private final List<CTFill> fills = new LinkedList<CTFill>(); private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
private final List<CTBorder> borders = new LinkedList<CTBorder>(); private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
private final List<CTXf> styleXfs = new LinkedList<CTXf>(); private final List<CTXf> styleXfs = new LinkedList<CTXf>();
private final List<CTXf> xfs = new LinkedList<CTXf>(); private final List<CTXf> xfs = new LinkedList<CTXf>();
@ -122,17 +122,21 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) { for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode()); numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
} }
if(doc.getStyleSheet().getFonts() != null) if(doc.getStyleSheet().getFonts() != null){
for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) { int idx = 0;
fonts.add(font); for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
} XSSFFont f = new XSSFFont(font, idx);
fonts.add(f);
idx++;
}
}
if(doc.getStyleSheet().getFills() != null) if(doc.getStyleSheet().getFills() != null)
for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) { for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
fills.add(fill); fills.add(new XSSFCellFill(fill));
} }
if(doc.getStyleSheet().getBorders() != null) if(doc.getStyleSheet().getBorders() != null)
for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) { for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
borders.add(border); borders.add(new XSSFCellBorder(border));
} }
if(doc.getStyleSheet().getCellXfs() != null) if(doc.getStyleSheet().getCellXfs() != null)
for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) { for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
@ -181,12 +185,17 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
return newKey; return newKey;
} }
public Font getFontAt(long idx) { public XSSFFont getFontAt(long idx) {
return new XSSFFont(fonts.get((int) idx)); return fonts.get((int)idx);
} }
public synchronized long putFont(Font font) { public long putFont(Font font) {
return putFont((XSSFFont)font, fonts); int idx = fonts.indexOf(font);
if (idx != -1) {
return idx;
}
fonts.add((XSSFFont)font);
return fonts.size() - 1;
} }
public XSSFCellStyle getStyleAt(long idx) { public XSSFCellStyle getStyleAt(long idx) {
@ -209,24 +218,44 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
return xfs.indexOf(mainXF); return xfs.indexOf(mainXF);
} }
public XSSFCellBorder getBorderAt(long idx) { public XSSFCellBorder getBorderAt(int idx) {
return new XSSFCellBorder(borders.get((int)idx)); return borders.get(idx);
}
public long putBorder(XSSFCellBorder border) {
return putBorder(border, borders);
} }
public XSSFCellFill getFillAt(long idx) { public int putBorder(XSSFCellBorder border) {
return new XSSFCellFill(fills.get((int) idx)); int idx = borders.indexOf(border);
if (idx != -1) {
return idx;
}
borders.add(border);
return borders.size() - 1;
} }
public long putFill(XSSFCellFill fill) {
return fill.putFill(fills); public List<XSSFCellBorder> getBorders(){
return borders;
}
public XSSFCellFill getFillAt(int idx) {
return fills.get(idx);
}
public List<XSSFCellFill> getFills(){
return fills;
}
public int putFill(XSSFCellFill fill) {
int idx = fills.indexOf(fill);
if (idx != -1) {
return idx;
}
fills.add(fill);
return fills.size() - 1;
} }
public CTXf getCellXfAt(long idx) { public CTXf getCellXfAt(long idx) {
return xfs.get((int) idx); return xfs.get((int) idx);
} }
public long putCellXf(CTXf cellXf) { public int putCellXf(CTXf cellXf) {
xfs.add(cellXf); xfs.add(cellXf);
return xfs.size(); return xfs.size();
} }
@ -234,7 +263,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
public CTXf getCellStyleXfAt(long idx) { public CTXf getCellStyleXfAt(long idx) {
return styleXfs.get((int) idx); return styleXfs.get((int) idx);
} }
public long putCellStyleXf(CTXf cellStyleXf) { public int putCellStyleXf(CTXf cellStyleXf) {
styleXfs.add(cellStyleXf); styleXfs.add(cellStyleXf);
return styleXfs.size(); return styleXfs.size();
} }
@ -323,24 +352,32 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
} }
doc.getStyleSheet().setNumFmts(formats); doc.getStyleSheet().setNumFmts(formats);
int idx = 0;
// Fonts // Fonts
CTFonts ctFonts = CTFonts.Factory.newInstance(); CTFonts ctFonts = CTFonts.Factory.newInstance();
ctFonts.setCount(fonts.size()); ctFonts.setCount(fonts.size());
ctFonts.setFontArray( CTFont[] ctfnt = new CTFont[fonts.size()];
fonts.toArray(new CTFont[fonts.size()]) idx = 0;
); for(XSSFFont f : fonts) ctfnt[idx++] = f.getCTFont();
ctFonts.setFontArray(ctfnt);
doc.getStyleSheet().setFonts(ctFonts); doc.getStyleSheet().setFonts(ctFonts);
// Fills // Fills
CTFills ctFills = CTFills.Factory.newInstance(); CTFills ctFills = CTFills.Factory.newInstance();
ctFills.setCount(fills.size()); ctFills.setCount(fills.size());
ctFills.setFillArray(fills.toArray(new CTFill[fills.size()])); CTFill[] ctf = new CTFill[fills.size()];
idx = 0;
for(XSSFCellFill f : fills) ctf[idx++] = f.getCTFill();
ctFills.setFillArray(ctf);
doc.getStyleSheet().setFills(ctFills); doc.getStyleSheet().setFills(ctFills);
// Borders // Borders
CTBorders ctBorders = CTBorders.Factory.newInstance(); CTBorders ctBorders = CTBorders.Factory.newInstance();
ctBorders.setCount(borders.size()); ctBorders.setCount(borders.size());
ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()])); CTBorder[] ctb = new CTBorder[borders.size()];
idx = 0;
for(XSSFCellBorder b : borders) ctb[idx++] = b.getCTBorder();
ctBorders.setBorderArray(ctb);
doc.getStyleSheet().setBorders(ctBorders); doc.getStyleSheet().setBorders(ctBorders);
// Xfs // Xfs
@ -384,25 +421,17 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
out.close(); out.close();
} }
private long putBorder(XSSFCellBorder border, List<CTBorder> borders) {
return border.putBorder((LinkedList<CTBorder>) borders); // TODO - use List instead of LinkedList
}
private long putFont(XSSFFont font, List<CTFont> fonts) {
return font.putFont((ArrayList<CTFont>) fonts); // TODO - use List instead of ArrayList
}
private void initialize() { private void initialize() {
//CTFont ctFont = createDefaultFont(); //CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont(); XSSFFont xssfFont = createDefaultFont();
fonts.add(xssfFont.getCTFont()); fonts.add(xssfFont);
CTFill[] ctFill = createDefaultFills(); CTFill[] ctFill = createDefaultFills();
fills.add(ctFill[0]); fills.add(new XSSFCellFill(ctFill[0]));
fills.add(ctFill[1]); fills.add(new XSSFCellFill(ctFill[1]));
CTBorder ctBorder = createDefaultBorder(); CTBorder ctBorder = createDefaultBorder();
borders.add(ctBorder); borders.add(new XSSFCellBorder(ctBorder));
CTXf styleXf = createDefaultXf(); CTXf styleXf = createDefaultXf();
styleXfs.add(styleXf); styleXfs.add(styleXf);
@ -439,7 +468,7 @@ public class StylesTable extends POIXMLDocumentPart implements StylesSource, XSS
private XSSFFont createDefaultFont() { private XSSFFont createDefaultFont() {
CTFont ctFont = CTFont.Factory.newInstance(); CTFont ctFont = CTFont.Factory.newInstance();
XSSFFont xssfFont=new XSSFFont(ctFont); XSSFFont xssfFont=new XSSFFont(ctFont, 0);
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE); xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME); xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);

View File

@ -0,0 +1,113 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
/**
* The enumeration value indicating the line style of a border in a cell,
* i.e., whether it is borded dash dot, dash dot dot, dashed, dotted, double, hair, medium,
* medium dash dot, medium dash dot dot, medium dashed, none, slant dash dot, thick or thin.
*/
public enum BorderStyle {
/**
* No border
*/
NONE,
/**
* Thin border
*/
THIN,
/**
* Medium border
*/
MEDIUM,
/**
* dash border
*/
DASHED,
/**
* dot border
*/
HAIR,
/**
* Thick border
*/
THICK,
/**
* double-line border
*/
DOUBLE,
/**
* hair-line border
*/
DOTTED,
/**
* Medium dashed border
*/
MEDIUM_DASHED,
/**
* dash-dot border
*/
DASH_DOT,
/**
* medium dash-dot border
*/
MEDIUM_DASH_DOT,
/**
* dash-dot-dot border
*/
DASH_DOT_DOT,
/**
* medium dash-dot-dot border
*/
MEDIUM_DASH_DOT_DOTC,
/**
* slanted dash-dot border
*/
SLANTED_DASH_DOT;
}

View File

@ -0,0 +1,85 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
/**
* The enumeration value indicating the style of fill pattern being used for a cell format.
*
*/
public enum FillPatternType {
/** No background */
NO_FILL,
/** Solidly filled */
SOLID_FOREGROUND,
/** Small fine dots */
FINE_DOTS,
/** Wide dots */
ALT_BARS,
/** Sparse dots */
SPARSE_DOTS,
/** Thick horizontal bands */
THICK_HORZ_BANDS,
/** Thick vertical bands */
THICK_VERT_BANDS,
/** Thick backward facing diagonals */
THICK_BACKWARD_DIAG,
/** Thick forward facing diagonals */
THICK_FORWARD_DIAG,
/** Large spots */
BIG_SPOTS,
/** Brick-like layout */
BRICKS,
/** Thin horizontal bands */
THIN_HORZ_BANDS,
/** Thin vertical bands */
THIN_VERT_BANDS,
/** Thin backward diagonal */
THIN_BACKWARD_DIAG,
/** Thin forward diagonal */
THIN_FORWARD_DIAG,
/** Squares */
SQUARES,
/** Diamonds */
DIAMONDS,
/** Less Dots */
LESS_DOTS,
/** Least Dots */
LEAST_DOTS;
}

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.extensions.XSSFColor; import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.apache.poi.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
@ -54,6 +55,7 @@ public class XSSFFont implements Font {
public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex(); public static final short DEFAULT_FONT_COLOR = IndexedColors.BLACK.getIndex();
private CTFont ctFont; private CTFont ctFont;
private short index;
/** /**
* Create a new XSSFFont * Create a new XSSFFont
@ -62,6 +64,12 @@ public class XSSFFont implements Font {
*/ */
public XSSFFont(CTFont font) { public XSSFFont(CTFont font) {
this.ctFont = font; this.ctFont = font;
this.index = 0;
}
public XSSFFont(CTFont font, int index) {
this.ctFont = font;
this.index = (short)index;
} }
/** /**
@ -69,6 +77,8 @@ public class XSSFFont implements Font {
*/ */
protected XSSFFont() { protected XSSFFont() {
this.ctFont = CTFont.Factory.newInstance(); this.ctFont = CTFont.Factory.newInstance();
setFontName(DEFAULT_FONT_NAME);
setFontHeight(DEFAULT_FONT_SIZE);
} }
/** /**
@ -130,8 +140,7 @@ public class XSSFFont implements Font {
*/ */
public XSSFColor getRgbColor() { public XSSFColor getRgbColor() {
CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? null : ctFont.getColorArray(0); CTColor ctColor = ctFont.sizeOfColorArray() == 0 ? null : ctFont.getColorArray(0);
XSSFColor color = new XSSFColor(ctColor); return ctColor == null ? null : new XSSFColor(ctColor);
return color;
} }
@ -180,7 +189,7 @@ public class XSSFFont implements Font {
*/ */
public String getFontName() { public String getFontName() {
CTFontName name = ctFont.sizeOfNameArray() == 0 ? null : ctFont.getNameArray(0); CTFontName name = ctFont.sizeOfNameArray() == 0 ? null : ctFont.getNameArray(0);
return name == null ? null : name.getVal(); return name == null ? DEFAULT_FONT_NAME : name.getVal();
} }
/** /**
@ -265,8 +274,29 @@ public class XSSFFont implements Font {
* @param bold - boldness to use * @param bold - boldness to use
*/ */
public void setBold(boolean bold) { public void setBold(boolean bold) {
CTBooleanProperty ctBold = ctFont.sizeOfBArray() == 0 ? ctFont.addNewB() : ctFont.getBArray(0); if(bold){
ctBold.setVal(true); CTBooleanProperty ctBold = ctFont.sizeOfBArray() == 0 ? ctFont.addNewB() : ctFont.getBArray(0);
ctBold.setVal(bold);
} else {
ctFont.setBArray(null);
}
}
public void setBoldweight(short boldweight)
{
setBold(boldweight == BOLDWEIGHT_BOLD);
}
/**
* get the boldness to use
* @return boldweight
* @see #BOLDWEIGHT_NORMAL
* @see #BOLDWEIGHT_BOLD
*/
public short getBoldweight()
{
return getBold() ? BOLDWEIGHT_BOLD : BOLDWEIGHT_NORMAL;
} }
/** /**
@ -323,6 +353,10 @@ public class XSSFFont implements Font {
ctColor.setIndexed(color); ctColor.setIndexed(color);
} }
} }
public void setColor(XSSFColor color) {
if(color == null) ctFont.setColorArray(null);
else ctFont.setColorArray(new CTColor[]{color.getCTColor()});
}
/** /**
* set the font height in points. * set the font height in points.
@ -385,7 +419,7 @@ public class XSSFFont implements Font {
*/ */
public void setFontName(String name) { public void setFontName(String name) {
CTFontName fontName = ctFont.sizeOfNameArray() == 0 ? ctFont.addNewName() : ctFont.getNameArray(0); CTFontName fontName = ctFont.sizeOfNameArray() == 0 ? ctFont.addNewName() : ctFont.getNameArray(0);
fontName.setVal(name); fontName.setVal(name == null ? DEFAULT_FONT_NAME : name);
} }
@ -396,8 +430,12 @@ public class XSSFFont implements Font {
* @param italic - value for italics or not * @param italic - value for italics or not
*/ */
public void setItalic(boolean italic) { public void setItalic(boolean italic) {
CTBooleanProperty bool = ctFont.sizeOfIArray() == 0 ? ctFont.addNewI() : ctFont.getIArray(0); if(italic){
bool.setVal(italic); CTBooleanProperty bool = ctFont.sizeOfIArray() == 0 ? ctFont.addNewI() : ctFont.getIArray(0);
bool.setVal(italic);
} else {
ctFont.setIArray(null);
}
} }
@ -408,8 +446,11 @@ public class XSSFFont implements Font {
* @param strikeout - value for strikeout or not * @param strikeout - value for strikeout or not
*/ */
public void setStrikeout(boolean strikeout) { public void setStrikeout(boolean strikeout) {
CTBooleanProperty strike = ctFont.sizeOfStrikeArray() == 0 ? ctFont.addNewStrike() : ctFont.getStrikeArray(0); if(!strikeout) ctFont.setStrikeArray(null);
strike.setVal(strikeout); else {
CTBooleanProperty strike = ctFont.sizeOfStrikeArray() == 0 ? ctFont.addNewStrike() : ctFont.getStrikeArray(0);
strike.setVal(strikeout);
}
} }
/** /**
@ -423,17 +464,21 @@ public class XSSFFont implements Font {
* @see #SS_SUB * @see #SS_SUB
*/ */
public void setTypeOffset(short offset) { public void setTypeOffset(short offset) {
CTVerticalAlignFontProperty offsetProperty = ctFont.sizeOfVertAlignArray() == 0 ? ctFont.addNewVertAlign() : ctFont.getVertAlignArray(0); if(offset == Font.SS_NONE){
switch (offset) { ctFont.setVertAlignArray(null);
case Font.SS_NONE: } else {
offsetProperty.setVal(STVerticalAlignRun.BASELINE); CTVerticalAlignFontProperty offsetProperty = ctFont.sizeOfVertAlignArray() == 0 ? ctFont.addNewVertAlign() : ctFont.getVertAlignArray(0);
break; switch (offset) {
case Font.SS_SUB: case Font.SS_NONE:
offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT); offsetProperty.setVal(STVerticalAlignRun.BASELINE);
break; break;
case Font.SS_SUPER: case Font.SS_SUB:
offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT); offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);
break; break;
case Font.SS_SUPER:
offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);
break;
}
} }
} }
@ -445,24 +490,25 @@ public class XSSFFont implements Font {
* @see FontUnderline * @see FontUnderline
*/ */
public void setUnderline(byte underline) { public void setUnderline(byte underline) {
CTUnderlineProperty ctUnderline = ctFont.sizeOfUArray() == 0 ? ctFont.addNewU() : ctFont.getUArray(0); if(underline == Font.U_NONE) {
switch (underline) { ctFont.setUArray(null);
case Font.U_DOUBLE: } else {
ctUnderline.setVal(FontUnderline.DOUBLE.getValue()); CTUnderlineProperty ctUnderline = ctFont.sizeOfUArray() == 0 ? ctFont.addNewU() : ctFont.getUArray(0);
break; switch (underline) {
case Font.U_DOUBLE_ACCOUNTING: case Font.U_DOUBLE:
ctUnderline.setVal(FontUnderline.DOUBLE_ACCOUNTING.getValue()); ctUnderline.setVal(FontUnderline.DOUBLE.getValue());
break; break;
case Font.U_SINGLE_ACCOUNTING: case Font.U_DOUBLE_ACCOUNTING:
ctUnderline.setVal(FontUnderline.SINGLE_ACCOUNTING.getValue()); ctUnderline.setVal(FontUnderline.DOUBLE_ACCOUNTING.getValue());
break; break;
case Font.U_NONE: case Font.U_SINGLE_ACCOUNTING:
ctUnderline.setVal(FontUnderline.NONE.getValue()); ctUnderline.setVal(FontUnderline.SINGLE_ACCOUNTING.getValue());
break; break;
case Font.U_SINGLE: case Font.U_SINGLE:
default: default:
ctUnderline.setVal(FontUnderline.SINGLE.getValue()); ctUnderline.setVal(FontUnderline.NONE.getValue());
break; break;
}
} }
} }
@ -486,18 +532,13 @@ public class XSSFFont implements Font {
} }
public long putFont(ArrayList<CTFont> fonts) { /**
//TODO * Register ourselfs in the style table
/* */
* we need to implement a method equals to check that 2 instances of CTFont public long putFont(StylesTable styles) {
* are different by comparison of all font attributes. short idx = (short)styles.putFont(this);
* NB: take a look to findFont method in XSSFWorkbook this.index = idx;
*/ return idx;
if (fonts.contains(ctFont)) {
return fonts.indexOf(ctFont);
}
fonts.add(ctFont);
return fonts.size() - 1;
} }
/** /**
@ -558,5 +599,26 @@ public class XSSFFont implements Font {
setFamily(family.getValue()); setFamily(family.getValue());
} }
/**
* get the index within the XSSFWorkbook (sequence within the collection of Font objects)
* @return unique index number of the underlying record this Font represents (probably you don't care
* unless you're comparing which one is which)
*/
public short getIndex()
{
return index;
}
public int hashCode(){
return ctFont.toString().hashCode();
}
public boolean equals(Object o){
if(!(o instanceof XSSFFont)) return false;
XSSFFont cf = (XSSFFont)o;
return ctFont.toString().equals(cf.getCTFont().toString());
}
} }

View File

@ -30,7 +30,6 @@ import org.apache.poi.ss.usermodel.Palette;
import org.apache.poi.ss.usermodel.PictureData; import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
@ -77,7 +76,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* A collection of shared objects used for styling content, * A collection of shared objects used for styling content,
* e.g. fonts, cell styles, colors, etc. * e.g. fonts, cell styles, colors, etc.
*/ */
private StylesSource stylesSource; private StylesTable stylesSource;
/** /**
* Used to keep track of the data formatter so that all * Used to keep track of the data formatter so that all
@ -160,7 +159,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
HashMap<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>(); HashMap<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
for(POIXMLDocumentPart p : getRelations()){ for(POIXMLDocumentPart p : getRelations()){
if(p instanceof SharedStringsTable) sharedStringSource = (SharedStringsTable)p; if(p instanceof SharedStringsTable) sharedStringSource = (SharedStringsTable)p;
else if(p instanceof StylesSource) stylesSource = (StylesSource)p; else if(p instanceof StylesTable) stylesSource = (StylesTable)p;
else if (p instanceof XSSFSheet) { else if (p instanceof XSSFSheet) {
shIdMap.put(p.getPackageRelationship().getId(), (XSSFSheet)p); shIdMap.put(p.getPackageRelationship().getId(), (XSSFSheet)p);
} }
@ -300,9 +299,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
xf.setFillId(0); xf.setFillId(0);
xf.setBorderId(0); xf.setBorderId(0);
xf.setXfId(0); xf.setXfId(0);
int xfSize=((StylesTable)stylesSource)._getStyleXfsSize(); int xfSize=(stylesSource)._getStyleXfsSize();
long indexXf=((StylesTable)stylesSource).putCellXf(xf); long indexXf=(stylesSource).putCellXf(xf);
XSSFCellStyle style = new XSSFCellStyle(new Long(indexXf-1).intValue(), xfSize-1, (StylesTable)stylesSource); XSSFCellStyle style = new XSSFCellStyle(new Long(indexXf-1).intValue(), xfSize-1, stylesSource);
return style; return style;
} }
@ -324,8 +323,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return new font object * @return new font object
*/ */
public XSSFFont createFont() { public XSSFFont createFont() {
XSSFFont font= new XSSFFont(); XSSFFont font = new XSSFFont();
stylesSource.putFont(font); font.putFont(stylesSource);
return font; return font;
} }
@ -394,7 +393,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* Finds a font that matches the one with the supplied attributes * Finds a font that matches the one with the supplied attributes
*/ */
public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
short fontNum=getNumberOfFonts(); short fontNum = getNumberOfFonts();
for (short i = 0; i < fontNum; i++) { for (short i = 0; i < fontNum; i++) {
XSSFFont xssfFont = getFontAt(i); XSSFFont xssfFont = getFontAt(i);
@ -471,7 +470,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
} }
public XSSFCellStyle getCellStyleAt(short idx) { public XSSFCellStyle getCellStyleAt(short idx) {
return (XSSFCellStyle)stylesSource.getStyleAt(idx); return stylesSource.getStyleAt(idx);
} }
public Palette getCustomPalette() { public Palette getCustomPalette() {
@ -536,7 +535,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return count of cell styles * @return count of cell styles
*/ */
public short getNumCellStyles() { public short getNumCellStyles() {
return (short) ((StylesTable)stylesSource).getNumCellStyles(); return (short) (stylesSource).getNumCellStyles();
} }
/** /**
@ -545,7 +544,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* @return number of fonts * @return number of fonts
*/ */
public short getNumberOfFonts() { public short getNumberOfFonts() {
return (short)((StylesTable)stylesSource).getNumberOfFonts(); return (short)(stylesSource).getNumberOfFonts();
} }
/** /**
@ -921,11 +920,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* Return a object representing a collection of shared objects used for styling content, * Return a object representing a collection of shared objects used for styling content,
* e.g. fonts, cell styles, colors, etc. * e.g. fonts, cell styles, colors, etc.
*/ */
public StylesSource getStylesSource() { public StylesTable getStylesSource() {
return this.stylesSource; return this.stylesSource;
} }
//TODO do we really need setStylesSource? //TODO do we really need setStylesSource?
protected void setStylesSource(StylesSource stylesSource) { protected void setStylesSource(StylesTable stylesSource) {
this.stylesSource = stylesSource; this.stylesSource = stylesSource;
} }

View File

@ -22,76 +22,96 @@ import java.util.LinkedList;
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.STBorderStyle; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.apache.poi.xssf.usermodel.BorderStyle;
public class XSSFCellBorder { public class XSSFCellBorder {
private CTBorder border;
/**
* Creates a Cell Border from the supplied XML definition
*/
public XSSFCellBorder(CTBorder border) {
this.border = border;
}
/**
* Creates a new, empty Cell Border, on the
* given Styles Table
*/
public XSSFCellBorder() {
border = CTBorder.Factory.newInstance();
}
public static enum BorderSide {
TOP, RIGHT, BOTTOM, LEFT
}
public long putBorder(LinkedList<CTBorder> borders) {
if(borders.contains(border)) {
return borders.indexOf(border);
}
borders.add(border);
return borders.size() - 1;
}
public STBorderStyle.Enum getBorderStyle(BorderSide side) {
return getBorder(side).getStyle();
}
public void setBorderStyle(BorderSide side, STBorderStyle.Enum style) {
getBorder(side).setStyle(style);
}
public XSSFColor getBorderColor(BorderSide side) {
CTBorderPr borderPr = getBorder(side);
if (!borderPr.isSetColor()) {
borderPr.addNewColor();
}
return new XSSFColor(getBorder(side).getColor());
}
public void setBorderColor(BorderSide side, XSSFColor color) { private CTBorder border;
getBorder(side).setColor(color.getCTColor());
} /**
* Creates a Cell Border from the supplied XML definition
private CTBorderPr getBorder(BorderSide side) { */
switch (side) { public XSSFCellBorder(CTBorder border) {
case TOP: { this.border = border;
CTBorderPr borderPr = border.isSetTop() ? border.getTop() : border.addNewTop(); }
return borderPr; /**
} * Creates a new, empty Cell Border, on the
case RIGHT: { * given Styles Table
CTBorderPr borderPr = border.isSetRight() ? border.getRight() : border.addNewRight(); */
return borderPr; public XSSFCellBorder() {
} border = CTBorder.Factory.newInstance();
case BOTTOM:{ }
CTBorderPr borderPr = border.isSetBottom() ? border.getBottom() : border.addNewBottom();
return borderPr; public static enum BorderSide {
} TOP, RIGHT, BOTTOM, LEFT
case LEFT:{ }
CTBorderPr borderPr = border.isSetLeft() ? border.getLeft() : border.addNewLeft();
return borderPr; public CTBorder getCTBorder() {
} return border;
default: throw new IllegalArgumentException("No suitable side specified for the border"); }
}
} public BorderStyle getBorderStyle(BorderSide side) {
CTBorderPr ctBorder = getBorder(side);
STBorderStyle.Enum border = ctBorder == null ? STBorderStyle.NONE : ctBorder.getStyle();
return BorderStyle.values()[border.intValue() - 1];
}
public void setBorderStyle(BorderSide side, BorderStyle style) {
getBorder(side, true).setStyle(STBorderStyle.Enum.forInt(style.ordinal() + 1));
}
public XSSFColor getBorderColor(BorderSide side) {
CTBorderPr borderPr = getBorder(side);
return borderPr != null && borderPr.isSetColor() ?
new XSSFColor(borderPr.getColor()) : null;
}
public void setBorderColor(BorderSide side, XSSFColor color) {
CTBorderPr borderPr = getBorder(side, true);
if(color == null) borderPr.unsetColor();
else borderPr.setColor(color.getCTColor());
}
private CTBorderPr getBorder(BorderSide side) {
return getBorder(side, false);
}
private CTBorderPr getBorder(BorderSide side, boolean ensure) {
CTBorderPr borderPr;
switch (side) {
case TOP:
borderPr = border.getTop();
if(ensure && borderPr == null) borderPr = border.addNewTop();
break;
case RIGHT:
borderPr = border.getRight();
if(ensure && borderPr == null) borderPr = border.addNewRight();
break;
case BOTTOM:
borderPr = border.getBottom();
if(ensure && borderPr == null) borderPr = border.addNewBottom();
break;
case LEFT:
borderPr = border.getLeft();
if(ensure && borderPr == null) borderPr = border.addNewLeft();
break;
default:
throw new IllegalArgumentException("No suitable side specified for the border");
}
return borderPr;
}
public int hashCode(){
return border.toString().hashCode();
}
public boolean equals(Object o){
if(!(o instanceof XSSFCellBorder)) return false;
XSSFCellBorder cf = (XSSFCellBorder)o;
return border.toString().equals(cf.getCTBorder().toString());
}
} }

View File

@ -22,6 +22,7 @@ import org.apache.poi.xssf.usermodel.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
public final class XSSFCellFill { public final class XSSFCellFill {
@ -37,41 +38,54 @@ public final class XSSFCellFill {
} }
public XSSFColor getFillBackgroundColor() { public XSSFColor getFillBackgroundColor() {
CTColor ctColor = getPatternFill().getBgColor(); CTPatternFill ptrn = _fill.getPatternFill();
if (ctColor == null) { if(ptrn == null) return null;
XSSFColor result = new XSSFColor();
result.setIndexed(IndexedColors.AUTOMATIC.getIndex()); CTColor ctColor = ptrn.getBgColor();
return result; return ctColor == null ? null : new XSSFColor(ctColor);
}
return new XSSFColor(ctColor);
} }
public XSSFColor getFillForegroundColor() { public void setFillBackgroundColor(int index) {
CTColor ctColor = getPatternFill().getFgColor(); CTPatternFill ptrn = ensureCTPatternFill();
if (ctColor == null) { CTColor ctColor = ptrn.isSetBgColor() ? ptrn.getBgColor() : ptrn.addNewBgColor();
XSSFColor result = new XSSFColor(); ctColor.setIndexed(index);
result.setIndexed(IndexedColors.AUTOMATIC.getIndex()); }
return result;
} public void setFillBackgroundColor(XSSFColor color) {
return new XSSFColor(ctColor); CTPatternFill ptrn = ensureCTPatternFill();
ptrn.setBgColor(color.getCTColor());
}
public XSSFColor getFillForegroundColor() {
CTPatternFill ptrn = _fill.getPatternFill();
if(ptrn == null) return null;
CTColor ctColor = ptrn.getFgColor();
return ctColor == null ? null : new XSSFColor(ctColor);
}
public void setFillForegroundColor(int index) {
CTPatternFill ptrn = ensureCTPatternFill();
CTColor ctColor = ptrn.isSetFgColor() ? ptrn.getFgColor() : ptrn.addNewFgColor();
ctColor.setIndexed(index);
}
public void setFillForegroundColor(XSSFColor color) {
CTPatternFill ptrn = ensureCTPatternFill();
ptrn.setFgColor(color.getCTColor());
}
public STPatternType.Enum getPatternType() {
CTPatternFill ptrn = _fill.getPatternFill();
return ptrn == null ? null : ptrn.getPatternType();
} }
public Enum getPatternType() { public void setPatternType(STPatternType.Enum patternType) {
return getPatternFill().getPatternType(); CTPatternFill ptrn = ensureCTPatternFill();
} ptrn.setPatternType(patternType);
}
/**
* @return the index of the just added fill
*/
public int putFill(List<CTFill> fills) {
if (fills.contains(_fill)) {
return fills.indexOf(_fill);
}
fills.add(_fill);
return fills.size() - 1;
}
private CTPatternFill getPatternFill() { private CTPatternFill ensureCTPatternFill() {
CTPatternFill patternFill = _fill.getPatternFill(); CTPatternFill patternFill = _fill.getPatternFill();
if (patternFill == null) { if (patternFill == null) {
patternFill = _fill.addNewPatternFill(); patternFill = _fill.addNewPatternFill();
@ -83,27 +97,14 @@ public final class XSSFCellFill {
return _fill; return _fill;
} }
public void setFillBackgroundColor(long index) { public int hashCode(){
CTColor ctColor=getPatternFill().addNewBgColor(); return _fill.toString().hashCode();
ctColor.setIndexed(index); }
_fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) { public boolean equals(Object o){
CTColor ctColor=getPatternFill().addNewFgColor(); if(!(o instanceof XSSFCellFill)) return false;
ctColor.setIndexed(index);
_fill.getPatternFill().setFgColor(ctColor);
}
public void setFillBackgroundRgbColor(XSSFColor color) {
_fill.getPatternFill().setBgColor(color.getCTColor());
}
public void setFillForegroundRgbColor(XSSFColor color) { XSSFCellFill cf = (XSSFCellFill)o;
_fill.getPatternFill().setFgColor(color.getCTColor()); return _fill.toString().equals(cf.getCTFill().toString());
} }
public void setPatternType(Enum patternType) {
getPatternFill().setPatternType(patternType);
}
} }

View File

@ -39,6 +39,11 @@ public class XSSFColor {
this.ctColor = CTColor.Factory.newInstance(); this.ctColor = CTColor.Factory.newInstance();
} }
public XSSFColor(java.awt.Color clr) {
this();
ctColor.setRgb(new byte[]{(byte)clr.getRed(), (byte)clr.getGreen(), (byte)clr.getBlue()});
}
/** /**
* A boolean value indicating the ctColor is automatic and system ctColor dependent. * A boolean value indicating the ctColor is automatic and system ctColor dependent.
*/ */
@ -56,8 +61,8 @@ public class XSSFColor {
/** /**
* Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors. * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
*/ */
public int getIndexed() { public short getIndexed() {
return (int)ctColor.getIndexed(); return (short)ctColor.getIndexed();
} }
/** /**
@ -195,4 +200,16 @@ public class XSSFColor {
public CTColor getCTColor(){ public CTColor getCTColor(){
return ctColor; return ctColor;
} }
public int hashCode(){
return ctColor.toString().hashCode();
}
public boolean equals(Object o){
if(!(o instanceof XSSFColor)) return false;
XSSFColor cf = (XSSFColor)o;
return ctColor.toString().equals(cf.getCTColor().toString());
}
} }

View File

@ -26,12 +26,13 @@ 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.XSSFColor; import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
public class TestXSSFCellStyle extends TestCase { public class TestXSSFCellStyle extends TestCase {
private static final int AUTO_COLOR_INDEX = 64;
private StylesTable stylesTable; private StylesTable stylesTable;
private CTBorder ctBorderA; private CTBorder ctBorderA;
private CTFill ctFill; private CTFill ctFill;
@ -48,14 +49,13 @@ public class TestXSSFCellStyle extends TestCase {
ctStylesheet = stylesTable._getRawStylesheet(); ctStylesheet = stylesTable._getRawStylesheet();
// Until we do XSSFBorder properly, cheat
ctBorderA = CTBorder.Factory.newInstance(); ctBorderA = CTBorder.Factory.newInstance();
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA); XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
long borderId = stylesTable.putBorder(borderA); long borderId = stylesTable.putBorder(borderA);
assertEquals(1, borderId); assertEquals(1, borderId);
XSSFCellBorder borderB = new XSSFCellBorder(); XSSFCellBorder borderB = new XSSFCellBorder();
assertEquals(2, stylesTable.putBorder(borderB)); assertEquals(1, stylesTable.putBorder(borderB));
ctFill = CTFill.Factory.newInstance(); ctFill = CTFill.Factory.newInstance();
XSSFCellFill fill = new XSSFCellFill(ctFill); XSSFCellFill fill = new XSSFCellFill(ctFill);
@ -84,225 +84,430 @@ public class TestXSSFCellStyle extends TestCase {
} }
public void testGetSetBorderBottom() { public void testGetSetBorderBottom() {
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN); //default values
assertEquals((short)1, cellStyle.getBorderBottom()); assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderBottom());
cellStyle.setBorderBottom((short) 2);
assertEquals(STBorderStyle.THIN, ctBorderA.getBottom().getStyle());
cellStyle.setBorderBottomEnum(STBorderStyle.THICK);
assertEquals(6, ctBorderA.getBottom().getStyle().intValue());
}
public void testGetBorderBottomAsString() { int num = stylesTable.getBorders().size();
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN); cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
assertEquals("thin", cellStyle.getBorderBottomAsString()); assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
} //a new border has been added
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getBottom().getStyle());
public void testGetSetBorderRight() { num = stylesTable.getBorders().size();
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM); //setting the same border multiple times should not change borderId
assertEquals((short)2, cellStyle.getBorderRight()); for (int i = 0; i < 3; i++) {
cellStyle.setBorderRight((short) 2); cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
assertEquals(STBorderStyle.THIN, ctBorderA.getRight().getStyle()); assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderBottom());
cellStyle.setBorderRightEnum(STBorderStyle.THICK); }
assertEquals(6, ctBorderA.getRight().getStyle().intValue()); assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
} assertEquals(num, stylesTable.getBorders().size());
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
public void testGetBorderRightAsString() { //setting border to none removes the <bottom> element
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM); cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
assertEquals("medium", cellStyle.getBorderRightAsString()); assertEquals(num, stylesTable.getBorders().size());
} borderId = (int)cellStyle.getCoreXf().getBorderId();
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertFalse(ctBorder.isSetBottom());
}
public void testGetSetBorderRight() {
//default values
assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderRight());
int num = stylesTable.getBorders().size();
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
//a new border has been added
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getRight().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
for (int i = 0; i < 3; i++) {
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderRight());
}
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
assertEquals(num, stylesTable.getBorders().size());
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
//setting border to none removes the <right> element
cellStyle.setBorderRight(CellStyle.BORDER_NONE);
assertEquals(num, stylesTable.getBorders().size());
borderId = (int)cellStyle.getCoreXf().getBorderId();
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertFalse(ctBorder.isSetRight());
}
public void testGetSetBorderLeft() { public void testGetSetBorderLeft() {
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED); //default values
assertEquals((short)3, cellStyle.getBorderLeft()); assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderLeft());
cellStyle.setBorderLeft((short) 2);
assertEquals(STBorderStyle.THIN, ctBorderA.getLeft().getStyle());
cellStyle.setBorderLeftEnum(STBorderStyle.THICK);
assertEquals(6, ctBorderA.getLeft().getStyle().intValue());
}
public void testGetBorderLeftAsString() { int num = stylesTable.getBorders().size();
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED); cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
assertEquals("dashed", cellStyle.getBorderLeftAsString()); assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
//a new border has been added
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getLeft().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
for (int i = 0; i < 3; i++) {
cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderLeft());
}
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
assertEquals(num, stylesTable.getBorders().size());
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
//setting border to none removes the <left> element
cellStyle.setBorderLeft(CellStyle.BORDER_NONE);
assertEquals(num, stylesTable.getBorders().size());
borderId = (int)cellStyle.getCoreXf().getBorderId();
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertFalse(ctBorder.isSetLeft());
} }
public void testGetSetBorderTop() { public void testGetSetBorderTop() {
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR); //default values
assertEquals((short)7, cellStyle.getBorderTop()); assertEquals(CellStyle.BORDER_NONE, cellStyle.getBorderTop());
cellStyle.setBorderTop((short) 2);
assertEquals(STBorderStyle.THIN, ctBorderA.getTop().getStyle());
cellStyle.setBorderTopEnum(STBorderStyle.THICK);
assertEquals(6, ctBorderA.getTop().getStyle().intValue());
}
public void testGetBorderTopAsString() { int num = stylesTable.getBorders().size();
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR); cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
assertEquals("hair", cellStyle.getBorderTopAsString()); assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
//a new border has been added
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
num = stylesTable.getBorders().size();
//setting the same border multiple times should not change borderId
for (int i = 0; i < 3; i++) {
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
assertEquals(CellStyle.BORDER_MEDIUM, cellStyle.getBorderTop());
}
assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
assertEquals(num, stylesTable.getBorders().size());
assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
//setting border to none removes the <top> element
cellStyle.setBorderTop(CellStyle.BORDER_NONE);
assertEquals(num, stylesTable.getBorders().size());
borderId = (int)cellStyle.getCoreXf().getBorderId();
ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertFalse(ctBorder.isSetTop());
} }
public void testGetSetBottomBorderColor() { public void testGetSetBottomBorderColor() {
CTColor ctColor = ctBorderA.addNewBottom().addNewColor(); //defaults
ctColor.setIndexed(2); assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getBottomBorderColor());
assertEquals((short)2, cellStyle.getBottomBorderColor()); assertNull(cellStyle.getBottomBorderRgbColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(4); int num = stylesTable.getBorders().size();
anotherCtColor.setTheme(3);
anotherCtColor.setRgb("1234".getBytes()); XSSFColor clr;
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor); //setting indexed color
assertEquals((short)4, cellStyle.getBottomBorderColor()); cellStyle.setBottomBorderColor(IndexedColors.BLUE_GREY.getIndex());
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb())); assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getBottomBorderColor());
} clr = cellStyle.getBottomBorderRgbColor();
assertTrue(clr.getCTColor().isSetIndexed());
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
//a new border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getBottom().getColor().getIndexed());
//setting XSSFColor
num = stylesTable.getBorders().size();
clr = new XSSFColor(java.awt.Color.CYAN);
cellStyle.setBottomBorderColor(clr);
assertEquals(clr.getCTColor().toString(), cellStyle.getBottomBorderRgbColor().getCTColor().toString());
byte[] rgb = cellStyle.getBottomBorderRgbColor().getRgb();
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
//another border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//passing null unsets the color
cellStyle.setBottomBorderColor(null);
assertNull(cellStyle.getBottomBorderRgbColor());
}
public void testGetSetTopBorderColor() { public void testGetSetTopBorderColor() {
CTColor ctColor = ctBorderA.addNewTop().addNewColor(); //defaults
ctColor.setIndexed(5); assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getTopBorderColor());
assertEquals((short)5, cellStyle.getTopBorderColor()); assertNull(cellStyle.getTopBorderRgbColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(7); int num = stylesTable.getBorders().size();
anotherCtColor.setTheme(3);
anotherCtColor.setRgb("abcd".getBytes()); XSSFColor clr;
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.TOP, anotherColor); //setting indexed color
assertEquals((short)7, cellStyle.getTopBorderColor()); cellStyle.setTopBorderColor(IndexedColors.BLUE_GREY.getIndex());
assertEquals("abcd", new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb())); assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getTopBorderColor());
clr = cellStyle.getTopBorderRgbColor();
assertTrue(clr.getCTColor().isSetIndexed());
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
//a new border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getTop().getColor().getIndexed());
//setting XSSFColor
num = stylesTable.getBorders().size();
clr = new XSSFColor(java.awt.Color.CYAN);
cellStyle.setTopBorderColor(clr);
assertEquals(clr.getCTColor().toString(), cellStyle.getTopBorderRgbColor().getCTColor().toString());
byte[] rgb = cellStyle.getTopBorderRgbColor().getRgb();
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
//another border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//passing null unsets the color
cellStyle.setTopBorderColor(null);
assertNull(cellStyle.getTopBorderRgbColor());
} }
public void testGetSetLeftBorderColor() { public void testGetSetLeftBorderColor() {
CTColor ctColor = ctBorderA.addNewLeft().addNewColor(); //defaults
ctColor.setIndexed(2); assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getLeftBorderColor());
assertEquals((short)2, cellStyle.getLeftBorderColor()); assertNull(cellStyle.getLeftBorderRgbColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(4); int num = stylesTable.getBorders().size();
anotherCtColor.setTheme(3);
anotherCtColor.setRgb("1234".getBytes()); XSSFColor clr;
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.LEFT, anotherColor); //setting indexed color
assertEquals((short)4, cellStyle.getLeftBorderColor()); cellStyle.setLeftBorderColor(IndexedColors.BLUE_GREY.getIndex());
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb())); assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getLeftBorderColor());
clr = cellStyle.getLeftBorderRgbColor();
assertTrue(clr.getCTColor().isSetIndexed());
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
//a new border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getLeft().getColor().getIndexed());
//setting XSSFColor
num = stylesTable.getBorders().size();
clr = new XSSFColor(java.awt.Color.CYAN);
cellStyle.setLeftBorderColor(clr);
assertEquals(clr.getCTColor().toString(), cellStyle.getLeftBorderRgbColor().getCTColor().toString());
byte[] rgb = cellStyle.getLeftBorderRgbColor().getRgb();
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
//another border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//passing null unsets the color
cellStyle.setLeftBorderColor(null);
assertNull(cellStyle.getLeftBorderRgbColor());
} }
public void testGetSetRightBorderColor() { public void testGetSetRightBorderColor() {
CTColor ctColor = ctBorderA.addNewRight().addNewColor(); //defaults
ctColor.setIndexed(8); assertEquals(IndexedColors.BLACK.getIndex(), cellStyle.getRightBorderColor());
assertEquals((short)8, cellStyle.getRightBorderColor()); assertNull(cellStyle.getRightBorderRgbColor());
CTColor anotherCtColor = CTColor.Factory.newInstance();
anotherCtColor.setIndexed(14); int num = stylesTable.getBorders().size();
anotherCtColor.setTheme(3);
anotherCtColor.setRgb("af67".getBytes()); XSSFColor clr;
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor); //setting indexed color
assertEquals((short)14, cellStyle.getRightBorderColor()); cellStyle.setRightBorderColor(IndexedColors.BLUE_GREY.getIndex());
assertEquals("af67", new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb())); assertEquals(IndexedColors.BLUE_GREY.getIndex(), cellStyle.getRightBorderColor());
clr = cellStyle.getRightBorderRgbColor();
assertTrue(clr.getCTColor().isSetIndexed());
assertEquals(IndexedColors.BLUE_GREY.getIndex(), clr.getIndexed());
//a new border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//id of the created border
int borderId = (int)cellStyle.getCoreXf().getBorderId();
assertTrue(borderId > 0);
//check changes in the underlying xml bean
CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
assertEquals(IndexedColors.BLUE_GREY.getIndex(), ctBorder.getRight().getColor().getIndexed());
//setting XSSFColor
num = stylesTable.getBorders().size();
clr = new XSSFColor(java.awt.Color.CYAN);
cellStyle.setRightBorderColor(clr);
assertEquals(clr.getCTColor().toString(), cellStyle.getRightBorderRgbColor().getCTColor().toString());
byte[] rgb = cellStyle.getRightBorderRgbColor().getRgb();
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
//another border was added to the styles table
assertEquals(num + 1, stylesTable.getBorders().size());
//passing null unsets the color
cellStyle.setRightBorderColor(null);
assertNull(cellStyle.getRightBorderRgbColor());
} }
public void testGetFillBackgroundColor() { public void testGetSetFillBackgroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
CTColor ctBgColor = ctPatternFill.addNewBgColor(); assertNull(cellStyle.getFillBackgroundRgbColor());
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
ctPatternFill.setBgColor(ctBgColor);
XSSFCellFill cellFill=new XSSFCellFill(ctFill); XSSFColor clr;
long index=stylesTable.putFill(cellFill);
cellStyle.getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId()); int num = stylesTable.getFills().size();
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
//setting indexed color
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex()); cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed()); assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
clr = cellStyle.getFillBackgroundRgbColor();
//test rgb color - XSSFColor assertTrue(clr.getCTColor().isSetIndexed());
CTColor ctColor=CTColor.Factory.newInstance(); assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
ctColor.setRgb("FFFFFF".getBytes()); //a new fill was added to the styles table
ctPatternFill.setBgColor(ctColor); assertEquals(num + 1, stylesTable.getFills().size());
assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
//id of the created border
cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor)); int fillId = (int)cellStyle.getCoreXf().getFillId();
assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]); assertTrue(fillId > 0);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]); //check changes in the underlying xml bean
assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]); CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]); assertEquals(IndexedColors.RED.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
//setting XSSFColor
num = stylesTable.getFills().size();
clr = new XSSFColor(java.awt.Color.CYAN);
cellStyle.setFillBackgroundColor(clr);
assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
byte[] rgb = cellStyle.getFillBackgroundRgbColor().getRgb();
assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
//another border was added to the styles table
assertEquals(num + 1, stylesTable.getFills().size());
//passing null unsets the color
cellStyle.setFillBackgroundColor(null);
assertNull(cellStyle.getFillBackgroundRgbColor());
assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
} }
public void testGetFillBackgroundColor_default() { public void testDefaultStyles() {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb1 = new XSSFWorkbook();
XSSFCellStyle style = wb.createCellStyle(); XSSFCellStyle style1 = wb1.createCellStyle();
assertEquals(IndexedColors.AUTOMATIC.getIndex(), style1.getFillBackgroundColor());
assertNull(style1.getFillBackgroundRgbColor());
short color; //compatibility with HSSF
try { HSSFWorkbook wb2 = new HSSFWorkbook();
color = style.getFillBackgroundColor(); HSSFCellStyle style2 = wb2.createCellStyle();
} catch (NullPointerException e) { assertEquals(style2.getFillBackgroundColor(), style1.getFillBackgroundColor());
throw new AssertionFailedError("Identified bug 45898"); assertEquals(style2.getFillForegroundColor(), style1.getFillForegroundColor());
} assertEquals(style2.getFillPattern(), style1.getFillPattern());
assertEquals(AUTO_COLOR_INDEX, color);
XSSFColor xcolor=style.getFillBackgroundRgbColor(); assertEquals(style2.getLeftBorderColor(), style1.getLeftBorderColor());
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX); assertEquals(style2.getTopBorderColor(), style1.getTopBorderColor());
assertEquals(style2.getRightBorderColor(), style1.getRightBorderColor());
assertEquals(style2.getBottomBorderColor(), style1.getBottomBorderColor());
assertEquals(style2.getBorderBottom(), style1.getBorderBottom());
assertEquals(style2.getBorderLeft(), style1.getBorderLeft());
assertEquals(style2.getBorderRight(), style1.getBorderRight());
assertEquals(style2.getBorderTop(), style1.getBorderTop());
} }
public void testGetFillForegroundColor() { public void testGetFillForegroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); XSSFWorkbook wb = new XSSFWorkbook();
CTColor ctFgColor = ctPatternFill.addNewFgColor(); StylesTable styles = wb.getStylesSource();
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex()); assertEquals(1, wb.getNumCellStyles());
ctPatternFill.setFgColor(ctFgColor); assertEquals(2, styles.getFills().size());
XSSFCellFill cellFill=new XSSFCellFill(ctFill); XSSFCellStyle defaultStyle = wb.getCellStyleAt((short)0);
long index=stylesTable.putFill(cellFill); assertEquals(IndexedColors.AUTOMATIC.getIndex(), defaultStyle.getFillForegroundColor());
cellStyle.getCoreXf().setFillId(index); assertEquals(null, defaultStyle.getFillForegroundRgbColor());
assertEquals(CellStyle.NO_FILL, defaultStyle.getFillPattern());
assertEquals(2,cellStyle.getCoreXf().getFillId()); XSSFCellStyle customStyle = wb.createCellStyle();
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
customStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex()); assertEquals(CellStyle.SOLID_FOREGROUND, customStyle.getFillPattern());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed()); assertEquals(3, styles.getFills().size());
//test rgb color - XSSFColor customStyle.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
CTColor ctColor=CTColor.Factory.newInstance(); assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), customStyle.getFillForegroundColor());
ctColor.setRgb("FFFFFF".getBytes()); assertEquals(4, styles.getFills().size());
ctPatternFill.setFgColor(ctColor);
assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString()); for (int i = 0; i < 3; i++) {
XSSFCellStyle style = wb.createCellStyle();
cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]); style.setFillPattern(CellStyle.SOLID_FOREGROUND);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]); assertEquals(CellStyle.SOLID_FOREGROUND, style.getFillPattern());
assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]); assertEquals(4, styles.getFills().size());
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), style.getFillForegroundColor());
assertEquals(4, styles.getFills().size());
}
} }
public void testGetFillForegroundColor_default() {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle style = wb.createCellStyle();
short color;
try {
color = style.getFillForegroundColor();
} catch (NullPointerException e) {
throw new AssertionFailedError("Identified bug 45898");
}
assertEquals(AUTO_COLOR_INDEX, color);
XSSFColor xcolor=style.getFillForegroundRgbColor();
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
}
public void testGetFillPattern() { public void testGetFillPattern() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
XSSFCellFill cellFill=new XSSFCellFill(ctFill); int num = stylesTable.getFills().size();
long index=stylesTable.putFill(cellFill); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.getCoreXf().setFillId(index); assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern());
assertEquals(num + 1, stylesTable.getFills().size());
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern()); int fillId = (int)cellStyle.getCoreXf().getFillId();
assertTrue(fillId > 0);
cellStyle.setFillPattern(CellStyle.BRICKS); //check changes in the underlying xml bean
assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue()); CTFill ctFill = stylesTable.getFillAt(fillId).getCTFill();
assertEquals(STPatternType.SOLID, ctFill.getPatternFill().getPatternType());
//setting the same fill multiple time does not update the styles table
for (int i = 0; i < 3; i++) {
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
}
assertEquals(num + 1, stylesTable.getFills().size());
cellStyle.setFillPattern(CellStyle.NO_FILL);
assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
fillId = (int)cellStyle.getCoreXf().getFillId();
ctFill = stylesTable.getFillAt(fillId).getCTFill();
assertFalse(ctFill.getPatternFill().isSetPatternType());
} }
public void testGetFont() { public void testGetFont() {

View File

@ -255,9 +255,11 @@ public final class TestXSSFFont extends TestCase{
s1 = wb.getSheetAt(0); s1 = wb.getSheetAt(0);
assertEquals(2, wb.getNumberOfFonts()); assertEquals(2, wb.getNumberOfFonts());
assertNotNull(s1.getRow(0).getCell(0).getCellStyle().getFont(wb)); short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
assertEquals(IndexedColors.YELLOW.getIndex(), s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getColor()); Font fnt = wb.getFontAt(idx);
assertEquals("Courier", s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getFontName()); assertNotNull(fnt);
assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
assertEquals("Courier", fnt.getFontName());
// Now add an orphaned one // Now add an orphaned one
XSSFFont font2 = wb.createFont(); XSSFFont font2 = wb.createFont();
@ -342,4 +344,92 @@ public final class TestXSSFFont extends TestCase{
XSSFTestDataSamples.writeOutAndReadBack(workbook); XSSFTestDataSamples.writeOutAndReadBack(workbook);
} }
/**
* Test that fonts get added properly
*
* @see org.apache.poi.hssf.usermodel.TestBugs#test45338()
*/
public void test45338() {
XSSFWorkbook wb = new XSSFWorkbook();
assertEquals(1, wb.getNumberOfFonts());
XSSFSheet s = wb.createSheet();
s.createRow(0);
s.createRow(1);
XSSFCell c1 = s.getRow(0).createCell(0);
XSSFCell c2 = s.getRow(1).createCell(0);
assertEquals(1, wb.getNumberOfFonts());
XSSFFont f1 = wb.getFontAt((short)0);
assertEquals(XSSFFont.BOLDWEIGHT_NORMAL, f1.getBoldweight());
// Check that asking for the same font
// multiple times gives you the same thing.
// Otherwise, our tests wouldn't work!
assertEquals(
wb.getFontAt((short)0),
wb.getFontAt((short)0)
);
// Look for a new font we have
// yet to add
assertNull(
wb.findFont(
(short)11, (short)123, (short)22,
"Thingy", false, true, (short)2, (byte)2
)
);
XSSFFont nf = wb.createFont();
assertEquals(2, wb.getNumberOfFonts());
assertEquals(1, nf.getIndex());
assertEquals(nf, wb.getFontAt((short)1));
nf.setBoldweight((short)11);
nf.setColor((short)123);
nf.setFontHeight((short)22);
nf.setFontName("Thingy");
nf.setItalic(false);
nf.setStrikeout(true);
nf.setTypeOffset((short)2);
nf.setUnderline((byte)2);
assertEquals(2, wb.getNumberOfFonts());
assertEquals(nf, wb.getFontAt((short)1));
assertEquals(
wb.getFontAt((short)1),
wb.getFontAt((short)1)
);
assertTrue(
wb.getFontAt((short)0)
!=
wb.getFontAt((short)1)
);
// Find it now
assertNotNull(
wb.findFont(
(short)11, (short)123, (short)22,
"Thingy", false, true, (short)2, (byte)2
)
);
assertEquals(
1,
wb.findFont(
(short)11, (short)123, (short)22,
"Thingy", false, true, (short)2, (byte)2
).getIndex()
);
assertEquals(nf,
wb.findFont(
(short)11, (short)123, (short)22,
"Thingy", false, true, (short)2, (byte)2
)
);
}
} }

View File

@ -256,7 +256,7 @@ public final class TestXSSFWorkbook extends TestCase {
Font font=workbook.createFont(); Font font=workbook.createFont();
((XSSFFont)font).setBold(true); ((XSSFFont)font).setBold(true);
font.setUnderline(Font.U_DOUBLE); font.setUnderline(Font.U_DOUBLE);
StylesSource styleSource=new StylesTable(); StylesTable styleSource=new StylesTable();
long index=styleSource.putFont(font); long index=styleSource.putFont(font);
System.out.println("index="+index); System.out.println("index="+index);
workbook.setStylesSource(styleSource); workbook.setStylesSource(styleSource);

View File

@ -17,14 +17,15 @@
package org.apache.poi.xssf.usermodel.extensions; package org.apache.poi.xssf.usermodel.extensions;
import junit.framework.TestCase;
import org.apache.poi.xssf.usermodel.BorderStyle;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
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.CTStylesheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import junit.framework.TestCase;
public class TestXSSFBorder extends TestCase { public class TestXSSFBorder extends TestCase {
@ -34,15 +35,20 @@ public class TestXSSFBorder extends TestCase {
CTBorderPr top = border.addNewTop(); CTBorderPr top = border.addNewTop();
CTBorderPr right = border.addNewRight(); CTBorderPr right = border.addNewRight();
CTBorderPr bottom = border.addNewBottom(); CTBorderPr bottom = border.addNewBottom();
top.setStyle(STBorderStyle.DASH_DOT); top.setStyle(STBorderStyle.DASH_DOT);
right.setStyle(STBorderStyle.NONE); right.setStyle(STBorderStyle.NONE);
bottom.setStyle(STBorderStyle.THIN); bottom.setStyle(STBorderStyle.THIN);
XSSFCellBorder cellBorderStyle = new XSSFCellBorder(border); XSSFCellBorder cellBorderStyle = new XSSFCellBorder(border);
assertEquals("dashDot", cellBorderStyle.getBorderStyle(BorderSide.TOP).toString()); assertEquals("DASH_DOT", cellBorderStyle.getBorderStyle(BorderSide.TOP).toString());
assertEquals("none", cellBorderStyle.getBorderStyle(BorderSide.RIGHT).toString());
assertEquals(1, cellBorderStyle.getBorderStyle(BorderSide.RIGHT).intValue()); assertEquals("NONE", cellBorderStyle.getBorderStyle(BorderSide.RIGHT).toString());
assertEquals("thin", cellBorderStyle.getBorderStyle(BorderSide.BOTTOM).toString()); assertEquals(BorderStyle.NONE.ordinal(), cellBorderStyle.getBorderStyle(BorderSide.RIGHT).ordinal());
assertEquals(2, cellBorderStyle.getBorderStyle(BorderSide.BOTTOM).intValue());
assertEquals("THIN", cellBorderStyle.getBorderStyle(BorderSide.BOTTOM).toString());
assertEquals(BorderStyle.THIN.ordinal(), cellBorderStyle.getBorderStyle(BorderSide.BOTTOM).ordinal());
} }
} }

View File

@ -1,58 +1,67 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership. this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0 The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
==================================================================== */ ==================================================================== */
package org.apache.poi.xssf.usermodel.extensions; package org.apache.poi.xssf.usermodel.extensions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor; import org.apache.poi.xssf.usermodel.FillPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import junit.framework.TestCase;
import junit.framework.TestCase;
public class TestXSSFCellFill extends TestCase {
public class TestXSSFCellFill extends TestCase {
public void testGetFillBackgroundColor() {
CTFill ctFill = CTFill.Factory.newInstance(); public void testGetFillBackgroundColor() {
XSSFCellFill cellFill = new XSSFCellFill(ctFill); CTFill ctFill = CTFill.Factory.newInstance();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); XSSFCellFill cellFill = new XSSFCellFill(ctFill);
CTColor bgColor = ctPatternFill.addNewBgColor(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
assertNotNull(cellFill.getFillBackgroundColor()); CTColor bgColor = ctPatternFill.addNewBgColor();
bgColor.setIndexed(2); assertNotNull(cellFill.getFillBackgroundColor());
assertEquals(2, cellFill.getFillBackgroundColor().getIndexed()); bgColor.setIndexed(2);
} assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());
}
public void testGetFillForegroundColor() {
CTFill ctFill = CTFill.Factory.newInstance(); public void testGetFillForegroundColor() {
XSSFCellFill cellFill = new XSSFCellFill(ctFill); CTFill ctFill = CTFill.Factory.newInstance();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); XSSFCellFill cellFill = new XSSFCellFill(ctFill);
CTColor fgColor = ctPatternFill.addNewFgColor(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
assertNotNull(cellFill.getFillForegroundColor()); CTColor fgColor = ctPatternFill.addNewFgColor();
fgColor.setIndexed(8); assertNotNull(cellFill.getFillForegroundColor());
assertEquals(8, cellFill.getFillForegroundColor().getIndexed()); fgColor.setIndexed(8);
} assertEquals(8, cellFill.getFillForegroundColor().getIndexed());
}
public void testGetPatternType() {
CTFill ctFill = CTFill.Factory.newInstance(); public void testGetSetPatternType() {
XSSFCellFill cellFill = new XSSFCellFill(ctFill); CTFill ctFill = CTFill.Factory.newInstance();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); XSSFCellFill cellFill = new XSSFCellFill(ctFill);
ctPatternFill.setPatternType(STPatternType.DARK_DOWN); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
assertEquals(8, cellFill.getPatternType().intValue()); ctPatternFill.setPatternType(STPatternType.SOLID);
} //assertEquals(FillPatternType.SOLID_FOREGROUND.ordinal(), cellFill.getPatternType().ordinal());
} }
public void testGetNotModifies() {
CTFill ctFill = CTFill.Factory.newInstance();
XSSFCellFill cellFill = new XSSFCellFill(ctFill);
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
assertEquals(8, cellFill.getPatternType().intValue());
}
}