patch #45881 from Sourcesense: implemented cell fills and borders, added examples

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@698674 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-09-24 17:53:29 +00:00
parent b0033606e4
commit 2c8ea83f9d
6 changed files with 491 additions and 254 deletions

View File

@ -16,13 +16,17 @@
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
/**
* Fills and Colors
*/
@ -36,7 +40,7 @@ public class FillsAndColors {
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("X"));
@ -44,7 +48,7 @@ public class FillsAndColors {
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue(new XSSFRichTextString("X"));

View File

@ -373,12 +373,12 @@ public class StylesTable implements StylesSource, XSSFModel {
return fill.putFill(fills);
}
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
}
private void initialize() {
//CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont();
fonts.add(xssfFont.getCTFont());
@ -423,24 +423,14 @@ public class StylesTable implements StylesSource, XSSFModel {
}
private XSSFFont createDefaultFont() {
/*
CTFont ctFont = CTFont.Factory.newInstance();
ctFont.addNewSz().setVal(11);
ctFont.addNewColor().setTheme(1);
ctFont.addNewName().setVal("Calibri");
ctFont.addNewFamily().setVal(2);
ctFont.addNewScheme().setVal(STFontScheme.MINOR);
XSSFFont font=new XSSFFont(ctFont);
return font;
*/
CTFont ctFont = CTFont.Factory.newInstance();
XSSFFont xssfFont=new XSSFFont(ctFont);
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
xssfFont.setFamily(FontFamily.SWISS);
xssfFont.setScheme(FontScheme.MINOR);
return xssfFont;
CTFont ctFont = CTFont.Factory.newInstance();
XSSFFont xssfFont=new XSSFFont(ctFont);
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
xssfFont.setFamily(FontFamily.SWISS);
xssfFont.setScheme(FontScheme.MINOR);
return xssfFont;
}
@ -453,8 +443,8 @@ public class StylesTable implements StylesSource, XSSFModel {
public long putDxf(CTDxf dxf) {
this.dxfs.add(dxf);
return this.dxfs.size();
this.dxfs.add(dxf);
return this.dxfs.size();
}

View File

@ -17,7 +17,10 @@
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
@ -28,6 +31,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
public class XSSFCellStyle implements CellStyle {
@ -74,7 +78,6 @@ public class XSSFCellStyle implements CellStyle {
*/
public XSSFCellStyle(StylesSource stylesSource) {
this.stylesSource = stylesSource;
// We need a new CTXf for the main styles
// TODO decide on a style ctxf
cellXf = CTXf.Factory.newInstance();
@ -173,12 +176,65 @@ public class XSSFCellStyle implements CellStyle {
return (short) getCellFill().getFillBackgroundColor().getIndexed();
}
public XSSFColor getFillBackgroundRgbColor() {
return getCellFill().getFillBackgroundColor();
}
public short getFillForegroundColor() {
return (short) getCellFill().getFillForegroundColor().getIndexed();
}
public XSSFColor getFillForegroundRgbColor() {
return getCellFill().getFillForegroundColor();
}
public short getFillPattern() {
return (short) getCellFill().getPatternType().intValue();
int fp= getCellFill().getPatternType().intValue();
switch (fp) {
case STPatternType.INT_NONE:
return CellStyle.NO_FILL;
case STPatternType.INT_SOLID:
return CellStyle.SOLID_FOREGROUND;
case STPatternType.INT_LIGHT_GRAY:
return CellStyle.FINE_DOTS;
case STPatternType.INT_DARK_GRID:
return CellStyle.ALT_BARS;
case STPatternType.INT_DARK_GRAY:
return CellStyle.SPARSE_DOTS;
case STPatternType.INT_DARK_HORIZONTAL:
return CellStyle.THICK_HORZ_BANDS;
case STPatternType.INT_DARK_VERTICAL:
return CellStyle.THICK_VERT_BANDS;
case STPatternType.INT_DARK_UP:
return CellStyle.THICK_BACKWARD_DIAG;
case STPatternType.INT_DARK_DOWN:
return CellStyle.THICK_FORWARD_DIAG;
case STPatternType.INT_GRAY_0625:
return CellStyle.BIG_SPOTS;
case STPatternType.INT_DARK_TRELLIS:
return CellStyle.BRICKS;
case STPatternType.INT_LIGHT_HORIZONTAL:
return CellStyle.THIN_HORZ_BANDS;
case STPatternType.INT_LIGHT_VERTICAL:
return CellStyle.THIN_VERT_BANDS;
case STPatternType.INT_LIGHT_UP:
return CellStyle.THIN_BACKWARD_DIAG;
case STPatternType.INT_LIGHT_DOWN:
return CellStyle.THIN_FORWARD_DIAG;
case STPatternType.INT_LIGHT_GRID:
return CellStyle.SQUARES;
case STPatternType.INT_LIGHT_TRELLIS:
return CellStyle.DIAMONDS;
case STPatternType.INT_GRAY_125:
return CellStyle.LESS_DOTS;
/*
case STPatternType.INT_GRAY_0625:
return CellStyle.LEAST_DOTS;
*/
default:
return CellStyle.NO_FILL;
}
// return (short) getCellFill().getPatternType().intValue();
}
public Font getFont(Workbook parentWorkbook) {
@ -288,17 +344,85 @@ public class XSSFCellStyle implements CellStyle {
cellXf.setNumFmtId((long)fmt);
}
public void setFillBackgroundRgbColor(XSSFColor color) {
cellFill=getCellFill();
cellFill.setFillBackgroundRgbColor(color);
}
public void setFillBackgroundColor(short bg) {
getCellFill().setFillBackgroundColor(bg);
}
public void setFillForegroundRgbColor(XSSFColor color) {
getCellFill().setFillForegroundRgbColor(color);
}
public void setFillForegroundColor(short bg) {
getCellFill().setFillForegroundColor(bg);
getCellFill().setFillForegroundColor(bg);
}
public void setFillPattern(short fp) {
// TODO Auto-generated method stub
cellFill=getCellFill();
switch (fp) {
case CellStyle.NO_FILL:
cellFill.setPatternType(STPatternType.NONE);
break;
case CellStyle.SOLID_FOREGROUND:
cellFill.setPatternType(STPatternType.SOLID);
break;
case CellStyle.FINE_DOTS:
cellFill.setPatternType(STPatternType.LIGHT_GRAY);
break;
case CellStyle.ALT_BARS:
cellFill.setPatternType(STPatternType.DARK_GRID);
break;
case CellStyle.SPARSE_DOTS:
cellFill.setPatternType(STPatternType.DARK_GRAY);
break;
case CellStyle.THICK_HORZ_BANDS:
cellFill.setPatternType(STPatternType.DARK_HORIZONTAL);
break;
case CellStyle.THICK_VERT_BANDS:
cellFill.setPatternType(STPatternType.DARK_VERTICAL);
break;
case CellStyle.THICK_BACKWARD_DIAG:
cellFill.setPatternType(STPatternType.DARK_UP);
break;
case CellStyle.THICK_FORWARD_DIAG:
cellFill.setPatternType(STPatternType.DARK_DOWN);
break;
case CellStyle.BIG_SPOTS:
cellFill.setPatternType(STPatternType.GRAY_0625);
break;
case CellStyle.BRICKS:
cellFill.setPatternType(STPatternType.DARK_TRELLIS);
break;
case CellStyle.THIN_HORZ_BANDS:
cellFill.setPatternType(STPatternType.LIGHT_HORIZONTAL);
break;
case CellStyle.THIN_VERT_BANDS:
cellFill.setPatternType(STPatternType.LIGHT_VERTICAL);
break;
case CellStyle.THIN_BACKWARD_DIAG:
cellFill.setPatternType(STPatternType.LIGHT_UP);
break;
case CellStyle.THIN_FORWARD_DIAG:
cellFill.setPatternType(STPatternType.LIGHT_DOWN);
break;
case CellStyle.SQUARES:
cellFill.setPatternType(STPatternType.LIGHT_GRID);
break;
case CellStyle.DIAMONDS:
cellFill.setPatternType(STPatternType.LIGHT_TRELLIS);
break;
case CellStyle.LESS_DOTS:
cellFill.setPatternType(STPatternType.GRAY_125);
break;
case CellStyle.LEAST_DOTS:
cellFill.setPatternType(STPatternType.GRAY_0625);
break;
default: throw new RuntimeException("Fill type ["+fp+"] not accepted");
}
}
public void setFont(Font font) {
@ -306,6 +430,7 @@ public class XSSFCellStyle implements CellStyle {
long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index);
}
this.cellXf.setApplyFont(true);
}
public void setHidden(boolean hidden) {
@ -320,10 +445,17 @@ public class XSSFCellStyle implements CellStyle {
setBorderColorIndexed(BorderSide.LEFT, color);
}
private void setBorderColorIndexed(BorderSide side, XSSFColor color) {
this.cellBorder.setBorderColor(side, color);
}
public void setLocked(boolean locked) {
getCellProtection().setLocked(locked);
}
public void setRightBorderColor(short color) {
setBorderColorIndexed(BorderSide.RIGHT, color);
}
@ -359,30 +491,50 @@ public class XSSFCellStyle implements CellStyle {
private XSSFCellBorder getCellBorder() {
if (cellBorder == null) {
// TODO make a common Cell Border object
cellBorder = ((StylesTable)stylesSource).getBorderAt(getBorderId());
int borderId=getBorderId();
if(borderId==-1){
cellBorder=new XSSFCellBorder();
long index=((StylesTable)stylesSource).putBorder(cellBorder);
this.cellXf.setBorderId(index);
this.cellXf.setApplyBorder(true);
}
else{
cellBorder = ((StylesTable)stylesSource).getBorderAt(borderId);
}
}
return cellBorder;
}
private int getBorderId() {
if (cellXf.isSetBorderId()) {
if (cellXf.isSetBorderId() && cellXf.getBorderId()>0) {
return (int) cellXf.getBorderId();
}
return (int) cellStyleXf.getBorderId();
return -1;
// return (int) cellStyleXf.getBorderId();
}
private XSSFCellFill getCellFill() {
if (cellFill == null) {
cellFill = ((StylesTable)stylesSource).getFillAt(getFillId());
}
return cellFill;
if (cellFill == null) {
int fillId=getFillId();
if(fillId == -1) {
cellFill=new XSSFCellFill();
long index=((StylesTable)stylesSource).putFill(cellFill);
this.cellXf.setFillId(index);
this.cellXf.setApplyFill(true);
}
else{
cellFill=((StylesTable)stylesSource).getFillAt(fillId);
}
}
return cellFill;
}
private int getFillId() {
if (cellXf.isSetFillId()) {
if (cellXf.isSetFillId() && cellXf.getFillId()>0) {
return (int) cellXf.getFillId();
}
return (int) cellStyleXf.getFillId();
//return (int) cellStyleXf.getFillId();
return -1;
}
private int getFontId() {

View File

@ -18,8 +18,8 @@
package org.apache.poi.xssf.usermodel;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
@ -31,7 +31,6 @@ import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CommentsSource;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Palette;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Row;
@ -68,6 +67,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument;
@ -250,16 +250,33 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null;
}
public XSSFCellStyle createCellStyle() {
CTXf xf=CTXf.Factory.newInstance();
xf.setNumFmtId(0);
xf.setFontId(0);
xf.setFillId(0);
xf.setBorderId(0);
xf.setXfId(0);
int xfSize=((StylesTable)stylesSource)._getStyleXfsSize();
long indexXf=((StylesTable)stylesSource).putCellXf(xf);
XSSFCellStyle style = new XSSFCellStyle(new Long(indexXf-1).intValue(), xfSize-1, (StylesTable)stylesSource);
return style;
}
/*
public XSSFCellStyle createCellStyle() {
return new XSSFCellStyle(stylesSource);
}
*/
public DataFormat createDataFormat() {
return getCreationHelper().createDataFormat();
}
public XSSFFont createFont() {
return new XSSFFont();
XSSFFont font= new XSSFFont();
stylesSource.putFont(font);
return font;
}
public XSSFName createName() {
@ -308,7 +325,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
short fontNum=getNumberOfFonts();
for (short i = 0; i < fontNum; i++) {
XSSFFont xssfFont = getFontAt(i);
if ( xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)
if ( (xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD))
&& xssfFont.getColor() == color
&& xssfFont.getFontHeightInPoints() == fontHeight
&& xssfFont.getFontName().equals(name)
@ -413,7 +431,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public short getNumberOfFonts() {
// TODO Auto-generated method stub
return (short)((StylesTable)stylesSource).getNumberOfFonts();
}

View File

@ -66,15 +66,30 @@ public class XSSFCellFill {
public CTFill getCTFill() {
return this.fill;
}
public void setFillBackgroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewBgColor();
CTColor ctColor=getPatternFill().addNewBgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewFgColor();
CTColor ctColor=getPatternFill().addNewFgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setFgColor(ctColor);
}
public void setFillBackgroundRgbColor(XSSFColor color) {
fill.getPatternFill().setBgColor(color.getCTColor());
}
public void setFillForegroundRgbColor(XSSFColor color) {
fill.getPatternFill().setFgColor(color.getCTColor());
}
public void setPatternType(Enum patternType) {
getPatternFill().setPatternType(patternType);
}
}

View File

@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
@ -56,7 +57,7 @@ public class TestXSSFCellStyle extends TestCase {
ctFill = CTFill.Factory.newInstance();
XSSFCellFill fill = new XSSFCellFill(ctFill);
long fillId = stylesTable.putFill(fill);
assertEquals(1, fillId);
assertEquals(2, fillId);
ctFont = CTFont.Factory.newInstance();
XSSFFont font = new XSSFFont(ctFont);
@ -67,11 +68,15 @@ public class TestXSSFCellStyle extends TestCase {
cellStyleXf.setBorderId(1);
cellStyleXf.setFillId(1);
cellStyleXf.setFontId(1);
cellXfs = ctStylesheet.addNewCellXfs();
cellXf = cellXfs.addNewXf();
cellXf.setXfId(1);
cellXf.setBorderId(1);
cellXf.setFillId(1);
cellXf.setFontId(1);
stylesTable.putCellStyleXf(cellStyleXf);
stylesTable.putCellXf(cellXf);
long id=stylesTable.putCellXf(cellXf);
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
}
@ -192,23 +197,77 @@ public class TestXSSFCellStyle extends TestCase {
}
public void testGetFillBackgroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(4);
assertEquals(4, cellStyle.getFillBackgroundColor());
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
ctPatternFill.setBgColor(ctBgColor);
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
//test rgb color - XSSFColor
CTColor ctColor=CTColor.Factory.newInstance();
ctColor.setRgb("FFFFFF".getBytes());
ctPatternFill.setBgColor(ctColor);
assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
}
public void testGetFillForegroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5);
assertEquals(5, cellStyle.getFillForegroundColor());
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
ctPatternFill.setFgColor(ctFgColor);
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(2,cellStyle.getCoreXf().getFillId());
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
//test rgb color - XSSFColor
CTColor ctColor=CTColor.Factory.newInstance();
ctColor.setRgb("FFFFFF".getBytes());
ctPatternFill.setFgColor(ctColor);
assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
}
public void testGetFillPattern() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
assertEquals(8, cellStyle.getFillPattern());
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
long index=stylesTable.putFill(cellFill);
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
cellStyle.setFillPattern(CellStyle.BRICKS);
assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
}
public void testGetFont() {