<delete file="${version.java}" />
</target>
- <target name="test" depends="test-main,test-scratchpad,test-contrib"
- description="Tests main, contrib and scratchpad"/>
+ <target name="test" depends="test-main,test-scratchpad,test-contrib,test-ooxml"
+ description="Tests main, contrib, scratchpad and ooxml"/>
<target name="-test-main-check">
<uptodate property="main.test.notRequired" targetfile="${main.testokfile}">
</manifest>
</jar>
</target>
- <target name="jar" depends="compile, compile-version, jar-14" description="Creates jar files for distribution">
+ <target name="jar" depends="compile, compile-version, jar-14, jar-ooxml" description="Creates jar files for distribution">
<jar destfile="${dist.dir}/${jar.name}-${version.id}-${DSTAMP}.jar">
<fileset dir="${main.output.dir}" />
<fileset dir="legal/" />
--- /dev/null
+/* ====================================================================
+ 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.examples;
+
+import java.io.FileOutputStream;
+import java.util.Date;
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFRichTextString;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+/**
+ *
+ */
+public class CreateCell {
+
+
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ CreationHelper creationHelper = wb.getCreationHelper();
+ Sheet sheet = wb.createSheet("new sheet");
+
+ // Create a row and put some cells in it. Rows are 0 based.
+ Row row = sheet.createRow((short)0);
+ // Create a cell and put a value in it.
+ Cell cell = row.createCell((short)0);
+ cell.setCellValue(1);
+
+ //numeric value
+ row.createCell(1).setCellValue(1.2);
+
+ //plain string value
+ row.createCell(2).setCellValue("This is a string cell");
+
+ //rich text string
+ RichTextString str = creationHelper.createRichTextString("Apache");
+ Font font = wb.createFont();
+ font.setItalic(true);
+ font.setUnderline(Font.U_SINGLE);
+ str.applyFont(font);
+ row.createCell(3).setCellValue(str);
+
+ //boolean value
+ row.createCell(4).setCellValue(true);
+
+ //formula
+ row.createCell(5).setCellFormula("SUM(A1:B1)");
+
+ //date
+ CellStyle style = wb.createCellStyle();
+ style.setDataFormat(creationHelper.createDataFormat().getFormat("m/d/yy h:mm"));
+ cell = row.createCell(6);
+ cell.setCellValue(new Date());
+ cell.setCellStyle(style);
+
+ // Write the output to a file
+ FileOutputStream fileOut = new FileOutputStream("ooxml-cell.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+}
s2.getRow(2).createCell(1).setCellValue(createHelper.createRichTextString("Sheet 2"));
-
+/*
// Comment
Comment comment = ((XSSFSheet)s1).createComment();
// HSSFPatriarch patriach = (HSSFPatriarch)s1.createDrawingPatriarch();
hyperlink.setLabel("Link to POI");
s1.getRow(1).createCell(1).setHyperlink(hyperlink);
s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI"));
-
+*/
// Save
FileOutputStream fout = new FileOutputStream("NewFile.xlsx");
wb.write(fout);
--- /dev/null
+/* ====================================================================
+ 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.examples;
+
+import java.io.FileOutputStream;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.DataFormat;
+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.XSSFWorkbook;
+
+/**
+ * How to set user-defined date formats
+ */
+public class CreateUserDefinedDataFormats {
+
+
+ public static void main(String[]args) throws Exception {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet("format sheet");
+ CellStyle style;
+ DataFormat format = wb.createDataFormat();
+ Row row;
+ Cell cell;
+ short rowNum = 0;
+ short colNum = 0;
+
+ row = sheet.createRow(rowNum++);
+ cell = row.createCell(colNum);
+ cell.setCellValue(11111.25);
+ style = wb.createCellStyle();
+ style.setDataFormat(format.getFormat("0.0"));
+ cell.setCellStyle(style);
+
+ row = sheet.createRow(rowNum++);
+ cell = row.createCell(colNum);
+ cell.setCellValue(11111.25);
+ style = wb.createCellStyle();
+ style.setDataFormat(format.getFormat("#,##0.0000"));
+ cell.setCellStyle(style);
+
+ FileOutputStream fileOut = new FileOutputStream("ooxml_dataFormat.xlsx");
+ wb.write(fileOut);
+ fileOut.close();
+ }
+
+}
package org.apache.poi.xssf.usermodel.examples;\r
\r
import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
+import org.apache.poi.xssf.usermodel.IndexedColors;\r
import org.apache.poi.ss.usermodel.*;\r
\r
import java.io.FileOutputStream;\r
// Style the cell with borders all around.\r
CellStyle style = wb.createCellStyle();\r
style.setBorderBottom(CellStyle.BORDER_THIN);\r
- style.setBottomBorderColor((short)IndexedColors.BLACK);\r
+ style.setBottomBorderColor(IndexedColors.BLACK.getIndex());\r
style.setBorderLeft(CellStyle.BORDER_THIN);\r
- style.setLeftBorderColor((short)IndexedColors.GREEN);\r
+ style.setLeftBorderColor(IndexedColors.GREEN.getIndex());\r
style.setBorderRight(CellStyle.BORDER_THIN);\r
- style.setRightBorderColor((short)IndexedColors.BLUE);\r
+ style.setRightBorderColor(IndexedColors.BLUE.getIndex());\r
style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);\r
- style.setTopBorderColor((short)IndexedColors.BLACK);\r
+ style.setTopBorderColor(IndexedColors.BLACK.getIndex());\r
cell.setCellStyle(style);\r
\r
// Write the output to a file\r
import org.apache.poi.ss.usermodel.*;\r
import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
import org.apache.poi.xssf.usermodel.XSSFRichTextString;\r
+import org.apache.poi.xssf.usermodel.IndexedColors;\r
import org.apache.poi.xssf.usermodel.extensions.XSSFColor;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
\r
import java.io.FileOutputStream;\r
\r
font.setFontHeightInPoints((short)24);\r
font.setFontName("Courier New");\r
\r
- font.setColor((short)IndexedColors.RED);\r
+ font.setColor(IndexedColors.RED.getIndex());\r
\r
font.setItalic(true);\r
font.setStrikeout(true);\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.usermodel.examples;\r
+\r
+import org.apache.poi.xssf.usermodel.*;\r
+import org.apache.poi.ss.usermodel.*;\r
+import org.apache.poi.hssf.util.HSSFColor;\r
+import org.apache.xmlbeans.XmlOptions;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.Writer;\r
+import java.io.StringWriter;\r
+\r
+/**\r
+ * Demonstrates how to work with rich text\r
+ */\r
+public class WorkingWithRichText {\r
+\r
+ public static void main(String[] args)\r
+ throws Exception\r
+ {\r
+ XSSFWorkbook wb = new XSSFWorkbook();\r
+\r
+ XSSFSheet sheet = wb.createSheet();\r
+ XSSFRow row = sheet.createRow((short) 2);\r
+\r
+ XSSFCell cell = row.createCell(1);\r
+ XSSFRichTextString rt = new XSSFRichTextString("The quick");\r
+\r
+ XSSFFont font1 = wb.createFont();\r
+ font1.setBold(true);\r
+ rt.append(" brown fox", font1);\r
+\r
+ XSSFFont font2 = wb.createFont();\r
+ font2.setItalic(true);\r
+ font2.setColor(IndexedColors.RED.getIndex());\r
+ rt.applyFont((short)0);\r
+ cell.setCellValue(rt);\r
+\r
+ // Write the output to a file\r
+ FileOutputStream fileOut = new FileOutputStream("rich_text.xlsx");\r
+ wb.write(fileOut);\r
+ fileOut.close();\r
+\r
+ }\r
+\r
+ }\r
package org.apache.poi.ss.usermodel;
public interface Comment {
- public void setRow(short row);
+ public void setRow(int row);
public void setColumn(short row);
}
void setCellValue(RichTextString value);
+ void setCellValue(String value);
+
void setCellFormula(String formula);
String getCellFormula();
public interface Color {
- /**
- * @return index to the standard palette
- */
-
- short getIndex();
-
- /**
- * @return triplet representation like that in Excel
- */
-
- short[] getTriplet();
-
- /**
- * @return a hex string exactly like a gnumeric triplet
- */
-
- String getHexString();
}
\ No newline at end of file
String getFontName();
- /**
- * get the index within the HSSFWorkbook (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)
- */
-
- short getIndex();
-
/**
* set the font height in unit's of 1/20th of a point. Maybe you might want to
* use the setFontHeightInPoints which matches to the familiar 10, 12, 14 etc..
*/
short getColor();
- /**
- * set the boldness to use
- * @param boldweight
- * @see #BOLDWEIGHT_NORMAL
- * @see #BOLDWEIGHT_BOLD
- */
-
- void setBoldweight(short boldweight);
-
- /**
- * get the boldness to use
- * @return boldweight
- * @see #BOLDWEIGHT_NORMAL
- * @see #BOLDWEIGHT_BOLD
- */
-
- short getBoldweight();
-
/**
* set normal,super or subscript.
* @param offset type to use (none,super,sub)
* @author Jason Height (jheight at apache.org)
*/
public interface RichTextString {
- /** Place holder for indicating that NO_FONT has been applied here */
- public static final short NO_FONT = 0;
/**
* Applies a font to the specified characters of a string.
int length();
/**
- * Returns the font in use at a particular index.
+ * @return The number of formatting runs used.
*
- * @param index The index.
- * @return The font that's currently being applied at that
- * index or null if no font is being applied or the
- * index is out of range.
- */
- short getFontAtIndex(int index);
-
- /**
- * @return The number of formatting runs used. There will always be at
- * least one of font NO_FONT.
- *
- * @see #NO_FONT
*/
int numFormattingRuns();
*/
int getIndexOfFormattingRun(int index);
- /**
- * Gets the font used in a particular formatting run.
- *
- * @param index the index of the formatting run
- * @return the font number used.
- */
- short getFontOfFormattingRun(int index);
-
/**
* Applies the specified font to the entire string.
*
throw new IOException(e.toString());
}
}
-
+ public static Package openPackage(InputStream is) throws IOException {
+ try {
+ return Package.open(is);
+ } catch (InvalidFormatException e) {
+ throw new IOException(e.toString());
+ }
+ }
+
protected Package getPackage() {
return this.pkg;
}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xssf.dev;\r
+\r
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+\r
+import java.io.FileOutputStream;\r
+\r
+/**\r
+ * Utility which loads a SpreadsheetML file and saves it back.\r
+ * This is a handy tool to investigate read-write round trip safety.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class XSSFSave {\r
+ public static void main(String[] args) throws Exception {\r
+ for (int i = 0; i < args.length; i++) {\r
+ XSSFWorkbook wb = new XSSFWorkbook(args[i]);\r
+\r
+ int sep = args[i].lastIndexOf('.');\r
+ String outfile = args[i].substring(0, sep) + "-save.xlsx";\r
+ FileOutputStream out = new FileOutputStream(outfile);\r
+ wb.write(out);\r
+ out.close();\r
+ }\r
+ }\r
+\r
+}\r
options.setUseDefaultNamespace();
//re-create the sst table every time saving a workbook
- SstDocument doc = SstDocument.Factory.newInstance(options);
+ SstDocument doc = SstDocument.Factory.newInstance();
CTSst sst = doc.addNewSst();
sst.setCount(count);
sst.setUniqueCount(uniqueCount);
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
*/
public class StylesTable implements StylesSource, XSSFModel {
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
- private final LinkedList<CTFont> fonts = new LinkedList<CTFont>();
+ private final ArrayList<CTFont> fonts = new ArrayList<CTFont>();
private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
public int getNumCellStyles(){
return styleXfs.size();
}
-
+ /**
+ * get the size of fonts
+ */
+ public int getNumberOfFonts(){
+ return this.fonts.size();
+ }
/**
* For unit testing only
*/
return fill.putFill(fills);
}
- private long putFont(XSSFFont font, LinkedList<CTFont> fonts) {
+ private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
return font.putFont(fonts);
}
private void initialize() {
XSSFFont font=new XSSFFont(ctFont);
return font;
*/
-
- XSSFFont xssfFont=new 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);
--- /dev/null
+package org.apache.poi.xssf.usermodel;
+
+/**
+ * A deprecated indexing scheme for colours that is still required for some records, and for backwards
+ * compatibility with OLE2 formats.
+ *
+ * <p>
+ * Each element corresponds to a color index (zero-based). When using the default indexed color palette,
+ * the values are not written out, but instead are implied. When the color palette has been modified from default,
+ * then the entire color palette is used.
+ * </p>
+ *
+ * @author Yegor Kozlov
+ */
+public enum IndexedColors {
+
+ BLACK(8),
+ WHITE(9),
+ RED(10),
+ BRIGHT_GREEN(11),
+ BLUE(12),
+ YELLOW(13),
+ PINK(14),
+ TURQUOISE(15),
+ DARK_RED(16),
+ GREEN(17),
+ DARK_BLUE(18),
+ DARK_YELLOW(19),
+ VIOLET(20),
+ TEAL(21),
+ GREY_25_PERCENT(22),
+ GREY_50_PERCENT(23),
+ CORNFLOWER_BLUE(24),
+ MAROON(25),
+ LEMON_CHIFFON(26),
+ ORCHID(28),
+ CORAL(29),
+ ROYAL_BLUE(30),
+ LIGHT_CORNFLOWER_BLUE(31),
+ SKY_BLUE(40),
+ LIGHT_TURQUOISE(41),
+ LIGHT_GREEN(42),
+ LIGHT_YELLOW(43),
+ PALE_BLUE(44),
+ ROSE(45),
+ LAVENDER(46),
+ TAN(47),
+ LIGHT_BLUE(48),
+ AQUA(49),
+ LIME(50),
+ GOLD(51),
+ LIGHT_ORANGE(52),
+ ORANGE(53),
+ BLUE_GREY(54),
+ GREY_40_PERCENT(55),
+ DARK_TEAL(56),
+ SEA_GREEN(57),
+ DARK_GREEN(58),
+ OLIVE_GREEN(59),
+ BROWN(60),
+ PLUM(61),
+ INDIGO(62),
+ GREY_80_PERCENT(63);
+
+ private short index;
+
+ IndexedColors(int idx){
+ index = (short)idx;
+ }
+
+ /**
+ * Returns index of this color
+ *
+ * @return index of this color
+ */
+ public short getIndex(){
+ return index;
+ }
+}
}
+ public void setCellValue(String str) {
+ this.setCellValue(new XSSFRichTextString(str));
+ }
+
public void setCellValue(RichTextString value) {
if(this.cell.getT() == STCellType.INLINE_STR) {
this.cell.setV(value.getString());
this.cell.setT(STCellType.S);
}
XSSFRichTextString rt = (XSSFRichTextString)value;
+ rt.setStylesTableReference(stylesSource);
int sRef = sharedStringSource.addEntry(rt.getCTRst());
this.cell.setV(Integer.toString(sRef));
}
}
}
- /**
- * Creates an XSSFRichTextString for you.
- */
- public RichTextString createRichTextString(String text) {
- return new XSSFRichTextString(text);
- }
-
public Hyperlink getHyperlink() {
return row.getSheet().getHyperlink(row.getRowNum(), cellNum);
}
return (short) getBorderColor(side).getIndexed();
}
- private void setBorderColorIndexed(BorderSide side, long color) {
+ private void setBorderColorIndexed(BorderSide side, int color) {
getBorderColor(side).setIndexed(color);
}
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.xssf.usermodel;\r
-\r
-import java.util.LinkedList;\r
-\r
-import org.apache.poi.ss.usermodel.Font;\r
-\r
-import org.apache.poi.xssf.util.CTFontWrapper;\r
-import org.apache.poi.xssf.util.Charset;\r
-import org.apache.poi.xssf.util.IndexedColors;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;\r
-\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontName;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontScheme;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFontSize;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTIntProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTUnderlineProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTVerticalAlignFontProperty;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFontScheme;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;\r
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;\r
-\r
-public class XSSFFont implements Font {\r
-\r
-\r
- public static final int SCHEME_MAJOR=2;\r
- public static final int SCHEME_MINOR=3;\r
- public static final int SCHEME_NONE=0;\r
-\r
- public static final int FONT_FAMILY_NOT_APPLICABLE=0;\r
- public static final int FONT_FAMILY_ROMAN=1;\r
- public static final int FONT_FAMILY_SWISS=2;\r
- public static final int FONT_FAMILY_MODERN=3;\r
- public static final int FONT_FAMILY_SCRIPT=4;\r
- public static final int FONT_FAMILY_DECORATIVE=5;\r
-\r
-\r
- public static final String DEFAULT_FONT_NAME="Calibri";\r
- public static final short DEFAULT_FONT_SIZE=11;\r
- public static final short DEFAULT_FONT_COLOR=(short)IndexedColors.BLACK;\r
-\r
- private int index=0;\r
-\r
-\r
- private CTFontWrapper fontWrapper;\r
-\r
-\r
-\r
- public XSSFFont(CTFont font) {\r
- this.fontWrapper=new CTFontWrapper(font);\r
- }\r
-\r
- /* \r
- public XSSFFont(int index) {\r
- this.fontWrapper=new CTFontWrapper(font);\r
- this.index=index;\r
- }\r
- */\r
-\r
- public XSSFFont() {\r
- this.fontWrapper = new CTFontWrapper(CTFont.Factory.newInstance());\r
- }\r
-\r
-\r
- public CTFont getCTFont(){\r
- return fontWrapper.getCTFont();\r
- }\r
-\r
-\r
- public short getBoldweight() {\r
- CTBooleanProperty bold=fontWrapper.getB();\r
- if(bold!=null && bold.getVal())\r
- return Font.BOLDWEIGHT_BOLD;\r
- else\r
- return Font.BOLDWEIGHT_NORMAL;\r
- } \r
-\r
-\r
-\r
- public byte getCharSet() {\r
- CTIntProperty charset= fontWrapper.getCharset();\r
- if(charset!=null){\r
- //this value must be set -- can't be null\r
- switch (charset.getVal()) {\r
- case Charset.ANSI_CHARSET:\r
- return Font.ANSI_CHARSET;\r
-\r
- case Charset.DEFAULT_CHARSET:\r
- return Font.DEFAULT_CHARSET;\r
-\r
- case Charset.SYMBOL_CHARSET:\r
- return Font.SYMBOL_CHARSET;\r
-\r
- default://maight be correct to return this byte value???\r
- return Byte.parseByte(Integer.toString(charset.getVal()));\r
- }\r
- }\r
- else\r
- return Font.ANSI_CHARSET;\r
- }\r
-\r
- public short getColor() {\r
- CTColor color=fontWrapper.getColor();\r
- long index=color.getIndexed();\r
- if (index==XSSFFont.DEFAULT_FONT_COLOR){\r
- return Font.COLOR_NORMAL;\r
- }\r
- else if(index==IndexedColors.RED){\r
- return Font.COLOR_RED;\r
- }\r
- else{\r
- return Short.parseShort(new Long(index).toString());\r
- }\r
- }\r
-\r
- public short getFontHeight() {\r
- if(fontWrapper.getSz()!=null){\r
- double fontHeight= fontWrapper.getSz().getVal()/20;\r
- return (short)fontHeight;\r
- }\r
- else\r
- return DEFAULT_FONT_SIZE/20;\r
- }\r
-\r
- public short getFontHeightInPoints() {\r
- if(fontWrapper.getSz()!=null){\r
- double fontHeight= fontWrapper.getSz().getVal();// /72;\r
- return (short)fontHeight;//new Double(fontHeight).shortValue();\r
- }\r
- else\r
- return DEFAULT_FONT_SIZE;\r
- }\r
-\r
- //AGGIUNGERE CONTROLLO NULL\r
- public String getFontName() { \r
- if(fontWrapper.getName()!=null)\r
- return fontWrapper.getName().getVal();\r
- else\r
- return DEFAULT_FONT_NAME;\r
- }\r
-\r
-\r
- public short getIndex() {\r
- // TODO Auto-generated method stub\r
- return 0;\r
- }\r
-\r
- public boolean getItalic() {\r
- if(fontWrapper.getI()!=null)\r
- return fontWrapper.getI().getVal();\r
- else\r
- return false;\r
- }\r
-\r
- public boolean getStrikeout() {\r
- if(fontWrapper.getStrike()!=null)\r
- return fontWrapper.getStrike().getVal();\r
- else\r
- return false;\r
- }\r
-\r
- public short getTypeOffset() {\r
- if(fontWrapper.getVertAlign()!=null){\r
- int val=fontWrapper.getVertAlign().getVal().intValue();\r
- switch (val) {\r
- case STVerticalAlignRun.INT_BASELINE:\r
- return Font.SS_NONE;\r
- case STVerticalAlignRun.INT_SUBSCRIPT:\r
- return Font.SS_SUB;\r
- case STVerticalAlignRun.INT_SUPERSCRIPT:\r
- return Font.SS_SUPER;\r
- default: throw new RuntimeException("Wrong offset value "+val);\r
- }\r
- }\r
- else\r
- return Font.SS_NONE;\r
- }\r
-\r
- public byte getUnderline() { \r
- if(fontWrapper.getU()!=null){\r
- //attenzione : -- get val pu\98 tornare null----\r
- switch (fontWrapper.getU().getVal().intValue()) {\r
- case STUnderlineValues.INT_DOUBLE:\r
- return Font.U_DOUBLE; \r
- case STUnderlineValues.INT_DOUBLE_ACCOUNTING:\r
- return Font.U_DOUBLE_ACCOUNTING; \r
-\r
- case STUnderlineValues.INT_SINGLE_ACCOUNTING:\r
- return Font.U_SINGLE_ACCOUNTING; \r
-\r
- case STUnderlineValues.INT_NONE:\r
- return Font.U_NONE;\r
-\r
- case STUnderlineValues.INT_SINGLE: \r
- default:\r
- return Font.U_SINGLE;\r
- }\r
- }\r
- return Font.U_NONE;\r
- }\r
-\r
- public void setBoldweight(short boldweight) { \r
- if(boldweight==Font.BOLDWEIGHT_BOLD){\r
-\r
- CTBooleanProperty bold;\r
- if(fontWrapper.getCTFont().getBArray().length==0){\r
- bold=fontWrapper.getCTFont().addNewB();\r
- }\r
- else{\r
- bold=CTBooleanProperty.Factory.newInstance();\r
- }\r
- bold.setVal(true);\r
- fontWrapper.setB(bold);\r
- }\r
- }\r
-\r
- public void setCharSet(byte charset) {\r
- CTIntProperty charsetProperty;\r
- if(fontWrapper.getCTFont().getCharsetArray().length==0){\r
- charsetProperty=fontWrapper.getCTFont().addNewCharset();\r
- }\r
- else{\r
- charsetProperty=CTIntProperty.Factory.newInstance();\r
- }\r
- switch (charset) {\r
- case Font.ANSI_CHARSET:\r
- charsetProperty.setVal(Charset.ANSI_CHARSET); \r
- break;\r
- case Font.SYMBOL_CHARSET:\r
- charsetProperty.setVal(Charset.SYMBOL_CHARSET); \r
- break;\r
- case Font.DEFAULT_CHARSET:\r
- charsetProperty.setVal(Charset.DEFAULT_CHARSET); \r
- break;\r
- default:\r
- throw new RuntimeException("Attention: an attempt to set a type of unknow charset and charset");\r
- }\r
-\r
- fontWrapper.setCharset(charsetProperty);\r
- }\r
-\r
-\r
- public void setColor(short color) {\r
- CTColor ctColor;\r
- if(fontWrapper.getCTFont().getColorArray().length==0){\r
- ctColor=fontWrapper.getCTFont().addNewColor();\r
- }\r
- else{\r
- ctColor=CTColor.Factory.newInstance();\r
- }\r
- \r
- switch (color) {\r
- case Font.COLOR_NORMAL:{\r
- ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);\r
- break;\r
- }\r
- case Font.COLOR_RED:{\r
- ctColor.setIndexed(IndexedColors.RED);\r
- break;\r
- }\r
- default:\r
- ctColor.setIndexed(color);\r
- }\r
-\r
- fontWrapper.setColor(ctColor);\r
- }\r
-\r
-\r
-\r
- public void setFontHeight(short height) {\r
- CTFontSize fontSize;\r
- if(fontWrapper.getCTFont().getSzArray().length==0){\r
- fontSize=fontWrapper.getCTFont().addNewSz();\r
- }\r
- else{\r
- fontSize=CTFontSize.Factory.newInstance();\r
- }\r
- fontSize.setVal(height*20);\r
- fontWrapper.setSz(fontSize);\r
- }\r
-\r
-\r
- public void setFontHeightInPoints(short height) {\r
- CTFontSize fontSize;\r
- if(fontWrapper.getCTFont().getSzArray().length==0){\r
- fontSize=fontWrapper.getCTFont().addNewSz();\r
- }\r
- else{\r
- fontSize=CTFontSize.Factory.newInstance();\r
- }\r
-\r
- fontSize.setVal(height);\r
- fontWrapper.setSz(fontSize);\r
- }\r
-\r
-\r
-\r
- public void setFontName(String name) {\r
- CTFontName fontName;\r
- if(fontWrapper.getCTFont().getNameArray().length==0){\r
- fontName=fontWrapper.getCTFont().addNewName();\r
- }\r
- else{\r
- fontName=CTFontName.Factory.newInstance();\r
- }\r
-\r
- fontName.setVal(name);\r
- fontWrapper.setName(fontName); \r
- }\r
- \r
-\r
- public void setItalic(boolean italic) {\r
- CTBooleanProperty bool;\r
- if(fontWrapper.getCTFont().getIArray().length==0){\r
- bool=fontWrapper.getCTFont().addNewI(); \r
- }\r
- else{\r
- bool=CTBooleanProperty.Factory.newInstance();\r
- }\r
-\r
- bool.setVal(italic);\r
- fontWrapper.setI(bool);\r
- }\r
-\r
- public void setStrikeout(boolean strikeout) {\r
- CTBooleanProperty strike;\r
- if(fontWrapper.getCTFont().getStrikeArray().length==0){\r
- strike=fontWrapper.getCTFont().addNewStrike();\r
- }\r
- else{\r
- strike=CTBooleanProperty.Factory.newInstance();\r
- }\r
- strike.setVal(strikeout);\r
- fontWrapper.setStrike(strike);\r
- }\r
-\r
- public void setTypeOffset(short offset) {\r
- CTVerticalAlignFontProperty offsetProperty;\r
- if(fontWrapper.getCTFont().getVertAlignArray().length==0){\r
- offsetProperty=fontWrapper.getCTFont().addNewVertAlign(); \r
- }\r
- else{\r
- offsetProperty=CTVerticalAlignFontProperty.Factory.newInstance();\r
- }\r
- switch (offset) {\r
- case Font.SS_NONE:\r
- offsetProperty.setVal(STVerticalAlignRun.BASELINE);\r
- break;\r
- case Font.SS_SUB:\r
- offsetProperty.setVal(STVerticalAlignRun.SUBSCRIPT);\r
- break;\r
- case Font.SS_SUPER:\r
- offsetProperty.setVal(STVerticalAlignRun.SUPERSCRIPT);\r
- break;\r
- }\r
- fontWrapper.setVertAlign(offsetProperty);\r
- }\r
-\r
- public void setUnderline(byte underline) {\r
- CTUnderlineProperty ctUnderline;\r
- if(fontWrapper.getCTFont().getUArray().length==0){\r
- ctUnderline=fontWrapper.getCTFont().addNewU();\r
- }\r
- else{\r
- ctUnderline=CTUnderlineProperty.Factory.newInstance();\r
- }\r
- switch (underline) {\r
- case Font.U_DOUBLE:\r
- ctUnderline.setVal(STUnderlineValues.DOUBLE); \r
- break;\r
- case Font.U_DOUBLE_ACCOUNTING:\r
- ctUnderline.setVal(STUnderlineValues.DOUBLE_ACCOUNTING); \r
- break;\r
- case Font.U_SINGLE_ACCOUNTING:\r
- ctUnderline.setVal(STUnderlineValues.SINGLE_ACCOUNTING); \r
- break;\r
- case Font.U_NONE:\r
- ctUnderline.setVal(STUnderlineValues.NONE); \r
- break;\r
-\r
- case Font.U_SINGLE:\r
- default:\r
- ctUnderline.setVal(STUnderlineValues.SINGLE);\r
- break;\r
- }\r
-\r
- fontWrapper.setU(ctUnderline);\r
- }\r
-\r
-\r
- public long putFont(LinkedList<CTFont> fonts) {\r
- //TODO\r
- /*\r
- * we need to implement a method equals to check that 2 instances of CTFont\r
- * are different by comparison of all font attributes.\r
- * NB: take a look to findFont method in XSSFWorkbook\r
- */\r
- CTFont font=fontWrapper.getCTFont();\r
- if(fonts.contains(font)) {\r
- return fonts.indexOf(font);\r
- }\r
- fonts.add(font);\r
- return fonts.size() - 1;\r
- }\r
-\r
- // solo di xssfFont - non di Font-\r
- //sono utilizzati nel metodo createDefaultFont in StylesTable insta.\r
-\r
- public int getScheme(){\r
- int fontScheme = fontWrapper.getFontScheme().getVal().intValue();\r
- switch (fontScheme) {\r
- case STFontScheme.INT_MAJOR:\r
- return XSSFFont.SCHEME_MAJOR; \r
- case STFontScheme.INT_MINOR:\r
- return XSSFFont.SCHEME_MINOR;\r
- case STFontScheme.INT_NONE:\r
- return XSSFFont.SCHEME_NONE;\r
-\r
- default:\r
- return fontScheme;\r
- } \r
- }\r
-\r
-\r
- public void setScheme(int scheme){\r
- CTFontScheme ctFontScheme;\r
- if(fontWrapper.getCTFont().getSchemeArray().length==0){\r
- ctFontScheme=fontWrapper.getCTFont().addNewScheme();\r
- }\r
- else{\r
- ctFontScheme=CTFontScheme.Factory.newInstance();\r
- }\r
- switch (scheme) {\r
- case XSSFFont.SCHEME_MAJOR:\r
- ctFontScheme.setVal(STFontScheme.MAJOR);\r
- break;\r
- case XSSFFont.SCHEME_MINOR:\r
- ctFontScheme.setVal(STFontScheme.MINOR);\r
- break;\r
- case XSSFFont.SCHEME_NONE:\r
- ctFontScheme.setVal(STFontScheme.NONE);\r
- break;\r
- default:\r
- throw new RuntimeException("Schema value ["+ scheme +"] not supported in XSSFFont");\r
- }\r
-\r
- fontWrapper.setFontScheme(ctFontScheme);\r
- } \r
-\r
-\r
-\r
- public int getFamily(){\r
- if(fontWrapper.getFamily()!=null)\r
- return fontWrapper.getFamily().getVal();\r
- else\r
- return XSSFFont.FONT_FAMILY_SWISS;\r
- }\r
-\r
- public void setFamily(int value){\r
- //TODO\r
- CTIntProperty family;\r
- if(fontWrapper.getCTFont().getSchemeArray().length==0){\r
- family=fontWrapper.getCTFont().addNewFamily();\r
- }\r
- else{\r
- family=CTIntProperty.Factory.newInstance();\r
- }\r
- family.setVal(value);\r
- //fontWrapper.setFamily\r
- } \r
-\r
-\r
-\r
-}\r
+/* ====================================================================
+ 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;
+
+import org.apache.poi.ss.usermodel.Font;
+
+import org.apache.poi.xssf.util.Charset;
+import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
+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;
+
+import java.util.ArrayList;
+
+public class XSSFFont implements 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 = IndexedColors.BLACK.getIndex();
+
+ private CTFont ctFont;
+
+ public XSSFFont(CTFont font) {
+ this.ctFont=font;
+ }
+
+ protected XSSFFont() {
+ this.ctFont = CTFont.Factory.newInstance();
+ }
+
+
+ public CTFont getCTFont(){
+ return ctFont;
+ }
+
+ /**
+ *
+ */
+ public boolean getBold() {
+ CTBooleanProperty bold=ctFont.sizeOfBArray() == 0 ? null : ctFont.getBArray(0);
+ return (bold!=null && bold.getVal());
+ }
+
+
+
+ public byte getCharSet() {
+ CTIntProperty charset= ctFont.sizeOfCharsetArray() == 0?null:ctFont.getCharsetArray(0);
+ 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() {
+ CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+ if(color == null) return Font.COLOR_NORMAL;
+
+ long index=color.getIndexed();
+ if (index==XSSFFont.DEFAULT_FONT_COLOR){
+ return Font.COLOR_NORMAL;
+ }
+ else if(index == IndexedColors.RED.getIndex()){
+ return Font.COLOR_RED;
+ }
+ else{
+ return Short.parseShort(new Long(index).toString());
+ }
+ }
+
+
+ public byte[] getRgbColor() {
+ CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+ return color.getRgb();
+ }
+
+ public short getThemeColor() {
+ CTColor color=ctFont.sizeOfColorArray()==0?null: ctFont.getColorArray(0);
+ long index=color.getTheme();
+ return (short)index;
+ }
+
+
+ public short getFontHeight() {
+ CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0);
+ if(size!=null){
+ double fontHeight= size.getVal()/20;
+ return (short)fontHeight;
+ }
+ else
+ return DEFAULT_FONT_SIZE/20;
+ }
+
+ public short getFontHeightInPoints() {
+ CTFontSize size=ctFont.sizeOfSzArray()==0?null: ctFont.getSzArray(0);
+ if(size!=null){
+ double fontHeight= size.getVal();
+ return (short)fontHeight;
+ }
+ else
+ return DEFAULT_FONT_SIZE;
+ }
+
+
+ public String getFontName() {
+ CTFontName name=ctFont.sizeOfNameArray()==0?null:ctFont.getNameArray(0);
+ return name==null? null:name.getVal();
+ }
+
+
+ public boolean getItalic() {
+ CTBooleanProperty italic=ctFont.sizeOfIArray()==0?null:ctFont.getIArray(0);
+ return italic!=null && italic.getVal();
+ }
+
+ public boolean getStrikeout() {
+ CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?null:ctFont.getStrikeArray(0);
+ return strike!=null && strike.getVal();
+ }
+
+ public short getTypeOffset() {
+ CTVerticalAlignFontProperty vAlign=ctFont.sizeOfVertAlignArray()==0?null:ctFont.getVertAlignArray(0);
+ if(vAlign!=null){
+ int val=vAlign.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() {
+ CTUnderlineProperty underline=ctFont.sizeOfUArray()==0?null:ctFont.getUArray(0);
+ if(underline!=null){
+ switch (underline.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;
+ }
+
+ /**
+ * Set characters in bold face font style.
+ * If omitted, the default value is true.
+ */
+ public void setBold(boolean bold) {
+ CTBooleanProperty ctBold=ctFont.sizeOfBArray()==0?ctFont.addNewB():ctFont.getBArray(0);
+ ctBold.setVal(true);
+ }
+
+ /**
+ *
+ */
+ public void setCharSet(byte charset) {
+ CTIntProperty charsetProperty=ctFont.sizeOfCharsetArray()==0?ctFont.addNewCharset():ctFont.getCharsetArray(0);
+ 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");
+ }
+ }
+
+
+ public void setColor(short color) {
+ CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+
+ switch (color) {
+ case Font.COLOR_NORMAL:{
+ ctColor.setIndexed(XSSFFont.DEFAULT_FONT_COLOR);
+ break;
+ }
+ case Font.COLOR_RED:{
+ ctColor.setIndexed(IndexedColors.RED.getIndex());
+ break;
+ }
+ default:
+ ctColor.setIndexed(color);
+ }
+ }
+
+
+
+ public void setFontHeight(short height) {
+ CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0);
+ fontSize.setVal(height*20);
+ }
+
+
+ public void setFontHeightInPoints(short height) {
+ CTFontSize fontSize=ctFont.sizeOfSzArray()==0?ctFont.addNewSz():ctFont.getSzArray(0);
+ fontSize.setVal(height);
+ }
+
+
+
+ public void setRgbColor(XSSFColor color) {
+ CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+ ctColor.setRgb(color.getRgb());
+ }
+
+ public void setThemeColor(short theme) {
+ CTColor ctColor=ctFont.sizeOfColorArray()==0?ctFont.addNewColor():ctFont.getColorArray(0);
+ ctColor.setTheme(theme);
+ }
+
+ public void setFontName(String name) {
+ CTFontName fontName=ctFont.sizeOfNameArray()==0?ctFont.addNewName():ctFont.getNameArray(0);
+ fontName.setVal(name);
+ }
+
+ public void setItalic(boolean italic) {
+ CTBooleanProperty bool=ctFont.sizeOfIArray()==0?ctFont.addNewI():ctFont.getIArray(0);
+ bool.setVal(italic);
+ }
+
+ public void setStrikeout(boolean strikeout) {
+ CTBooleanProperty strike=ctFont.sizeOfStrikeArray()==0?ctFont.addNewStrike():ctFont.getStrikeArray(0);
+ strike.setVal(strikeout);
+ }
+
+ public void setTypeOffset(short offset) {
+ CTVerticalAlignFontProperty offsetProperty=ctFont.sizeOfVertAlignArray()==0?ctFont.addNewVertAlign(): ctFont.getVertAlignArray(0);
+ 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;
+ }
+ }
+
+ public void setUnderline(byte underline) {
+ CTUnderlineProperty ctUnderline=ctFont.sizeOfUArray()==0?ctFont.addNewU():ctFont.getUArray(0);
+ 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;
+ }
+ }
+
+
+ public long putFont(ArrayList<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
+ */
+ if(fonts.contains(ctFont)) {
+ return fonts.indexOf(ctFont);
+ }
+ fonts.add(ctFont);
+ return fonts.size() - 1;
+ }
+
+ // solo di xssfFont - non di Font-
+ //sono utilizzati nel metodo createDefaultFont in StylesTable insta.
+
+ public int getScheme(){
+ CTFontScheme scheme=ctFont.sizeOfSchemeArray()==0?null:ctFont.getSchemeArray(0);
+ if(scheme!=null){
+ int fontScheme = scheme.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;
+ }
+ }
+ return 0;
+ }
+
+
+ public void setScheme(int scheme){
+ CTFontScheme ctFontScheme=ctFont.sizeOfSchemeArray()==0?ctFont.addNewScheme():ctFont.getSchemeArray(0);
+ 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");
+ }
+ }
+
+
+ public int getFamily(){
+ CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0);
+ if(family!=null)
+ return family.getVal();
+ else
+ return XSSFFont.FONT_FAMILY_SWISS;
+ }
+
+ public void setFamily(int value){
+ CTIntProperty family=ctFont.sizeOfFamilyArray()==0?ctFont.addNewFamily():ctFont.getFamilyArray(0);
+ family.setVal(value);
+ }
+
+}
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.model.StylesTable;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+
+import java.util.ArrayList;
/**
* <p>
* Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the
* same formatting applied. In these cases, the formatting for the cell is stored in the styles part,
- * and the string for the cell can be shared across the workbook. The following xml and code snippet illustrate the example.
+ * and the string for the cell can be shared across the workbook. The following code illustrates the example.
* </p>
*
* <blockquote>
* <pre>
- * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main
- * count="1" uniqueCount="1">
- * <si>
- * <t>Apache POI</t>
- * </si>
- * </sst>
- * </pre>
- * </blockquote>
- *
- * The code to produce xml above:
- * <blockquote>
- * <pre>
* cell1.setCellValue(new XSSFRichTextString("Apache POI"));
* cell2.setCellValue(new XSSFRichTextString("Apache POI"));
* cell3.setCellValue(new XSSFRichTextString("Apache POI"));
* </pre>
* </blockquote>
*
- * The code above will produce the following xml:
- * <blockquote>
- * <pre>
- * <sst xmlns=http://schemas.openxmlformats.org/spreadsheetml/2006/5/main count="2" uniqueCount="2">
- * <si>
- * <r>
- * <rPr>
- * <b/>
- * <sz val="11"/>
- * <color theme="1"/>
- * <rFont val="Arial"/>
- * <family val="2"/>
- * <scheme val="minor"/>
- * </rPr>
- * <t>Apache POI</t>
- * </r>
- * </si>
- * <si>
- * <r>
- * <rPr>
- * <i/>
- * <sz val="11"/>
- * <color theme="1"/>
- * <rFont val="Courier"/>
- * <family val="1"/>
- * <scheme val="minor"/>
- * </rPr>
- * <t>Apache POI</t>
- * </r>
- * </si>
- *</sst>
- *
- * </pre>
- * </blockquote>
*
* @author Yegor Kozlov
*/
public class XSSFRichTextString implements RichTextString {
private CTRst st;
private StylesTable styles;
+ private ArrayList<CTRPrElt> fontIdRuns;
+ /**
+ * Create a rich text string and initialize it with empty string
+ */
public XSSFRichTextString(String str) {
st = CTRst.Factory.newInstance();
st.setT(str);
}
+ /**
+ * Create empty rich text string
+ */
public XSSFRichTextString() {
st = CTRst.Factory.newInstance();
}
+ /**
+ * Create a rich text string from the supplied XML bean
+ */
public XSSFRichTextString(CTRst st) {
this.st = st;
}
* @param fontIndex The font to use.
*/
public void applyFont(int startIndex, int endIndex, short fontIndex) {
- // TODO Auto-generated method stub
-
+ XSSFFont font;
+ if(styles == null) {
+ //style table is not set, remember fontIndex and set the run properties later,
+ //when setStylesTableReference is called
+ font = new XSSFFont();
+ font.setFontName("#" + fontIndex);
+ fontIdRuns = new ArrayList<CTRPrElt>();
+ } else {
+ font = (XSSFFont)styles.getFontAt(fontIndex);
+ }
+ applyFont(startIndex, endIndex, font);
}
/**
* @param font The index of the font to use.
*/
public void applyFont(int startIndex, int endIndex, Font font) {
- applyFont(0, length(), font.getIndex());
+ if (startIndex > endIndex)
+ throw new IllegalArgumentException("Start index must be less than end index.");
+ if (startIndex < 0 || endIndex > length())
+ throw new IllegalArgumentException("Start and end index not in range.");
+ if (startIndex == endIndex)
+ return;
+
+ if(st.sizeOfRArray() == 0 && st.isSetT()) {
+ //convert <t>string</t> into a text run: <r><t>string</t></r>
+ st.addNewR().setT(st.getT());
+ st.unsetT();
+ }
+
+ String text = getString();
+
+ XSSFFont xssfFont = (XSSFFont)font;
+ ArrayList<CTRElt> runs = new ArrayList<CTRElt>();
+
+ int pos = 0;
+ int i;
+ for (i = 0; i < st.sizeOfRArray(); i++) {
+ CTRElt r = st.getRArray(i);
+
+ int len = r.getT().length();
+ int p1 = pos;
+ int p2 = pos + len;
+ if(startIndex > p2) {
+ runs.add(r);
+ } else if (startIndex >= p1 && startIndex < p2){
+ String t = r.getT();
+ r.setT(t.substring(0, startIndex-p1));
+ runs.add(r);
+ } else {
+ break;
+ }
+ pos = p2;
+ }
+ CTRElt r = CTRElt.Factory.newInstance();
+ r.setT(text.substring(startIndex, endIndex));
+ CTRPrElt pr = r.addNewRPr();
+ setRunAttributes(xssfFont.getCTFont(), pr);
+ if(fontIdRuns != null) fontIdRuns.add(pr);
+ runs.add(r);
+
+ for (; i < st.sizeOfRArray(); i++) {
+ r = st.getRArray(i);
+
+ int len = r.getT().length();
+ int p1 = pos;
+ int p2 = pos + len;
+ if(endIndex > p2) {
+ ;
+ } else if (endIndex >= p1 && endIndex < p2){
+ String t = r.getT();
+ r.setT(t.substring(endIndex-p1, len));
+ runs.add(r);
+ } else {
+ runs.add(r);
+ }
+ pos = p2;
+ }
+
+ st.setRArray(runs.toArray(new CTRElt[runs.size()]));
}
/**
* @param font The font to use.
*/
public void applyFont(Font font) {
- applyFont(0, length(), font);
+ if(st.sizeOfRArray() == 0 && st.isSetT()) {
+ CTRElt r = st.addNewR();
+ r.setT(st.getT());
+ setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
+ st.unsetT();
+ } else {
+ CTRElt r = CTRElt.Factory.newInstance();
+ r.setT(getString());
+ setRunAttributes(((XSSFFont)font).getCTFont(), r.addNewRPr());
+ st.setRArray(new CTRElt[]{r});
+ }
+
+ if(fontIdRuns != null) fontIdRuns.add(st.getRArray(0).getRPr());
+
}
/**
* @param fontIndex the font to apply.
*/
public void applyFont(short fontIndex) {
- applyFont(0, length(), fontIndex);
+ XSSFFont font;
+ if(styles == null) {
+ font = new XSSFFont();
+ font.setFontName("#" + fontIndex);
+ fontIdRuns = new ArrayList<CTRPrElt>();
+ } else {
+ font = (XSSFFont)styles.getFontAt(fontIndex);
+ }
+ applyFont(font);
}
/**
- * Removes any formatting that may have been applied to the string.
+ * Append new text to this text run and apply the specify font to it
+ *
+ * @param text the text to append
+ * @param font the font to apply to the appended text or <code>null</code> if no formatting is required
*/
- public void clearFormatting() {
- for (int i = 0; i < st.sizeOfRArray(); i++) {
- st.removeR(i);
+ public void append(String text, XSSFFont font){
+ if(st.sizeOfRArray() == 0 && st.isSetT()) {
+ //convert <t>string</t> into a text run: <r><t>string</t></r>
+ st.addNewR().setT(st.getT());
+ st.unsetT();
}
+ CTRElt lt = st.addNewR();
+ lt.setT(text);
+ CTRPrElt pr = lt.addNewRPr();
+ if(font != null) setRunAttributes(font.getCTFont(), pr);
+
+ if(fontIdRuns != null) fontIdRuns.add(pr);
}
/**
- * Returns the font in use at a particular index.
+ * Append new text to this text run
*
- * @param index The index.
- * @return The font that's currently being applied at that
- * index or null if no font is being applied or the
- * index is out of range.
+ * @param text the text to append
*/
- public short getFontAtIndex(int index) {
- // TODO Auto-generated method stub
- return 0;
+ public void append(String text){
+ append(text, null);
}
/**
- * Gets the font used in a particular formatting run.
- *
- * @param index the index of the formatting run
- * @return the font number used.
+ * Copy font attributes from CTFont bean into CTRPrElt bean
*/
- public short getFontOfFormattingRun(int index) {
- // TODO Auto-generated method stub
- return 0;
+ private void setRunAttributes(CTFont ctFont, CTRPrElt pr){
+ if(ctFont.sizeOfBArray() > 0) pr.addNewB().setVal(ctFont.getBArray(0).getVal());
+ if(ctFont.sizeOfUArray() > 0) pr.addNewU().setVal(ctFont.getUArray(0).getVal());
+ if(ctFont.sizeOfIArray() > 0) pr.addNewI().setVal(ctFont.getIArray(0).getVal());
+ if(ctFont.sizeOfColorArray() > 0) {
+ CTColor c1 = ctFont.getColorArray(0);
+ CTColor c2 = pr.addNewColor();
+ if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
+ if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
+ if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
+ if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
+ if(c1.isSetTint()) c2.setTint(c1.getTint());
+ }
+ if(ctFont.sizeOfNameArray() > 0) pr.addNewRFont().setVal(ctFont.getNameArray(0).getVal());
+ if(ctFont.sizeOfFamilyArray() > 0) pr.addNewFamily().setVal(ctFont.getFamilyArray(0).getVal());
+ if(ctFont.sizeOfSchemeArray() > 0) pr.addNewScheme().setVal(ctFont.getSchemeArray(0).getVal());
+ if(ctFont.sizeOfCharsetArray() > 0) pr.addNewCharset().setVal(ctFont.getCharsetArray(0).getVal());
+ if(ctFont.sizeOfCondenseArray() > 0) pr.addNewCondense().setVal(ctFont.getCondenseArray(0).getVal());
+ if(ctFont.sizeOfExtendArray() > 0) pr.addNewExtend().setVal(ctFont.getExtendArray(0).getVal());
+ if(ctFont.sizeOfVertAlignArray() > 0) pr.addNewVertAlign().setVal(ctFont.getVertAlignArray(0).getVal());
+ if(ctFont.sizeOfOutlineArray() > 0) pr.addNewOutline().setVal(ctFont.getOutlineArray(0).getVal());
+ if(ctFont.sizeOfShadowArray() > 0) pr.addNewShadow().setVal(ctFont.getShadowArray(0).getVal());
+ if(ctFont.sizeOfStrikeArray() > 0) pr.addNewStrike().setVal(ctFont.getStrikeArray(0).getVal());
+ }
+
+ /**
+ * Removes any formatting that may have been applied to the string.
+ */
+ public void clearFormatting() {
+ String text = getString();
+ while (st.sizeOfRArray() > 0) {
+ st.removeR(st.sizeOfRArray()-1);
+ }
+ st.setT(text);
}
/**
* The index within the string to which the specified formatting run applies.
+ *
* @param index the index of the formatting run
* @return the index within the string.
*/
public int getIndexOfFormattingRun(int index) {
- // TODO Auto-generated method stub
- return 0;
+ if(st.sizeOfRArray() == 0) return 0;
+
+ int pos = 0;
+ for(int i = 0; i < st.sizeOfRArray(); i++){
+ CTRElt r = st.getRArray(i);
+ if(i == index) return pos;
+
+ pos += r.getT().length();
+ }
+ return -1;
+ }
+
+ /**
+ * Returns the number of characters this format run covers.
+ *
+ * @param index the index of the formatting run
+ * @return the number of characters this format run covers
+ */
+ public int getLengthOfFormattingRun(int index) {
+ if(st.sizeOfRArray() == 0) return length();
+
+ for(int i = 0; i < st.sizeOfRArray(); i++){
+ CTRElt r = st.getRArray(i);
+ if(i == index) return r.getT().length();
+ }
+ return -1;
}
/**
return st.sizeOfRArray();
}
+ /**
+ * Gets a copy of the font used in a particular formatting run.
+ *
+ * @param index the index of the formatting run
+ * @return A copy of the font used or null if no formatting is applied to the specified text run.
+ */
+ public XSSFFont getFontOfFormattingRun(int index) {
+ if(st.sizeOfRArray() == 0) return null;
+
+ for(int i = 0; i < st.sizeOfRArray(); i++){
+ CTRElt r = st.getRArray(i);
+ if(i == index) return new XSSFFont(toCTFont(r.getRPr()));
+ }
+ return null;
+ }
+
+ /**
+ * Return a copy of the font in use at a particular index.
+ *
+ * @param index The index.
+ * @return A copy of the font that's currently being applied at that
+ * index or null if no font is being applied or the
+ * index is out of range.
+ */
+ public XSSFFont getFontAtIndex( int index ) {
+ if(st.sizeOfRArray() == 0) return null;
+
+ int pos = 0;
+ for(int i = 0; i < st.sizeOfRArray(); i++){
+ CTRElt r = st.getRArray(i);
+ if(index >= pos && index < pos + r.getT().length()) return new XSSFFont(toCTFont(r.getRPr()));
+
+ pos += r.getT().length();
+ }
+ return null;
+
+ }
+
/**
* Return the underlying xml bean
*/
protected void setStylesTableReference(StylesTable tbl){
styles = tbl;
+ if(fontIdRuns != null){
+ for (CTRPrElt pr : fontIdRuns) {
+ if(pr.sizeOfRFontArray() > 0 ) {
+ String fontName = pr.getRFontArray(0).getVal();
+ if(fontName.startsWith("#")){
+ int idx = Integer.parseInt(fontName.substring(1));
+ XSSFFont font = (XSSFFont)styles.getFontAt(idx);
+ pr.removeRFont(0);
+ setRunAttributes(font.getCTFont(), pr);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ *
+ * CTRPrElt --> CTFont adapter
+ */
+ protected static CTFont toCTFont(CTRPrElt pr){
+ CTFont ctFont = CTFont.Factory.newInstance();
+
+ if(pr.sizeOfBArray() > 0) ctFont.addNewB().setVal(pr.getBArray(0).getVal());
+ if(pr.sizeOfUArray() > 0) ctFont.addNewU().setVal(pr.getUArray(0).getVal());
+ if(pr.sizeOfIArray() > 0) ctFont.addNewI().setVal(pr.getIArray(0).getVal());
+ if(pr.sizeOfColorArray() > 0) {
+ CTColor c1 = pr.getColorArray(0);
+ CTColor c2 = ctFont.addNewColor();
+ if(c1.isSetAuto()) c2.setAuto(c1.getAuto());
+ if(c1.isSetIndexed()) c2.setIndexed(c1.getIndexed());
+ if(c1.isSetRgb()) c2.setRgb(c1.getRgb());
+ if(c1.isSetTheme()) c2.setTheme(c1.getTheme());
+ if(c1.isSetTint()) c2.setTint(c1.getTint());
+ }
+ if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal());
+ if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal());
+ if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal());
+ if(pr.sizeOfCharsetArray() > 0) ctFont.addNewCharset().setVal(pr.getCharsetArray(0).getVal());
+ if(pr.sizeOfCondenseArray() > 0) ctFont.addNewCondense().setVal(pr.getCondenseArray(0).getVal());
+ if(pr.sizeOfExtendArray() > 0) ctFont.addNewExtend().setVal(pr.getExtendArray(0).getVal());
+ if(pr.sizeOfVertAlignArray() > 0) ctFont.addNewVertAlign().setVal(pr.getVertAlignArray(0).getVal());
+ if(pr.sizeOfOutlineArray() > 0) ctFont.addNewOutline().setVal(pr.getOutlineArray(0).getVal());
+ if(pr.sizeOfShadowArray() > 0) ctFont.addNewShadow().setVal(pr.getShadowArray(0).getVal());
+ if(pr.sizeOfStrikeArray() > 0) ctFont.addNewStrike().setVal(pr.getStrikeArray(0).getVal());
+
+ return ctFont;
}
}
if (this.worksheet.getSheetData() == null) {
this.worksheet.addNewSheetData();
}
+ //CTSheetView sheetView = getSheetTypeSheetView();
+ //sheetView.setWorkbookViewId(0);
initRows(this.worksheet);
initColumns(this.worksheet);
}
// HSSFSheet compatibility methods. See also the following zoom related methods
+ /**
+ * Sets the zoom magnication for the sheet. The zoom is expressed as a
+ * fraction. For example to express a zoom of 75% use 3 for the numerator
+ * and 4 for the denominator.
+ *
+ * @param numerator The numerator for the zoom magnification.
+ * @param denominator The denominator for the zoom magnification.
+ */
public void setZoom(int numerator, int denominator) {
- setZoom((numerator/denominator) * 100);
+ Float result = new Float(numerator)/new Float(denominator)*100;
+ setZoom(result.intValue());
}
public void setZoom(long scale) {
import java.io.IOException;
import java.io.OutputStream;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public XSSFWorkbook(String path) throws IOException {
this(openPackage(path));
}
+ public XSSFWorkbook(InputStream is) throws IOException {
+ this(openPackage(is));
+ }
+
public XSSFWorkbook(Package pkg) throws IOException {
super(pkg);
try {
}
- public Font 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();
- for (short i = 0; i <= fontNum; i++) {
- XSSFFont xssfFont = (XSSFFont)getFontAt(i);
- if (xssfFont.getBoldweight() == boldWeight
+ for (short i = 0; i < fontNum; i++) {
+ XSSFFont xssfFont = getFontAt(i);
+ if ( xssfFont.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD)
&& xssfFont.getColor() == color
&& xssfFont.getFontHeightInPoints() == fontHeight
&& xssfFont.getFontName().equals(name)
return (short) getFirstVisibleTab();
}
- public Font getFontAt(short idx) {
- return stylesSource.getFontAt(idx);
+ public XSSFFont getFontAt(short idx) {
+ return (XSSFFont)stylesSource.getFontAt(idx);
}
public XSSFName getNameAt(int index) {
public short getNumberOfFonts() {
// TODO Auto-generated method stub
- return 0;
+ return (short)((StylesTable)stylesSource).getNumberOfFonts();
}
public int getNumberOfNames() {
}
public void setBorderColor(BorderSide side, XSSFColor color) {
- color.setToBorder(getBorder(side));
+ getBorder(side).setColor(color.getCTColor());
}
private CTBorderPr getBorder(BorderSide side) {
==================================================================== */
package org.apache.poi.xssf.usermodel.extensions;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
+/**
+ * Represents a color in SpreadsheetML
+ */
public class XSSFColor {
- private CTColor color;
-
- public XSSFColor(CTColor color) {
- this.color = color;
- if (this.color == null) {
- this.color = CTColor.Factory.newInstance();
- }
+ private CTColor ctColor;
+
+ /**
+ * Create an instance of XSSFColor from the supplied XML bean
+ */
+ public XSSFColor(CTColor color) {
+ this.ctColor = color;
}
- public boolean isAuto() {
- return color.getAuto();
+ /**
+ * Create an new instance of XSSFColor
+ */
+ public XSSFColor() {
+ this.ctColor = CTColor.Factory.newInstance();
+ }
+
+ /**
+ * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+ */
+ public boolean isAuto() {
+ return ctColor.getAuto();
}
+ /**
+ * A boolean value indicating the ctColor is automatic and system ctColor dependent.
+ */
public void setAuto(boolean auto) {
- color.setAuto(auto);
+ ctColor.setAuto(auto);
}
-
- public long getIndexed() {
- return color.getIndexed();
+
+ /**
+ * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+ */
+ public int getIndexed() {
+ return (int)ctColor.getIndexed();
}
- public void setIndexed(long indexed) {
- color.setIndexed(indexed);
+ /**
+ * Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
+ */
+ public void setIndexed(int indexed) {
+ ctColor.setIndexed(indexed);
}
-
- public byte[] getRgb() {
- return color.getRgb();
+
+ /**
+ * Standard Alpha Red Green Blue ctColor value (ARGB).
+ */
+ public byte[] getRgb() {
+ return ctColor.getRgb();
}
+ /**
+ * Standard Alpha Red Green Blue ctColor value (ARGB).
+ */
public void setRgb(byte[] rgb) {
- color.setRgb(rgb);
+ ctColor.setRgb(rgb);
}
-
- public long getTheme() {
- return color.getTheme();
+
+ /**
+ * Index into the <clrScheme> collection, referencing a particular <sysClr> or
+ * <srgbClr> value expressed in the Theme part.
+ */
+ public int getTheme() {
+ return (int)ctColor.getTheme();
}
- public void setTheme(long theme) {
- color.setTheme(theme);
+ /**
+ * Index into the <clrScheme> collection, referencing a particular <sysClr> or
+ * <srgbClr> value expressed in the Theme part.
+ */
+ public void setTheme(int theme) {
+ ctColor.setTheme(theme);
}
-
- public double getTint() {
- return color.getTint();
+
+ /**
+ * Specifies the tint value applied to the ctColor.
+ *
+ * <p>
+ * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
+ * ctColor applied.
+ * </p>
+ * <p>
+ * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
+ * 1.0 means 100% lighten. Also, 0.0 means no change.
+ * </p>
+ * <p>
+ * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
+ * HLSMAX is currently 255.
+ * </p>
+ * Here are some examples of how to apply tint to ctColor:
+ * <blockquote>
+ * <pre>
+ * If (tint < 0)
+ * Lum\92 = Lum * (1.0 + tint)
+ *
+ * For example: Lum = 200; tint = -0.5; Darken 50%
+ * Lum\91 = 200 * (0.5) => 100
+ * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+ * Lum\91 = 200 * (1.0-1.0) => 0
+ * If (tint > 0)
+ * Lum\91 = Lum * (1.0-tint) + (HLSMAX \96 HLSMAX * (1.0-tint))
+ * For example: Lum = 100; tint = 0.75; Lighten 75%
+ *
+ * Lum\91 = 100 * (1-.75) + (HLSMAX \96 HLSMAX*(1-.75))
+ * = 100 * .25 + (255 \96 255 * .25)
+ * = 25 + (255 \96 63) = 25 + 192 = 217
+ * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+ * Lum\91 = 100 * (1-1) + (HLSMAX \96 HLSMAX*(1-1))
+ * = 100 * 0 + (255 \96 255 * 0)
+ * = 0 + (255 \96 0) = 255
+ * </pre>
+ * </blockquote>
+ *
+ * @return the tint value
+ */
+ public double getTint() {
+ return ctColor.getTint();
}
+ /**
+ * Specifies the tint value applied to the ctColor.
+ *
+ * <p>
+ * If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
+ * ctColor applied.
+ * </p>
+ * <p>
+ * The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
+ * 1.0 means 100% lighten. Also, 0.0 means no change.
+ * </p>
+ * <p>
+ * In loading the RGB value, it is converted to HLS where HLS values are (0..HLSMAX), where
+ * HLSMAX is currently 255.
+ * </p>
+ * Here are some examples of how to apply tint to ctColor:
+ * <blockquote>
+ * <pre>
+ * If (tint < 0)
+ * Lum\92 = Lum * (1.0 + tint)
+ *
+ * For example: Lum = 200; tint = -0.5; Darken 50%
+ * Lum\91 = 200 * (0.5) => 100
+ * For example: Lum = 200; tint = -1.0; Darken 100% (make black)
+ * Lum\91 = 200 * (1.0-1.0) => 0
+ * If (tint > 0)
+ * Lum\91 = Lum * (1.0-tint) + (HLSMAX \96 HLSMAX * (1.0-tint))
+ * For example: Lum = 100; tint = 0.75; Lighten 75%
+ *
+ * Lum\91 = 100 * (1-.75) + (HLSMAX \96 HLSMAX*(1-.75))
+ * = 100 * .25 + (255 \96 255 * .25)
+ * = 25 + (255 \96 63) = 25 + 192 = 217
+ * For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
+ * Lum\91 = 100 * (1-1) + (HLSMAX \96 HLSMAX*(1-1))
+ * = 100 * 0 + (255 \96 255 * 0)
+ * = 0 + (255 \96 0) = 255
+ * </pre>
+ * </blockquote>
+ *
+ * @param tint the tint value
+ */
public void setTint(double tint) {
- color.setTint(tint);
+ ctColor.setTint(tint);
}
- public void setToBorder(CTBorderPr border) {
- border.setColor(this.color);
- }
+ /**
+ * Returns the underlying XML bean
+ *
+ * @return the underlying XML bean
+ */
+ public CTColor getCTColor(){
+ return ctColor;
+ }
}
-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);
- }
-
-
-
-}
-
-
\ No newline at end of file
+++ /dev/null
-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;
-
-}
POITextExtractor extractor = extractors[i];
String text = extractor.getText().replaceAll("[\r\t]", "");
- //System.out.println(text.length());
- //System.out.println(text);
assertTrue(text.startsWith("First Sheet\nTest spreadsheet\n2nd row2nd row 2nd column\n"));
Pattern pattern = Pattern.compile(".*13(\\.0+)?\\s+Sheet3.*", Pattern.DOTALL);
Matcher m = pattern.matcher(text);
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.CommentsTable;
+import org.apache.poi.xssf.model.SharedStringsTable;
import org.apache.poi.xssf.model.SharedStringSource;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
private static XSSFRow createParentObjects() {
XSSFWorkbook wb = new XSSFWorkbook();
- wb.setSharedStringSource(new DummySharedStringSource());
+ wb.setSharedStringSource(new SharedStringsTable());
XSSFSheet sheet = new XSSFSheet(wb);
XSSFRow row = new XSSFRow(sheet);
return row;
package org.apache.poi.xssf.usermodel;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
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.openxml4j.opc.Package;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
public void testBoldweight(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
- CTBooleanProperty bool=wrapper.getCTFont().addNewB();
+ CTBooleanProperty bool=ctFont.addNewB();
bool.setVal(false);
- wrapper.setB(bool);
-
+ ctFont.setBArray(0,bool);
XSSFFont xssfFont=new XSSFFont(ctFont);
- assertEquals(Font.BOLDWEIGHT_NORMAL, xssfFont.getBoldweight());
+ assertEquals(false, xssfFont.getBold());
- xssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ xssfFont.setBold(true);
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);
+
+ ctFont.setCharsetArray(0,prop);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.ANSI_CHARSET,xssfFont.getCharSet());
-
+
xssfFont.setCharSet(Font.DEFAULT_CHARSET);
- assertEquals(Charset.DEFAULT_CHARSET, wrapper.getCharset().getVal());
+ assertEquals(Charset.DEFAULT_CHARSET,ctFont.getCharsetArray(0).getVal());
}
public void testFontName(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontName fname=ctFont.addNewName();
fname.setVal("Arial");
- wrapper.setFontName(fname);
+ ctFont.setNameArray(0,fname);
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals("Arial", xssfFont.getFontName());
xssfFont.setFontName("Courier");
- assertEquals("Courier",wrapper.getName().getVal());
+ assertEquals("Courier",ctFont.getNameArray(0).getVal());
}
public void testItalic(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
- CTBooleanProperty bool=wrapper.getCTFont().addNewI();
+ CTBooleanProperty bool=ctFont.addNewI();
bool.setVal(false);
- wrapper.setI(bool);
+ ctFont.setIArray(0,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());
+ assertEquals(true,ctFont.getIArray(0).getVal());
}
public void testStrikeout(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
- CTBooleanProperty bool=wrapper.getCTFont().addNewStrike();
+ CTBooleanProperty bool=ctFont.addNewStrike();
bool.setVal(false);
- wrapper.setStrike(bool);
+ ctFont.setStrikeArray(0,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());
+ assertEquals(true,ctFont.getStrikeArray(0).getVal());
}
public void testFontHeight(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontSize size=ctFont.addNewSz();
size.setVal(11);
- wrapper.setSz(size);
-
+ ctFont.setSzArray(0,size);
+
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(11/20,xssfFont.getFontHeight());
-
+
xssfFont.setFontHeight((short)20);
- assertEquals(new Double(20*20).doubleValue(),wrapper.getSz().getVal()); }
+ assertEquals(new Double(20*20).doubleValue(),ctFont.getSzArray(0).getVal());
+ }
public void testFontHeightInPoint(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
CTFontSize size=ctFont.addNewSz();
size.setVal(14);
- wrapper.setSz(size);
-
+ ctFont.setSzArray(0,size);
+
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(14,xssfFont.getFontHeightInPoints());
-
+
xssfFont.setFontHeightInPoints((short)20);
- assertEquals(new Double(20).doubleValue(),wrapper.getSz().getVal());
+ assertEquals(new Double(20).doubleValue(),ctFont.getSzArray(0).getVal());
}
public void testUnderline(){
CTFont ctFont=CTFont.Factory.newInstance();
- CTFontWrapper wrapper=new CTFontWrapper(ctFont);
-
- CTUnderlineProperty underlinePropr=wrapper.getCTFont().addNewU();
+ CTUnderlineProperty underlinePropr=ctFont.addNewU();
underlinePropr.setVal(STUnderlineValues.SINGLE);
- wrapper.setU(underlinePropr);
+ ctFont.setUArray(0,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());
- }
+ assertEquals(STUnderlineValues.DOUBLE,ctFont.getUArray(0).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);
-
+ ctFont.setColorArray(0,color);
+
XSSFFont xssfFont=new XSSFFont(ctFont);
assertEquals(Font.COLOR_NORMAL,xssfFont.getColor());
-
- xssfFont.setColor(Font.COLOR_RED);
- assertEquals(IndexedColors.RED,wrapper.getColor().getIndexed());
+
+ xssfFont.setColor(IndexedColors.RED.getIndex());
+ assertEquals(IndexedColors.RED.getIndex(), ctFont.getColorArray(0).getIndexed());
+ }
+/*
+ public void testRgbColor(){
+ CTFont ctFont=CTFont.Factory.newInstance();
+ CTColor color=ctFont.addNewColor();
+ color.setRgb(new byte[]{});
+ ctFont.setColorArray(0,color);
+
+ XSSFFont xssfFont=new XSSFFont(ctFont);
+ assertEquals(,xssfFont.getRgbColor());
+
+ xssfFont.setRgbColor(new XSSFColor(new java.awt.Color(10,19,10)));
+ //assertEquals(,ctFont.getColorArray(0).getRgb());
}
+ public void testThemeColor(){
+ CTFont ctFont=CTFont.Factory.newInstance();
+ CTColor color=ctFont.addNewColor();
+ color.setTheme();
+ ctFont.setColorArray(0,color);
+
+ XSSFFont xssfFont=new XSSFFont(ctFont);
+ assertEquals(,xssfFont.getThemeColor());
+ xssfFont.setThemeColor(Font.COLOR_RED);
+ assertEquals(,ctFont.getColorArray(0).getTheme());
+ assertEquals(,ctFont.getColorArray(0).getTint());
+ }
+*/
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);
-
+ ctFont.setFamilyArray(0,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);
-
+ ctFont.setSchemeArray(0,scheme);
+
XSSFFont font=new XSSFFont(ctFont);
assertEquals(XSSFFont.SCHEME_MAJOR,font.getScheme());
-
+
font.setScheme(XSSFFont.SCHEME_NONE);
- assertEquals(STFontScheme.NONE,wrapper.getFontScheme().getVal());
+ assertEquals(STFontScheme.NONE,ctFont.getSchemeArray(0).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);
-
+ ctFont.setVertAlignArray(0,valign);
+
XSSFFont font=new XSSFFont(ctFont);
assertEquals(Font.SS_NONE,font.getTypeOffset());
-
+
font.setTypeOffset(XSSFFont.SS_SUPER);
- assertEquals(STVerticalAlignRun.SUPERSCRIPT,wrapper.getVertAlign().getVal());
+ assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
}
-
+
+ /**
+ * Tests that we can define fonts to a new
+ * file, save, load, and still see them
+ * @throws Exception
+ */
+ public void testCreateSave() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ XSSFSheet s1 = (XSSFSheet)wb.createSheet();
+ Row r1 = s1.createRow(0);
+ Cell r1c1 = r1.createCell(0);
+ r1c1.setCellValue(2.2);
+
+ assertEquals(1, wb.getNumberOfFonts());
+
+ XSSFFont font=wb.createFont();
+ font.setBold(true);
+ font.setStrikeout(true);
+ font.setColor(IndexedColors.YELLOW.getIndex());
+ font.setFontName("Courier");
+ wb.createCellStyle().setFont(font);
+ assertEquals(2, wb.getNumberOfFonts());
+
+ CellStyle cellStyleTitle=wb.createCellStyle();
+ cellStyleTitle.setFont(font);
+ r1c1.setCellStyle(cellStyleTitle);
+
+ // Save and re-load
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ wb.write(baos);
+ ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+
+ wb = new XSSFWorkbook(Package.open(bais));
+ s1 = (XSSFSheet)wb.getSheetAt(0);
+
+ assertEquals(2, wb.getNumberOfFonts());
+ assertNotNull(s1.getRow(0).getCell(0).getCellStyle().getFont(wb));
+ assertEquals(IndexedColors.YELLOW.getIndex(), s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getColor());
+ assertEquals("Courier", s1.getRow(0).getCell(0).getCellStyle().getFont(wb).getFontName());
+
+ // Now add an orphaned one
+ XSSFFont font2 = wb.createFont();
+ font2.setItalic(true);
+ font2.setFontHeightInPoints((short)15);
+ wb.createCellStyle().setFont(font2);
+ assertEquals(3, wb.getNumberOfFonts());
+
+ // Save and re-load
+ baos = new ByteArrayOutputStream();
+ wb.write(baos);
+ bais = new ByteArrayInputStream(baos.toByteArray());
+
+ wb = new XSSFWorkbook(Package.open(bais));
+ s1 = (XSSFSheet)wb.getSheetAt(0);
+
+ assertEquals(3, wb.getNumberOfFonts());
+ assertNotNull(wb.getFontAt((short)1));
+ assertNotNull(wb.getFontAt((short)2));
+
+ assertEquals(15, wb.getFontAt((short)2).getFontHeightInPoints());
+ assertEquals(true, wb.getFontAt((short)2).getItalic());
+ }
+
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);
+ XSSFFont font=new XSSFFont();
+ font.setBold(true);
font.setFontHeightInPoints((short)22);
- font.setColor((short)IndexedColors.BLUE);
+ font.setColor(IndexedColors.BLUE.getIndex());
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);
+ XSSFFont font1=new XSSFFont();
+ font1.setBold(true);
font1.setItalic(true);
font1.setFontHeightInPoints((short)18);
- font1.setColor(Font.COLOR_RED);
+ font1.setColor(IndexedColors.RED.getIndex());
font1.setFontName("Arial");
CellStyle cellStyle1=workbook.createCellStyle();
cellStyle1.setFont(font1);
font3.setFontHeightInPoints((short)9);
font3.setFontName("Times");
font3.setStrikeout(true);
- font3.setColor((short)IndexedColors.PINK);
+ font3.setColor(IndexedColors.PINK.getIndex());
CellStyle cellStyle3=workbook.createCellStyle();
cellStyle3.setFont(font3);
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.xssf.usermodel;\r
+\r
+import java.io.*;\r
+import java.util.ArrayList;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import org.apache.poi.ss.usermodel.Cell;\r
+import org.apache.poi.ss.usermodel.CellStyle;\r
+import org.apache.poi.ss.usermodel.Comment;\r
+import org.apache.poi.ss.usermodel.CreationHelper;\r
+import org.apache.poi.ss.usermodel.Row;\r
+import org.apache.poi.ss.usermodel.SharedStringSource;\r
+import org.apache.poi.ss.usermodel.Sheet;\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+import org.apache.poi.xssf.model.CommentsTable;\r
+import org.apache.xmlbeans.XmlOptions;\r
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;\r
+\r
+/**\r
+ * Tests functionality of the XSSFRichTextRun object\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestXSSFRichTextString extends TestCase {\r
+\r
+ public void testCreate() throws Exception {\r
+\r
+ XSSFRichTextString rt = new XSSFRichTextString("Apache POI");\r
+ assertEquals("Apache POI", rt.getString());\r
+\r
+ CTRst st = rt.getCTRst();\r
+ assertTrue(st.isSetT());\r
+ assertEquals("Apache POI", st.getT());\r
+\r
+ rt.append(" is cool stuff");\r
+ assertEquals(2, st.sizeOfRArray());\r
+ assertFalse(st.isSetT());\r
+\r
+ assertEquals("Apache POI is cool stuff", rt.getString());\r
+ }\r
+\r
+\r
+ public void testApplyFont() throws Exception {\r
+\r
+ XSSFRichTextString rt = new XSSFRichTextString();\r
+ rt.append("123");\r
+ rt.append("4567");\r
+ rt.append("89");\r
+\r
+ XSSFFont font1 = new XSSFFont();\r
+ font1.setBold(true);\r
+\r
+ rt.applyFont(2, 5, font1);\r
+\r
+ assertEquals(4, rt.numFormattingRuns());\r
+ assertEquals(0, rt.getIndexOfFormattingRun(0));\r
+ assertEquals(2, rt.getLengthOfFormattingRun(0));\r
+\r
+ assertEquals(2, rt.getIndexOfFormattingRun(1));\r
+ assertEquals(3, rt.getLengthOfFormattingRun(1));\r
+\r
+ assertEquals(5, rt.getIndexOfFormattingRun(2));\r
+ assertEquals(2, rt.getLengthOfFormattingRun(2));\r
+\r
+ assertEquals(7, rt.getIndexOfFormattingRun(3));\r
+ assertEquals(2, rt.getLengthOfFormattingRun(3));\r
+ }\r
+\r
+ public void testClearFormatting() throws Exception {\r
+\r
+ XSSFRichTextString rt = new XSSFRichTextString("Apache POI");\r
+ assertEquals("Apache POI", rt.getString());\r
+\r
+ rt.clearFormatting();\r
+\r
+ CTRst st = rt.getCTRst();\r
+ assertTrue(st.isSetT());\r
+ assertEquals("Apache POI", rt.getString());\r
+ assertEquals(0, rt.numFormattingRuns());\r
+\r
+ XSSFFont font = new XSSFFont();\r
+ font.setBold(true);\r
+\r
+ rt.applyFont(7, 10, font);\r
+ assertEquals(2, rt.numFormattingRuns());\r
+ rt.clearFormatting();\r
+ assertEquals("Apache POI", rt.getString());\r
+ assertEquals(0, rt.numFormattingRuns());\r
+\r
+ }\r
+\r
+ public void testGetFonts() throws Exception {\r
+\r
+ XSSFRichTextString rt = new XSSFRichTextString();\r
+\r
+ XSSFFont font1 = new XSSFFont();\r
+ font1.setFontName("Arial");\r
+ font1.setItalic(true);\r
+ rt.append("The quick", font1);\r
+\r
+ XSSFFont font1$ = rt.getFontOfFormattingRun(0);\r
+ assertEquals(font1.getItalic(), font1$.getItalic());\r
+ assertEquals(font1.getFontName(), font1$.getFontName());\r
+\r
+ XSSFFont font2 = new XSSFFont();\r
+ font2.setFontName("Courier");\r
+ font2.setBold(true);\r
+ rt.append(" brown fox", font2);\r
+\r
+ XSSFFont font2$ = rt.getFontOfFormattingRun(1);\r
+ assertEquals(font2.getBold(), font2$.getBold());\r
+ assertEquals(font2.getFontName(), font2$.getFontName());\r
+\r
+ }\r
+}\r
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.TestXSSFCell.DummySharedStringSource;
+import org.apache.poi.xssf.model.SharedStringsTable;
/**
* Tests for XSSFRow
row.setZeroHeight(true);
assertTrue(row.getZeroHeight());
}
-
+
/**
* Tests for the missing/blank cell policy stuff
*/
row.createCell((short)1).setCellValue(3.2);
row.createCell((short)4, Cell.CELL_TYPE_BLANK);
row.createCell((short)5).setCellValue(4);
-
+
// First up, no policy
assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1).getCellType());
assertEquals(null, row.getCell(3));
assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5).getCellType());
-
+
// RETURN_NULL_AND_BLANK - same as default
assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, Row.RETURN_NULL_AND_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, Row.RETURN_NULL_AND_BLANK).getCellType());
assertEquals(null, row.getCell(3, Row.RETURN_NULL_AND_BLANK));
assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, Row.RETURN_NULL_AND_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, Row.RETURN_NULL_AND_BLANK).getCellType());
-
+
// RETURN_BLANK_AS_NULL - nearly the same
assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
assertEquals(null, row.getCell(3, XSSFRow.RETURN_BLANK_AS_NULL));
assertEquals(null, row.getCell(4, XSSFRow.RETURN_BLANK_AS_NULL));
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.RETURN_BLANK_AS_NULL).getCellType());
-
+
// CREATE_NULL_AS_BLANK - creates as needed
assertEquals(Cell.CELL_TYPE_STRING, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_BLANK, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellType());
-
+
// Check created ones get the right column
assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum());
private static XSSFSheet createParentObjects() {
XSSFWorkbook wb = new XSSFWorkbook();
- wb.setSharedStringSource(new DummySharedStringSource());
+ wb.setSharedStringSource(new SharedStringsTable());
return new XSSFSheet(wb);
}
}
}
public void testGetActiveCell() {
- Workbook workbook = new XSSFWorkbook();
- CTSheet ctSheet = CTSheet.Factory.newInstance();
- CTWorksheet ctWorksheet = CTWorksheet.Factory.newInstance();
- XSSFSheet sheet = new XSSFSheet(ctSheet, ctWorksheet, (XSSFWorkbook) workbook);
- ctWorksheet.addNewSheetViews().addNewSheetView().addNewSelection().setActiveCell("R5");
-
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet();
+ sheet.setActiveCell("R5");
+
assertEquals("R5", sheet.getActiveCell());
}
assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
XSSFRow row = (XSSFRow) sheet.createRow(0);
XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3);
- //System.out.println(cell.getCellStyle());
}
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(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());
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(1,sheet.getSheetTypeSheetFormatPr().getOutlineLevelRow());
}
+
+ public void testSetZoom() {
+ Workbook workBook = new XSSFWorkbook();
+ XSSFSheet sheet1 = (XSSFSheet) workBook.createSheet("new sheet");
+ sheet1.setZoom(3,4); // 75 percent magnification
+ long zoom = sheet1.getSheetTypeSheetView().getZoomScale();
+ assertEquals(zoom, 75);
+ }
}
//get default font, then change 2 values and check against different values (height changes)
Font font=workbook.createFont();
- font.setBoldweight(Font.BOLDWEIGHT_BOLD);
+ ((XSSFFont)font).setBold(true);
font.setUnderline(Font.U_DOUBLE);
StylesSource styleSource=new StylesTable();
long index=styleSource.putFont(font);
assertNotNull(fontAt);
}
+ public void testGetNumberOfFonts(){
+ XSSFWorkbook wb = new XSSFWorkbook();
+
+ XSSFFont f1=wb.createFont();
+ f1.setBold(true);
+ wb.createCellStyle().setFont(f1);
+
+ XSSFFont f2=wb.createFont();
+ f2.setUnderline(Font.U_DOUBLE);
+ wb.createCellStyle().setFont(f2);
+
+ XSSFFont f3=wb.createFont();
+ f3.setFontHeightInPoints((short)23);
+ wb.createCellStyle().setFont(f3);
+
+ assertEquals(4,wb.getNumberOfFonts());
+ assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
+ }
+
public void testGetNumCellStyles(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = workbook.getNumCellStyles();