more ooxml progress from SourseSense developers

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@694288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-09-11 15:11:57 +00:00
parent 5aae8b64f2
commit 6d1b247b69
17 changed files with 1441 additions and 115 deletions

View File

@ -38,6 +38,8 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorders;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellStyleXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDxfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFills;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
@ -46,12 +48,10 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmt;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTNumFmts;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
/**
* Table of styles shared across all sheets in a workbook.
*
@ -65,6 +65,8 @@ public class StylesTable implements StylesSource, XSSFModel {
private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
/**
* The first style id available for use as a custom style
*/
@ -101,7 +103,6 @@ public class StylesTable implements StylesSource, XSSFModel {
public void readFrom(InputStream is) throws IOException {
try {
doc = StyleSheetDocument.Factory.parse(is);
// Grab all the different bits we care about
if(doc.getStyleSheet().getNumFmts() != null)
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
@ -127,6 +128,12 @@ public class StylesTable implements StylesSource, XSSFModel {
for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
styleXfs.add(xf);
}
// dxf
if(doc.getStyleSheet().getDxfs() != null)
for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
dxfs.add(dxf);
}
} catch (XmlException e) {
throw new IOException(e.getLocalizedMessage());
}
@ -163,6 +170,7 @@ public class StylesTable implements StylesSource, XSSFModel {
public Font getFontAt(long idx) {
return new XSSFFont(fonts.get((int) idx));
}
public synchronized long putFont(Font font) {
return putFont((XSSFFont)font, fonts);
}
@ -216,7 +224,14 @@ public class StylesTable implements StylesSource, XSSFModel {
styleXfs.add(cellStyleXf);
return styleXfs.size();
}
/**
/**
* get the size of cell styles
*/
public int getNumCellStyles(){
return styleXfs.size();
}
/**
* For unit testing only
*/
public int _getNumberFormatSize() {
@ -329,6 +344,17 @@ public class StylesTable implements StylesSource, XSSFModel {
doc.getStyleSheet().setCellStyleXfs(ctSXfs);
}
// Style dxf
if(dxfs.size() > 0) {
CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
ctDxfs.setCount(dxfs.size());
ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
);
doc.getStyleSheet().setDxfs(ctDxfs);
}
// Save
doc.save(out, options);
}
@ -345,8 +371,9 @@ public class StylesTable implements StylesSource, XSSFModel {
return font.putFont(fonts);
}
private void initialize() {
CTFont ctFont = createDefaultFont();
fonts.add(ctFont);
//CTFont ctFont = createDefaultFont();
XSSFFont xssfFont = createDefaultFont();
fonts.add(xssfFont.getCTFont());
CTFill ctFill = createDefaultFill();
fills.add(ctFill);
@ -360,6 +387,7 @@ public class StylesTable implements StylesSource, XSSFModel {
xf.setXfId(0);
xfs.add(xf);
}
private CTXf createDefaultXf() {
CTXf ctXf = CTXf.Factory.newInstance();
ctXf.setNumFmtId(0);
@ -377,18 +405,50 @@ public class StylesTable implements StylesSource, XSSFModel {
ctBorder.addNewDiagonal();
return ctBorder;
}
private CTFill createDefaultFill() {
CTFill ctFill = CTFill.Factory.newInstance();
ctFill.addNewPatternFill().setPatternType(STPatternType.NONE);
return ctFill;
}
private CTFont createDefaultFont() {
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);
return ctFont;
XSSFFont font=new XSSFFont(ctFont);
return font;
*/
XSSFFont xssfFont=new XSSFFont();
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
xssfFont.setFamily(XSSFFont.FONT_FAMILY_SWISS);
xssfFont.setScheme(XSSFFont.SCHEME_MINOR);
return xssfFont;
}
public CTDxf getDxf(long idx) {
if(dxfs.size()==0)
return CTDxf.Factory.newInstance();
else
return dxfs.get((int) idx);
}
public long putDxf(CTDxf dxf) {
this.dxfs.add(dxf);
return this.dxfs.size();
}
}

View File

@ -293,13 +293,11 @@ public class XSSFCellStyle implements CellStyle {
}
public void setFillBackgroundColor(short bg) {
// TODO Auto-generated method stub
getCellFill().setFillBackgroundColor(bg);
}
public void setFillForegroundColor(short bg) {
// TODO Auto-generated method stub
getCellFill().setFillForegroundColor(bg);
}
public void setFillPattern(short fp) {
@ -308,8 +306,10 @@ public class XSSFCellStyle implements CellStyle {
}
public void setFont(Font font) {
// TODO Auto-generated method stub
if(font!=null){
long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index);
}
}
public void setHidden(boolean hidden) {

View File

@ -19,126 +19,400 @@ package org.apache.poi.xssf.usermodel;
import java.util.LinkedList;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.util.CTFontWrapper;
import org.apache.poi.xssf.util.Charset;
import org.apache.poi.xssf.util.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
public class XSSFFont implements Font {
private CTFont font;
public static final int SCHEME_MAJOR=2;
public static final int SCHEME_MINOR=3;
public static final int SCHEME_NONE=0;
public static final int FONT_FAMILY_NOT_APPLICABLE=0;
public static final int FONT_FAMILY_ROMAN=1;
public static final int FONT_FAMILY_SWISS=2;
public static final int FONT_FAMILY_MODERN=3;
public static final int FONT_FAMILY_SCRIPT=4;
public static final int FONT_FAMILY_DECORATIVE=5;
public static final String DEFAULT_FONT_NAME="Calibri";
public static final short DEFAULT_FONT_SIZE=11;
public static final short DEFAULT_FONT_COLOR=(short)IndexedColors.BLACK;
private int index=0;
private CTFontWrapper fontWrapper;
public XSSFFont(CTFont font) {
this.font = font;
this.fontWrapper=new CTFontWrapper(font);
}
/*
public XSSFFont(int index) {
this.fontWrapper=new CTFontWrapper(font);
this.index=index;
}
*/
public XSSFFont() {
this.font = CTFont.Factory.newInstance();
this.fontWrapper = new CTFontWrapper(CTFont.Factory.newInstance());
}
public CTFont getCTFont(){
return fontWrapper.getCTFont();
}
public short getBoldweight() {
// TODO Auto-generated method stub
return 0;
}
CTBooleanProperty bold=fontWrapper.getB();
if(bold!=null && bold.getVal())
return Font.BOLDWEIGHT_BOLD;
else
return Font.BOLDWEIGHT_NORMAL;
}
public byte getCharSet() {
// TODO Auto-generated method stub
return 0;
CTIntProperty charset= fontWrapper.getCharset();
if(charset!=null){
//this value must be set -- can't be null
switch (charset.getVal()) {
case Charset.ANSI_CHARSET:
return Font.ANSI_CHARSET;
case Charset.DEFAULT_CHARSET:
return Font.DEFAULT_CHARSET;
case Charset.SYMBOL_CHARSET:
return Font.SYMBOL_CHARSET;
default://maight be correct to return this byte value???
return Byte.parseByte(Integer.toString(charset.getVal()));
}
}
else
return Font.ANSI_CHARSET;
}
public short getColor() {
// TODO Auto-generated method stub
return 0;
CTColor color=fontWrapper.getColor();
long index=color.getIndexed();
if (index==XSSFFont.DEFAULT_FONT_COLOR){
return Font.COLOR_NORMAL;
}
else if(index==IndexedColors.RED){
return Font.COLOR_RED;
}
else{
return Short.parseShort(new Long(index).toString());
}
}
public short getFontHeight() {
// TODO Auto-generated method stub
return 0;
if(fontWrapper.getSz()!=null){
double fontHeight= fontWrapper.getSz().getVal()/20;
return (short)fontHeight;
}
else
return DEFAULT_FONT_SIZE/20;
}
public short getFontHeightInPoints() {
// TODO Auto-generated method stub
return 0;
if(fontWrapper.getSz()!=null){
double fontHeight= fontWrapper.getSz().getVal();// /72;
return (short)fontHeight;//new Double(fontHeight).shortValue();
}
else
return DEFAULT_FONT_SIZE;
}
public String getFontName() {
// TODO Auto-generated method stub
return null;
//AGGIUNGERE CONTROLLO NULL
public String getFontName() {
if(fontWrapper.getName()!=null)
return fontWrapper.getName().getVal();
else
return DEFAULT_FONT_NAME;
}
public short getIndex() {
// TODO Auto-generated method stub
return 0;
}
public boolean getItalic() {
// TODO Auto-generated method stub
return false;
if(fontWrapper.getI()!=null)
return fontWrapper.getI().getVal();
else
return false;
}
public boolean getStrikeout() {
// TODO Auto-generated method stub
return false;
if(fontWrapper.getStrike()!=null)
return fontWrapper.getStrike().getVal();
else
return false;
}
public short getTypeOffset() {
// TODO Auto-generated method stub
return 0;
if(fontWrapper.getVertAlign()!=null){
int val=fontWrapper.getVertAlign().getVal().intValue();
switch (val) {
case STVerticalAlignRun.INT_BASELINE:
return Font.SS_NONE;
case STVerticalAlignRun.INT_SUBSCRIPT:
return Font.SS_SUB;
case STVerticalAlignRun.INT_SUPERSCRIPT:
return Font.SS_SUPER;
default: throw new RuntimeException("Wrong offset value "+val);
}
}
else
return Font.SS_NONE;
}
public byte getUnderline() {
// TODO Auto-generated method stub
return 0;
public byte getUnderline() {
if(fontWrapper.getU()!=null){
//attenzione : -- get val pu˜ tornare null----
switch (fontWrapper.getU().getVal().intValue()) {
case STUnderlineValues.INT_DOUBLE:
return Font.U_DOUBLE;
case STUnderlineValues.INT_DOUBLE_ACCOUNTING:
return Font.U_DOUBLE_ACCOUNTING;
case STUnderlineValues.INT_SINGLE_ACCOUNTING:
return Font.U_SINGLE_ACCOUNTING;
case STUnderlineValues.INT_NONE:
return Font.U_NONE;
case STUnderlineValues.INT_SINGLE:
default:
return Font.U_SINGLE;
}
}
return Font.U_NONE;
}
public void setBoldweight(short boldweight) {
// TODO Auto-generated method stub
public void setBoldweight(short boldweight) {
if(boldweight==Font.BOLDWEIGHT_BOLD){
CTBooleanProperty bold;
if(fontWrapper.getCTFont().getBArray().length==0){
bold=fontWrapper.getCTFont().addNewB();
}
else{
bold=CTBooleanProperty.Factory.newInstance();
}
bold.setVal(true);
fontWrapper.setB(bold);
}
}
public void setCharSet(byte charset) {
// TODO Auto-generated method stub
CTIntProperty charsetProperty;
if(fontWrapper.getCTFont().getCharsetArray().length==0){
charsetProperty=fontWrapper.getCTFont().addNewCharset();
}
else{
charsetProperty=CTIntProperty.Factory.newInstance();
}
switch (charset) {
case Font.ANSI_CHARSET:
charsetProperty.setVal(Charset.ANSI_CHARSET);
break;
case Font.SYMBOL_CHARSET:
charsetProperty.setVal(Charset.SYMBOL_CHARSET);
break;
case Font.DEFAULT_CHARSET:
charsetProperty.setVal(Charset.DEFAULT_CHARSET);
break;
default:
throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset");
}
fontWrapper.setCharset(charsetProperty);
}
public void setColor(short color) {
// TODO Auto-generated method stub
CTColor ctColor;
if(fontWrapper.getCTFont().getColorArray().length==0){
ctColor=fontWrapper.getCTFont().addNewColor();
}
else{
ctColor=CTColor.Factory.newInstance();
}
switch (color) {
case Font.COLOR_NORMAL:{
ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
break;
}
case Font.COLOR_RED:{
ctColor.setIndexed(IndexedColors.RED);
break;
}
default:
ctColor.setIndexed(color);
}
fontWrapper.setColor(ctColor);
}
public void setFontHeight(short height) {
// TODO Auto-generated method stub
CTFontSize fontSize;
if(fontWrapper.getCTFont().getSzArray().length==0){
fontSize=fontWrapper.getCTFont().addNewSz();
}
else{
fontSize=CTFontSize.Factory.newInstance();
}
fontSize.setVal(height*20);
fontWrapper.setSz(fontSize);
}
public void setFontHeightInPoints(short height) {
// TODO Auto-generated method stub
CTFontSize fontSize;
if(fontWrapper.getCTFont().getSzArray().length==0){
fontSize=fontWrapper.getCTFont().addNewSz();
}
else{
fontSize=CTFontSize.Factory.newInstance();
}
fontSize.setVal(height);
fontWrapper.setSz(fontSize);
}
public void setFontName(String name) {
// TODO Auto-generated method stub
CTFontName fontName;
if(fontWrapper.getCTFont().getNameArray().length==0){
fontName=fontWrapper.getCTFont().addNewName();
}
else{
fontName=CTFontName.Factory.newInstance();
}
fontName.setVal(name);
fontWrapper.setName(fontName);
}
public void setItalic(boolean italic) {
// TODO Auto-generated method stub
CTBooleanProperty bool;
if(fontWrapper.getCTFont().getIArray().length==0){
bool=fontWrapper.getCTFont().addNewI();
}
else{
bool=CTBooleanProperty.Factory.newInstance();
}
bool.setVal(italic);
fontWrapper.setI(bool);
}
public void setStrikeout(boolean strikeout) {
// TODO Auto-generated method stub
CTBooleanProperty strike;
if(fontWrapper.getCTFont().getStrikeArray().length==0){
strike=fontWrapper.getCTFont().addNewStrike();
}
else{
strike=CTBooleanProperty.Factory.newInstance();
}
strike.setVal(strikeout);
fontWrapper.setStrike(strike);
}
public void setTypeOffset(short offset) {
// TODO Auto-generated method stub
CTVerticalAlignFontProperty offsetProperty;
if(fontWrapper.getCTFont().getVertAlignArray().length==0){
offsetProperty=fontWrapper.getCTFont().addNewVertAlign();
}
else{
offsetProperty=CTVerticalAlignFontProperty.Factory.newInstance();
}
switch (offset) {
case Font.SS_NONE:
offsetProperty.setVal(STVerticalAlignRun.BASELINE);
break;
case Font.SS_SUB:
offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);
break;
case Font.SS_SUPER:
offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);
break;
}
fontWrapper.setVertAlign(offsetProperty);
}
public void setUnderline(byte underline) {
// TODO Auto-generated method stub
CTUnderlineProperty ctUnderline;
if(fontWrapper.getCTFont().getUArray().length==0){
ctUnderline=fontWrapper.getCTFont().addNewU();
}
else{
ctUnderline=CTUnderlineProperty.Factory.newInstance();
}
switch (underline) {
case Font.U_DOUBLE:
ctUnderline.setVal(STUnderlineValues.DOUBLE);
break;
case Font.U_DOUBLE_ACCOUNTING:
ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING);
break;
case Font.U_SINGLE_ACCOUNTING:
ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING);
break;
case Font.U_NONE:
ctUnderline.setVal(STUnderlineValues.NONE);
break;
case Font.U_SINGLE:
default:
ctUnderline.setVal(STUnderlineValues.SINGLE);
break;
}
fontWrapper.setU(ctUnderline);
}
public long putFont(LinkedList<CTFont> fonts) {
//TODO
/*
* we need to implement a method equals to check that 2 instances of CTFont
* are different by comparison of all font attributes.
* NB: take a look to findFont method in XSSFWorkbook
*/
CTFont font=fontWrapper.getCTFont();
if(fonts.contains(font)) {
return fonts.indexOf(font);
}
@ -146,4 +420,72 @@ public class XSSFFont implements Font {
return fonts.size() - 1;
}
// solo di xssfFont - non di Font-
//sono utilizzati nel metodo createDefaultFont in StylesTable insta.
public int getScheme(){
int fontScheme = fontWrapper.getFontScheme().getVal().intValue();
switch (fontScheme) {
case STFontScheme.INT_MAJOR:
return XSSFFont.SCHEME_MAJOR;
case STFontScheme.INT_MINOR:
return XSSFFont.SCHEME_MINOR;
case STFontScheme.INT_NONE:
return XSSFFont.SCHEME_NONE;
default:
return fontScheme;
}
}
public void setScheme(int scheme){
CTFontScheme ctFontScheme;
if(fontWrapper.getCTFont().getSchemeArray().length==0){
ctFontScheme=fontWrapper.getCTFont().addNewScheme();
}
else{
ctFontScheme=CTFontScheme.Factory.newInstance();
}
switch (scheme) {
case XSSFFont.SCHEME_MAJOR:
ctFontScheme.setVal(STFontScheme.MAJOR);
break;
case XSSFFont.SCHEME_MINOR:
ctFontScheme.setVal(STFontScheme.MINOR);
break;
case XSSFFont.SCHEME_NONE:
ctFontScheme.setVal(STFontScheme.NONE);
break;
default:
throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont");
}
fontWrapper.setFontScheme(ctFontScheme);
}
public int getFamily(){
if(fontWrapper.getFamily()!=null)
return fontWrapper.getFamily().getVal();
else
return XSSFFont.FONT_FAMILY_SWISS;
}
public void setFamily(int value){
//TODO
CTIntProperty family;
if(fontWrapper.getCTFont().getSchemeArray().length==0){
family=fontWrapper.getCTFont().addNewFamily();
}
else{
family=CTIntProperty.Factory.newInstance();
}
family.setVal(value);
//fontWrapper.setFamily
}
}

View File

@ -257,5 +257,9 @@ public class XSSFRow implements Row {
this.row.setHidden(height);
}
public CTRow getCTRow(){
return this.row;
}
}

View File

@ -45,28 +45,38 @@ import org.apache.xmlbeans.XmlOptions;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipCollection;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTControls;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomProperties;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHeaderFooter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOutlinePr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageBreak;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetUpPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetup;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPane;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPrintOptions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRecord;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSelection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetFormatPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetProtection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetView;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetViews;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState;
public class XSSFSheet implements Sheet {
@ -79,9 +89,12 @@ public class XSSFSheet implements Sheet {
protected XSSFWorkbook workbook;
protected CommentsSource sheetComments;
protected CTMergeCells ctMergeCells;
protected ArrayList<Drawing> drawings;
protected ArrayList<Control> controls;
public static final short LeftMargin = 0;
public static final short RightMargin = 1;
public static final short TopMargin = 2;
@ -598,13 +611,15 @@ public class XSSFSheet implements Sheet {
}
public boolean getRowSumsBelow() {
// TODO Auto-generated method stub
return false;
CTSheetPr sheetPr = getSheetTypeSheetPr();
CTOutlinePr outLinePr = sheetPr.getOutlinePr();
return outLinePr.getSummaryBelow();
}
public boolean getRowSumsRight() {
// TODO Auto-generated method stub
return false;
CTSheetPr sheetPr = getSheetTypeSheetPr();
CTOutlinePr outLinePr = sheetPr.getOutlinePr();
return outLinePr.getSummaryRight();
}
public boolean getScenarioProtect() {
@ -633,16 +648,57 @@ public class XSSFSheet implements Sheet {
return getSheetTypePrintOptions().getVerticalCentered();
}
public void groupColumn(short fromColumn, short toColumn) {
// TODO Auto-generated method stub
CTCols ctCols=worksheet.getColsArray(0);
CTCol ctCol=CTCol.Factory.newInstance();
ctCol.setMin(fromColumn);
ctCol.setMax(toColumn);
this.columnHelper.addCleanColIntoCols(ctCols, ctCol);
for(int index=fromColumn;index<=toColumn;index++){
CTCol col=columnHelper.getColumn(index, false);
//col must exist
short outlineLevel=col.getOutlineLevel();
col.setOutlineLevel((short)(outlineLevel+1));
index=(int)col.getMax();
}
worksheet.setColsArray(0,ctCols);
setSheetFormatPrOutlineLevelCol();
}
public void groupRow(int fromRow, int toRow) {
// TODO Auto-generated method stub
for(int i=fromRow;i<=toRow;i++){
XSSFRow xrow=(XSSFRow)getRow(i-1);
if(xrow==null){//create a new Row
xrow=(XSSFRow)createRow(i-1);
}
CTRow ctrow=xrow.getCTRow();
short outlineLevel=ctrow.getOutlineLevel();
ctrow.setOutlineLevel((short)(outlineLevel+1));
}
setSheetFormatPrOutlineLevelRow();
}
private short getMaxOutlineLevelRows(){
short outlineLevel=0;
for(Row r:rows){
XSSFRow xrow=(XSSFRow)r;
outlineLevel=xrow.getCTRow().getOutlineLevel()>outlineLevel? xrow.getCTRow().getOutlineLevel(): outlineLevel;
}
return outlineLevel;
}
private short getMaxOutlineLevelCols(){
CTCols ctCols=worksheet.getColsArray(0);
CTCol[]colArray=ctCols.getColArray();
short outlineLevel=0;
for(CTCol col: colArray){
outlineLevel=col.getOutlineLevel()>outlineLevel? col.getOutlineLevel(): outlineLevel;
}
return outlineLevel;
}
public boolean isColumnBroken(short column) {
CTBreak[] brkArray = getSheetTypeColumnBreaks().getBrkArray();
for (int i = 0 ; i < brkArray.length ; i++) {
@ -720,9 +776,10 @@ public class XSSFSheet implements Sheet {
public void removeRow(Row row) {
int counter = 0;
int rowNum=row.getRowNum();
for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
Row r = it.next();
if (r.getRowNum() == row.getRowNum()) {
if (r.getRowNum() == rowNum) {
it.remove();
worksheet.getSheetData().removeRow(counter);
}
@ -804,7 +861,12 @@ public class XSSFSheet implements Sheet {
}
public void setDialog(boolean b) {
// TODO Auto-generated method stub
if(b && dialogsheet == null){
CTDialogsheet dialogSheet = CTDialogsheet.Factory.newInstance();
dialogsheet = dialogSheet;
}else{
dialogsheet = null;
}
}
public void setDisplayFormulas(boolean show) {
@ -884,13 +946,15 @@ public class XSSFSheet implements Sheet {
}
public void setRowSumsBelow(boolean b) {
// TODO Auto-generated method stub
CTSheetPr sheetPr = getSheetTypeSheetPr();
CTOutlinePr outLinePr = sheetPr.getOutlinePr();
outLinePr.setSummaryBelow(b);
}
public void setRowSumsRight(boolean b) {
// TODO Auto-generated method stub
CTSheetPr sheetPr = getSheetTypeSheetPr();
CTOutlinePr outLinePr = sheetPr.getOutlinePr();
outLinePr.setSummaryRight(b);
}
public void setVerticallyCenter(boolean value) {
@ -946,16 +1010,51 @@ public class XSSFSheet implements Sheet {
getSheetTypeSheetView().setTopLeftCell(cellRef);
}
public void ungroupColumn(short fromColumn, short toColumn) {
// TODO Auto-generated method stub
public void ungroupColumn(short fromColumn, short toColumn) {
CTCols cols=worksheet.getColsArray(0);
for(int index=fromColumn;index<=toColumn;index++){
CTCol col=columnHelper.getColumn(index, false);
if(col!=null){
short outlineLevel=col.getOutlineLevel();
col.setOutlineLevel((short)(outlineLevel-1));
index=(int)col.getMax();
if(col.getOutlineLevel()<=0){
int colIndex=columnHelper.getIndexOfColumn(cols,col);
worksheet.getColsArray(0).removeCol(colIndex);
}
}
}
worksheet.setColsArray(0,cols);
setSheetFormatPrOutlineLevelCol();
}
public void ungroupRow(int fromRow, int toRow) {
// TODO Auto-generated method stub
for(int i=fromRow;i<=toRow;i++){
XSSFRow xrow=(XSSFRow)getRow(i-1);
if(xrow!=null){
CTRow ctrow=xrow.getCTRow();
short outlinelevel=ctrow.getOutlineLevel();
ctrow.setOutlineLevel((short)(outlinelevel-1));
//remove a row only if the row has no cell and if the outline level is 0
if(ctrow.getOutlineLevel()==0 && xrow.getFirstCellNum()==-1){
removeRow(xrow);
}
}
}
setSheetFormatPrOutlineLevelRow();
}
private void setSheetFormatPrOutlineLevelRow(){
short maxLevelRow=getMaxOutlineLevelRows();
getSheetTypeSheetFormatPr().setOutlineLevelRow((short)(maxLevelRow));
}
private void setSheetFormatPrOutlineLevelCol(){
short maxLevelCol=getMaxOutlineLevelCols();
getSheetTypeSheetFormatPr().setOutlineLevelCol((short)(maxLevelCol));
}
public void setSelected(boolean flag) {
CTSheetViews views = getSheetTypeSheetViews();
for (CTSheetView view : views.getSheetViewArray()) {
@ -1101,4 +1200,9 @@ public class XSSFSheet implements Sheet {
}
return getDefaultSheetView().getPane();
}
}

View File

@ -65,6 +65,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
@ -260,8 +261,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public Font createFont() {
// TODO Auto-generated method stub
return null;
return new XSSFFont();
}
public XSSFName createName() {
@ -306,8 +306,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public Font findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
// TODO Auto-generated method stub
return null;
short fontNum=getNumberOfFonts();
for (short i = 0; i <= fontNum; i++) {
XSSFFont xssfFont = (XSSFFont)getFontAt(i);
if (xssfFont.getBoldweight() == boldWeight
&& xssfFont.getColor() == color
&& xssfFont.getFontHeightInPoints() == fontHeight
&& xssfFont.getFontName().equals(name)
&& xssfFont.getItalic() == italic
&& xssfFont.getStrikeout() == strikeout
&& xssfFont.getTypeOffset() == typeOffset
&& xssfFont.getUnderline() == underline)
{
return xssfFont;
}
}
return null;
}
public List getAllEmbeddedObjects() {
@ -352,8 +366,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
public CellStyle getCellStyleAt(short idx) {
// TODO Auto-generated method stub
return null;
return stylesSource.getStyleAt(idx);
}
public Palette getCustomPalette() {
@ -361,14 +374,24 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null;
}
/**
* get the first tab that is displayed in the list of tabs in excel.
*/
public int getFirstVisibleTab() {
CTBookViews bookViews = workbook.getBookViews();
CTBookView bookView = bookViews.getWorkbookViewArray(0);
return (short) bookView.getActiveTab();
}
/**
* deprecated Aug 2008
* @deprecated - Misleading name - use getFirstVisibleTab()
*/
public short getDisplayedTab() {
// TODO Auto-generated method stub
return 0;
return (short) getFirstVisibleTab();
}
public Font getFontAt(short idx) {
// TODO Auto-generated method stub
return null;
return stylesSource.getFontAt(idx);
}
public XSSFName getNameAt(int index) {
@ -395,9 +418,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
return null;
}
public short getNumCellStyles() {
// TODO Auto-generated method stub
return 0;
public short getNumCellStyles() {
return (short) ((StylesTable)stylesSource).getNumCellStyles();
}
public short getNumberOfFonts() {
@ -553,9 +575,22 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
}
/**
* sets the first tab that is displayed in the list of tabs
* in excel.
* @param index
*/
public void setFirstVisibleTab(short index) {
CTBookViews bookViews = workbook.getBookViews();
CTBookView bookView= bookViews.getWorkbookViewArray(0);
bookView.setActiveTab(index);
}
/**
* deprecated Aug 2008
* @deprecated - Misleading name - use setFirstVisibleTab()
*/
public void setDisplayedTab(short index) {
// TODO Auto-generated method stub
setFirstVisibleTab(index);
}
public void setPrintArea(int sheetIndex, String reference) {

View File

@ -18,6 +18,7 @@ package org.apache.poi.xssf.usermodel.extensions;
import java.util.LinkedList;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
@ -65,5 +66,15 @@ public class XSSFCellFill {
public CTFill getCTFill() {
return this.fill;
}
public void setFillBackgroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewBgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setBgColor(ctColor);
}
public void setFillForegroundColor(long index) {
CTColor ctColor=fill.getPatternFill().addNewFgColor();
ctColor.setIndexed(index);
fill.getPatternFill().setFgColor(ctColor);
}
}

View File

@ -161,15 +161,18 @@ public class ColumnHelper {
* Insert a new CTCol at position 0 into cols, setting min=min, max=max and
* copying all the colsWithAttributes array cols attributes into newCol
*/
private CTCol insertCol(CTCols cols, long min, long max,
CTCol[] colsWithAttributes) {
CTCol newCol = cols.insertNewCol(0);
newCol.setMin(min);
newCol.setMax(max);
for (CTCol col : colsWithAttributes) {
setColumnAttributes(col, newCol);
private CTCol insertCol(CTCols cols, long min, long max,
CTCol[] colsWithAttributes) {
if(!columnExists(cols,min,max)){
CTCol newCol = cols.insertNewCol(0);
newCol.setMin(min);
newCol.setMax(max);
for (CTCol col : colsWithAttributes) {
setColumnAttributes(col, newCol);
}
return newCol;
}
return newCol;
return null;
}
public boolean columnExists(CTCols cols, long index) {
@ -186,6 +189,9 @@ public class ColumnHelper {
toCol.setHidden(fromCol.getHidden());
toCol.setBestFit(fromCol.getBestFit());
toCol.setStyle(fromCol.getStyle());
if(fromCol.getOutlineLevel()!=0){
toCol.setOutlineLevel(fromCol.getOutlineLevel());
}
}
public void setColBestFit(long index, boolean bestFit) {
@ -229,5 +235,22 @@ public class ColumnHelper {
}
return -1;
}
private boolean columnExists(CTCols cols, long min, long max) {
for (int i = 0; i < cols.sizeOfColArray(); i++) {
if (cols.getColArray(i).getMin() == min && cols.getColArray(i).getMax() == max) {
return true;
}
}
return false;
}
public int getIndexOfColumn(CTCols cols, CTCol col) {
for (int i = 0; i < cols.sizeOfColArray(); i++) {
if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) {
return i;
}
}
return -1;
}
}

View File

@ -0,0 +1,176 @@
package org.apache.poi.xssf.util;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
/*
* The font element in xml is definited like <choice maxOccurs="unbounded">.
* So in the java object CTFont all methods get and set returns an array of elements also if there is always defined
* only one type of attribute per type.
* This class is made to make simple using method get and set instead of getArray() or set(index,object).
* We consider always the index 0 like the only one index to refer of CT_Font attribute.
*
*/
public class CTFontWrapper{
private CTFont font;
public CTFontWrapper(CTFont font){
this.font=font;
}
public CTFont getCTFont(){
return font;
}
public CTBooleanProperty getB(){
if( font.getBArray().length>0)
return font.getBArray(0);
else
return null;
}
public CTIntProperty getCharset(){
if(font.getCharsetArray().length>0)
return font.getCharsetArray(0);
else
return null;
}
public CTColor getColor(){
if(font.getColorArray().length>0)
return font.getColorArray(0);
else
return null;
}
public CTBooleanProperty getStrike(){
if(font.getStrikeArray().length>0)
return font.getStrikeArray(0);
else
return null;
}
public CTVerticalAlignFontProperty getVertAlign() {
if(font.getVertAlignArray().length>0)
return font.getVertAlignArray(0);
else
return null;
}
public CTFontName setName(){
if(font.getNameArray().length>0)
return font.getNameArray(0);
else
return null;
}
public CTFontSize getSz(){
if(font.getSzArray().length>0)
return font.getSzArray(0);
else
return null;
}
public CTBooleanProperty getI(){
if(font.getIArray().length>0)
return font.getIArray(0);
else
return null;
}
public CTUnderlineProperty getU(){
if(font.getUArray().length>0)
return font.getUArray(0);
else
return null;
}
public void setB(CTBooleanProperty value){
font.setBArray(0,value);
}
public void setCharset(CTIntProperty value){
font.setCharsetArray(0, value);
}
public void setColor(CTColor value){
font.setColorArray(0,value);
}
public void setFontName(CTFontName value){
font.setNameArray(0,value);
}
public void setSz(CTFontSize value){
font.setSzArray(0,value);
}
public void setI(CTBooleanProperty value){
font.setIArray(0,value);
}
public void setU(CTUnderlineProperty value){
font.setUArray(0,value);
}
public void setStrike(CTBooleanProperty value){
font.setStrikeArray(0,value);
}
public void setVertAlign(CTVerticalAlignFontProperty value){
font.setVertAlignArray(0,value);
}
public void setName(CTFontName fontName) {
font.setNameArray(0,fontName);
}
public CTFontName getName() {
return font.getNameArray(0);
}
public CTIntProperty getFamily() {
return font.getFamilyArray(0);
}
public void setFamily(CTIntProperty family) {
font.setFamilyArray(0,family);
}
public void setFontScheme(CTFontScheme ctFontScheme) {
font.setSchemeArray(0,ctFontScheme);
}
public CTFontScheme getFontScheme() {
return font.getSchemeArray(0);
}
// methods used in FontFormatting
public CTBooleanProperty getOutline(){
return font.getOutlineArray(0);
}
}

View File

@ -0,0 +1,27 @@
package org.apache.poi.xssf.util;
public class Charset {
public static final int ANSI_CHARSET=0;
public static final int DEFAULT_CHARSET=1;
public static final int SYMBOL_CHARSET=2;
public static final int MAC_CHARSET=77;
public static final int SHIFTJIS_CHARSET=128;
public static final int HANGEUL_CHARSET=129;
public static final int HANGUL_CHARSET=129;
public static final int JOHAB_CHARSET=130;
public static final int GB2312_CHARSET=134;
public static final int CHINESEBIG5_CHARSET=136;
public static final int GREEK_CHARSET=161;
public static final int TURKISH_CHARSET=162;
public static final int VIETNAMESE_CHARSET=163;
public static final int HEBREW_CHARSET=177;
public static final int ARABIC_CHARSET=178;
public static final int BALTIC_CHARSET=186;
public static final int RUSSIAN_CHARSET=204;
public static final int THAI_CHARSET=222;
public static final int EASTEUROPE_CHARSET=238;
public static final int OEM_CHARSET=255;
}

View File

@ -0,0 +1,16 @@
package org.apache.poi.xssf.util;
public class IndexedColors {
public static int BLACK=0;
public static int WHITE=1;
public static int RED=2;
public static int GREEN=3;
public static int BLUE=4;
public static int YELLOW=5;
public static int PINK=6;
public static int LIGHT_GREY=22;
public static int DARK_GREY=23;
}

View File

@ -27,6 +27,9 @@ import org.apache.poi.xssf.io.TestLoadSaveXSSF;
import org.apache.poi.xssf.model.TestCommentsTable;
import org.apache.poi.xssf.model.TestStylesTable;
import org.apache.poi.xssf.usermodel.AllXSSFUsermodelTests;
import org.apache.poi.xssf.usermodel.TestXSSFFont;
import org.apache.poi.xssf.usermodel.TestXSSFSheet;
import org.apache.poi.xssf.usermodel.TestXSSFWorkbook;
import org.apache.poi.xssf.util.TestCTColComparator;
import org.apache.poi.xssf.util.TestNumericRanges;
@ -40,7 +43,6 @@ public final class AllXSSFTests {
public static Test suite() {
TestSuite result = new TestSuite(AllXSSFTests.class.getName());
result.addTest(AllXSSFUsermodelTests.suite());
result.addTestSuite(TestXSSFReader.class);
result.addTestSuite(TestXSSFExcelExtractor.class);
result.addTestSuite(TestLoadSaveXSSF.class);
@ -48,7 +50,7 @@ public final class AllXSSFTests {
result.addTestSuite(TestStylesTable.class);
result.addTestSuite(TestCellReference.class);
result.addTestSuite(TestCTColComparator.class);
result.addTestSuite(TestNumericRanges.class);
result.addTestSuite(TestNumericRanges.class);
return result;
}
}

View File

@ -52,6 +52,9 @@ public final class AllXSSFUsermodelTests {
result.addTestSuite(TestXSSFRow.class);
result.addTestSuite(TestXSSFSheet.class);
result.addTestSuite(TestXSSFWorkbook.class);
result.addTestSuite(TestXSSFFont.class);
return result;
}
}

View File

@ -19,11 +19,13 @@ package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.apache.poi.xssf.util.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
@ -222,8 +224,33 @@ public class TestXSSFCellStyle extends TestCase {
assertEquals(8, cellStyle.getFillPattern());
}
public void testGetFont() {
assertNotNull(cellStyle.getFont());
public void testGetSetFont() {
assertNotNull(this.cellStyle.getFont());
StylesSource stylesSource=new StylesTable();
XSSFFont xssfFont=new XSSFFont();
xssfFont.setFontName("Arial");
stylesSource.putFont(xssfFont);
XSSFCellStyle cellStyle=new XSSFCellStyle(stylesSource);
XSSFFont xssfFont2=new XSSFFont();
xssfFont2.setFontName("courier");
xssfFont2.setFontHeightInPoints((short)10);
cellStyle.setFont(xssfFont2);
assertEquals(2,cellStyle.getFontIndex());
assertEquals(xssfFont2.getFontName(),cellStyle.getFont().getFontName());
assertEquals(stylesSource.getFontAt(2).getFontHeightInPoints(),cellStyle.getFont().getFontHeightInPoints());
cellStyle.setFont(xssfFont);
assertEquals(1,cellStyle.getFontIndex());
XSSFFont xssfFont3=new XSSFFont();
xssfFont3.setFontName("Arial");
cellStyle.setFont(xssfFont3);
assertNotSame(1,cellStyle.getFontIndex());
}
public void testGetSetHidden() {
@ -274,6 +301,32 @@ public class TestXSSFCellStyle extends TestCase {
assertFalse(cellXf.getAlignment().getWrapText());
}
public void testGetSetFillBackgroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(IndexedColors.BLUE);
assertEquals(IndexedColors.BLUE, cellStyle.getFillBackgroundColor());
cellStyle.setFillBackgroundColor((short)IndexedColors.GREEN);
assertEquals(IndexedColors.GREEN,ctFill.getPatternFill().getBgColor().getIndexed());
}
public void testGetSetFillForegroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5);
assertEquals(5, cellStyle.getFillForegroundColor());
ctFgColor.setIndexed(IndexedColors.BLUE);
assertEquals(IndexedColors.BLUE, cellStyle.getFillForegroundColor());
cellStyle.setFillForegroundColor((short)IndexedColors.GREEN);
assertEquals(IndexedColors.GREEN,ctFill.getPatternFill().getFgColor().getIndexed());
}
/**
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
*/

View File

@ -0,0 +1,297 @@
package org.apache.poi.xssf.usermodel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.util.CTFontWrapper;
import org.apache.poi.xssf.util.Charset;
import org.apache.poi.xssf.util.IndexedColors;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
public class TestXSSFFont extends TestCase{
public void testConstructor(){
XSSFFont xssfFont=new XSSFFont();
assertNotNull(xssfFont);
assertNotNull(xssfFont.getCTFont());
}
public void testBoldweight(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTBooleanProperty bool=wrapper.getCTFont().addNewB();
bool.setVal(false);
wrapper.setB(bool);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.BOLDWEIGHT_NORMAL, xssfFont.getBoldweight());
xssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
assertEquals(ctFont.getBArray().length,1);
assertEquals(true, ctFont.getBArray(0).getVal());
assertEquals(true,wrapper.getB().getVal());
}
public void testCharSet(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTIntProperty prop=ctFont.addNewCharset();
prop.setVal(Charset.ANSI_CHARSET);
wrapper.setCharset(prop);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
xssfFont.setCharSet(Font.DEFAULT_CHARSET);
assertEquals(Charset.DEFAULT_CHARSET, wrapper.getCharset().getVal());
}
public void testFontName(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontName fname=ctFont.addNewName();
fname.setVal("Arial");
wrapper.setFontName(fname);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals("Arial", xssfFont.getFontName());
xssfFont.setFontName("Courier");
assertEquals("Courier",wrapper.getName().getVal());
}
public void testItalic(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTBooleanProperty bool=wrapper.getCTFont().addNewI();
bool.setVal(false);
wrapper.setI(bool);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(false, xssfFont.getItalic());
xssfFont.setItalic(true);
assertEquals(ctFont.getIArray().length,1);
assertEquals(true, ctFont.getIArray(0).getVal());
assertEquals(true,wrapper.getI().getVal());
}
public void testStrikeout(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTBooleanProperty bool=wrapper.getCTFont().addNewStrike();
bool.setVal(false);
wrapper.setStrike(bool);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(false, xssfFont.getStrikeout());
xssfFont.setStrikeout(true);
assertEquals(ctFont.getStrikeArray().length,1);
assertEquals(true, ctFont.getStrikeArray(0).getVal());
assertEquals(true,wrapper.getStrike().getVal());
}
public void testFontHeight(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontSize size=ctFont.addNewSz();
size.setVal(11);
wrapper.setSz(size);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(11/20,xssfFont.getFontHeight());
xssfFont.setFontHeight((short)20);
assertEquals(new Double(20*20).doubleValue(),wrapper.getSz().getVal()); }
public void testFontHeightInPoint(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontSize size=ctFont.addNewSz();
size.setVal(14);
wrapper.setSz(size);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(14,xssfFont.getFontHeightInPoints());
xssfFont.setFontHeightInPoints((short)20);
assertEquals(new Double(20).doubleValue(),wrapper.getSz().getVal());
}
public void testUnderline(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTUnderlineProperty underlinePropr=wrapper.getCTFont().addNewU();
underlinePropr.setVal(STUnderlineValues.SINGLE);
wrapper.setU(underlinePropr);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.U_SINGLE, xssfFont.getUnderline());
xssfFont.setUnderline(Font.U_DOUBLE);
assertEquals(ctFont.getUArray().length,1);
assertEquals(STUnderlineValues.DOUBLE,wrapper.getU().getVal());
}
public void testColor(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTColor color=ctFont.addNewColor();
//color.setIndexed(IndexedColors.DEFAULT_COLOR);
color.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
wrapper.setColor(color);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.COLOR_NORMAL,xssfFont.getColor());
xssfFont.setColor(Font.COLOR_RED);
assertEquals(IndexedColors.RED,wrapper.getColor().getIndexed());
}
public void testFamily(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTIntProperty family=ctFont.addNewFamily();
family.setVal(XSSFFont.FONT_FAMILY_MODERN);
wrapper.setFamily(family);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(XSSFFont.FONT_FAMILY_MODERN,xssfFont.getFamily());
}
public void testScheme(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontScheme scheme=ctFont.addNewScheme();
scheme.setVal(STFontScheme.MAJOR);
wrapper.setFontScheme(scheme);
XSSFFont font=new XSSFFont(ctFont);
assertEquals(XSSFFont.SCHEME_MAJOR,font.getScheme());
font.setScheme(XSSFFont.SCHEME_NONE);
assertEquals(STFontScheme.NONE,wrapper.getFontScheme().getVal());
}
public void testTypeOffset(){
CTFont ctFont=CTFont.Factory.newInstance();
CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTVerticalAlignFontProperty valign=ctFont.addNewVertAlign();
valign.setVal(STVerticalAlignRun.BASELINE);
wrapper.setVertAlign(valign);
XSSFFont font=new XSSFFont(ctFont);
assertEquals(Font.SS_NONE,font.getTypeOffset());
font.setTypeOffset(XSSFFont.SS_SUPER);
assertEquals(STVerticalAlignRun.SUPERSCRIPT,wrapper.getVertAlign().getVal());
}
public void testXSSFFont() throws IOException{
XSSFWorkbook workbook=new XSSFWorkbook();
//Font font1=workbook.createFont();
Sheet sheet=workbook.createSheet("sheet 1 - test font");
Row row=sheet.createRow(0);
Cell cell=row.createCell(0);
cell.setCellValue(new XSSFRichTextString("XSSFFont test example file"));
Font font=new XSSFFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short)22);
font.setColor((short)IndexedColors.BLUE);
font.setFontName("Verdana");
CellStyle cellStyleTitle=workbook.createCellStyle();
cellStyleTitle.setFont(font);
cell.setCellStyle(cellStyleTitle);
row=sheet.createRow(3);
Font font1=new XSSFFont();
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
font1.setItalic(true);
font1.setFontHeightInPoints((short)18);
font1.setColor(Font.COLOR_RED);
font1.setFontName("Arial");
CellStyle cellStyle1=workbook.createCellStyle();
cellStyle1.setFont(font1);
Cell cell1=row.createCell(0);
cell1.setCellValue(new XSSFRichTextString("red bold 18pt italic Arial"));
cell1.setCellStyle(cellStyle1);
row=sheet.createRow(4);
Font font2=new XSSFFont();
font2.setFontHeight((short)1);
font2.setFontName("Courier");
font2.setColor(Font.COLOR_NORMAL);
font2.setUnderline(Font.U_DOUBLE);
CellStyle cellStyle2=workbook.createCellStyle();
cellStyle2.setFont(font2);
Cell cell2=row.createCell(0);
cell2.setCellValue(new XSSFRichTextString("Something in courier underlined"));
cell2.setCellStyle(cellStyle2);
row=sheet.createRow(5);
cell1=row.createCell(0);
Font font3=new XSSFFont();
font3.setFontHeightInPoints((short)9);
font3.setFontName("Times");
font3.setStrikeout(true);
font3.setColor((short)IndexedColors.PINK);
CellStyle cellStyle3=workbook.createCellStyle();
cellStyle3.setFont(font3);
cell1.setCellValue(new XSSFRichTextString("pink italic Times 9pt strikeout!!!"));
cell1.setCellStyle(cellStyle3);
File tmpFile = new File("test-ooxml-font.xlsx");
if(tmpFile.exists()) tmpFile.delete();
FileOutputStream out = new FileOutputStream(tmpFile);
workbook.write(out);
out.close();
}
}

View File

@ -35,11 +35,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPane;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPaneState;
public class TestXSSFSheet extends TestCase {
@ -727,4 +727,98 @@ public class TestXSSFSheet extends TestCase {
row9.setHeight((short) 9);
return sheet;
}
public void testGroupUngroupColumn() {
Workbook workbook = new XSSFWorkbook();
CTSheet ctSheet = CTSheet.Factory.newInstance();
CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
//one level
System.out.println("livello 1");
sheet.groupColumn((short)2,(short)7);
sheet.groupColumn((short)10,(short)11);
CTCols cols=sheet.getWorksheet().getColsArray(0);
assertEquals(2,cols.sizeOfColArray());
CTCol[]colArray=cols.getColArray();
assertNotNull(colArray);
assertEquals(2,colArray[0].getMin());
assertEquals(7,colArray[0].getMax());
assertEquals(1, colArray[0].getOutlineLevel());
//two level
System.out.println("\n livello 2");
sheet.groupColumn((short)1,(short)2);
cols=sheet.getWorksheet().getColsArray(0);
assertEquals(4,cols.sizeOfColArray());
colArray=cols.getColArray();
assertEquals(2, colArray[1].getOutlineLevel());
//three level
System.out.println("\n livello 3");
sheet.groupColumn((short)6,(short)8);
sheet.groupColumn((short)2,(short)3);
cols=sheet.getWorksheet().getColsArray(0);
assertEquals(7,cols.sizeOfColArray());
colArray=cols.getColArray();
assertEquals(3, colArray[1].getOutlineLevel());
assertEquals(3,sheet.getSheetTypeSheetFormatPr().getOutlineLevelCol());
sheet.ungroupColumn((short)8,(short) 10);
colArray=cols.getColArray();
//assertEquals(3, colArray[1].getOutlineLevel());
sheet.ungroupColumn((short)4,(short)6);
sheet.ungroupColumn((short)2,(short)2);
colArray=cols.getColArray();
assertEquals(4, colArray.length);
assertEquals(2,sheet.getSheetTypeSheetFormatPr().getOutlineLevelCol());
}
public void testGroupUngroupRow() {
Workbook workbook = new XSSFWorkbook();
CTSheet ctSheet = CTSheet.Factory.newInstance();
CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
//one level
sheet.groupRow(9,10);
assertEquals(2,sheet.rows.size());
CTRow[]rowArray=sheet.getWorksheet().getSheetData().getRowArray();
assertEquals(2,rowArray.length);
CTRow ctrow=rowArray[0];
assertNotNull(ctrow);
assertEquals(9,ctrow.getR());
assertEquals(1, ctrow.getOutlineLevel());
assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
//two level
sheet.groupRow(10,13);
rowArray=sheet.getWorksheet().getSheetData().getRowArray();
assertEquals(5,rowArray.length);
assertEquals(5,sheet.rows.size());
ctrow=rowArray[1];
assertNotNull(ctrow);
assertEquals(10,ctrow.getR());
assertEquals(2, ctrow.getOutlineLevel());
assertEquals(2,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
sheet.ungroupRow(8, 10);
rowArray=sheet.getWorksheet().getSheetData().getRowArray();
assertEquals(4,rowArray.length);
assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
sheet.ungroupRow(10,10);
rowArray=sheet.getWorksheet().getSheetData().getRowArray();
assertEquals(3,rowArray.length);
assertEquals(3,sheet.rows.size());
assertEquals(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
}
}

View File

@ -25,19 +25,19 @@ import java.io.OutputStream;
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Name;
import org.apache.poi.ss.usermodel.RichTextString;
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.xssf.model.StylesTable;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
import org.openxml4j.opc.ContentTypes;
import org.openxml4j.opc.Package;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.PackagingURIHelper;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
public class TestXSSFWorkbook extends TestCase {
public TestXSSFWorkbook(String name) {
@ -239,6 +239,85 @@ public class TestXSSFWorkbook extends TestCase {
}
public void testFindFont(){
//get dafault font and check against default value
XSSFWorkbook workbook = new XSSFWorkbook();
Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, Font.COLOR_NORMAL, (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
assertNotNull(fontFind);
//get default font, then change 2 values and check against different values (height changes)
Font font=workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setUnderline(Font.U_DOUBLE);
StylesSource styleSource=new StylesTable();
long index=styleSource.putFont(font);
System.out.println("index="+index);
workbook.setStylesSource(styleSource);
fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, Font.COLOR_NORMAL, (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
assertNull(fontFind);
}
public void testGetCellStyleAt(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = 0;
//get default style
CellStyle cellStyleAt = workbook.getCellStyleAt(i);
assertNotNull(cellStyleAt);
//get custom style
StylesSource styleSource = workbook.getStylesSource();
CellStyle customStyle = new XSSFCellStyle(styleSource);
Font font = new XSSFFont();
font.setFontName("Verdana");
customStyle.setFont(font);
Long x = styleSource.putStyle(customStyle);
cellStyleAt = workbook.getCellStyleAt(x.shortValue());
assertNotNull(cellStyleAt);
}
public void testGetFontAt(){
XSSFWorkbook workbook = new XSSFWorkbook();
StylesSource styleSource = workbook.getStylesSource();
short i = 0;
//get default font
Font fontAt = workbook.getFontAt(i);
assertNotNull(fontAt);
//get customized font
Font customFont = new XSSFFont();
customFont.setItalic(true);
Long x = styleSource.putFont(customFont);
fontAt = workbook.getFontAt(x.shortValue());
assertNotNull(fontAt);
}
public void testGetNumCellStyles(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = workbook.getNumCellStyles();
//get default cellStyles
assertEquals(1, i);
//get wrong value
assertNotSame(2, i);
}
public void testGetDisplayedTab(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = (short) workbook.getFirstVisibleTab();
//get default diplayedTab
assertEquals(0, i);
}
public void testSetDisplayedTab(){
XSSFWorkbook workbook = new XSSFWorkbook();
workbook.setFirstVisibleTab(new Integer(1).shortValue());
short i = (short) workbook.getFirstVisibleTab();
//0 (defualt value) is not longer set
assertNotSame(0, i);
//1 is the default tab
assertEquals(1, i);
}
public void testLoadSave() throws Exception {
File xml = new File(
System.getProperty("HSSF.testdata.path") +