Applied patch 45898 - fixed XSSFCellFill getFillBackgroundColor and getFillForegroundColor to not throw NPE if fill has not been set. Some other code clean up.
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@699984 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
84e3bbd179
commit
72d626f2b2
@ -24,6 +24,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
@ -54,149 +55,148 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
|
|||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Table of styles shared across all sheets in a workbook.
|
* Table of styles shared across all sheets in a workbook.
|
||||||
*
|
*
|
||||||
* @version $Id: SharedStringsTable.java 612495 2008-01-16 16:08:22Z ugo $
|
* @author ugo
|
||||||
*/
|
*/
|
||||||
public class StylesTable implements StylesSource, XSSFModel {
|
public class StylesTable implements StylesSource, XSSFModel {
|
||||||
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
|
private final Hashtable<Long,String> numberFormats = new Hashtable<Long,String>();
|
||||||
private final ArrayList<CTFont> fonts = new ArrayList<CTFont>();
|
private final List<CTFont> fonts = new ArrayList<CTFont>();
|
||||||
private final LinkedList<CTFill> fills = new LinkedList<CTFill>();
|
private final List<CTFill> fills = new LinkedList<CTFill>();
|
||||||
private final LinkedList<CTBorder> borders = new LinkedList<CTBorder>();
|
private final List<CTBorder> borders = new LinkedList<CTBorder>();
|
||||||
private final LinkedList<CTXf> styleXfs = new LinkedList<CTXf>();
|
private final List<CTXf> styleXfs = new LinkedList<CTXf>();
|
||||||
private final LinkedList<CTXf> xfs = new LinkedList<CTXf>();
|
private final List<CTXf> xfs = new LinkedList<CTXf>();
|
||||||
|
|
||||||
private final LinkedList<CTDxf> dxfs = new LinkedList<CTDxf>();
|
private final List<CTDxf> dxfs = new LinkedList<CTDxf>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The first style id available for use as a custom style
|
* The first style id available for use as a custom style
|
||||||
*/
|
*/
|
||||||
public static final long FIRST_CUSTOM_STYLE_ID = 165;
|
public static final long FIRST_CUSTOM_STYLE_ID = 165;
|
||||||
|
|
||||||
private StyleSheetDocument doc;
|
private StyleSheetDocument doc;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new StylesTable, by reading it from
|
* Create a new StylesTable, by reading it from
|
||||||
* the InputStream of a a PackagePart.
|
* the InputStream of a a PackagePart.
|
||||||
*
|
*
|
||||||
* @param is The input stream containing the XML document.
|
* @param is The input stream containing the XML document.
|
||||||
* @throws IOException if an error occurs while reading.
|
* @throws IOException if an error occurs while reading.
|
||||||
*/
|
*/
|
||||||
public StylesTable(InputStream is) throws IOException {
|
public StylesTable(InputStream is) throws IOException {
|
||||||
readFrom(is);
|
readFrom(is);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Create a new, empty StylesTable
|
* Create a new, empty StylesTable
|
||||||
*/
|
*/
|
||||||
public StylesTable() {
|
public StylesTable() {
|
||||||
doc = StyleSheetDocument.Factory.newInstance();
|
doc = StyleSheetDocument.Factory.newInstance();
|
||||||
doc.addNewStyleSheet();
|
doc.addNewStyleSheet();
|
||||||
// Initialization required in order to make the document readable by MSExcel
|
// Initialization required in order to make the document readable by MSExcel
|
||||||
initialize();
|
initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read this shared styles table from an XML file.
|
* Read this shared styles table from an XML file.
|
||||||
*
|
*
|
||||||
* @param is The input stream containing the XML document.
|
* @param is The input stream containing the XML document.
|
||||||
* @throws IOException if an error occurs while reading.
|
* @throws IOException if an error occurs while reading.
|
||||||
*/
|
*/
|
||||||
public void readFrom(InputStream is) throws IOException {
|
public void readFrom(InputStream is) throws IOException {
|
||||||
try {
|
try {
|
||||||
doc = StyleSheetDocument.Factory.parse(is);
|
doc = StyleSheetDocument.Factory.parse(is);
|
||||||
// Grab all the different bits we care about
|
// Grab all the different bits we care about
|
||||||
if(doc.getStyleSheet().getNumFmts() != null)
|
if(doc.getStyleSheet().getNumFmts() != null)
|
||||||
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
|
for (CTNumFmt nfmt : doc.getStyleSheet().getNumFmts().getNumFmtArray()) {
|
||||||
numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
|
numberFormats.put(nfmt.getNumFmtId(), nfmt.getFormatCode());
|
||||||
}
|
}
|
||||||
if(doc.getStyleSheet().getFonts() != null)
|
if(doc.getStyleSheet().getFonts() != null)
|
||||||
for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
|
for (CTFont font : doc.getStyleSheet().getFonts().getFontArray()) {
|
||||||
fonts.add(font);
|
fonts.add(font);
|
||||||
}
|
}
|
||||||
if(doc.getStyleSheet().getFills() != null)
|
if(doc.getStyleSheet().getFills() != null)
|
||||||
for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
|
for (CTFill fill : doc.getStyleSheet().getFills().getFillArray()) {
|
||||||
fills.add(fill);
|
fills.add(fill);
|
||||||
}
|
}
|
||||||
if(doc.getStyleSheet().getBorders() != null)
|
if(doc.getStyleSheet().getBorders() != null)
|
||||||
for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
|
for (CTBorder border : doc.getStyleSheet().getBorders().getBorderArray()) {
|
||||||
borders.add(border);
|
borders.add(border);
|
||||||
}
|
}
|
||||||
if(doc.getStyleSheet().getCellXfs() != null)
|
if(doc.getStyleSheet().getCellXfs() != null)
|
||||||
for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
|
for (CTXf xf : doc.getStyleSheet().getCellXfs().getXfArray()) {
|
||||||
xfs.add(xf);
|
xfs.add(xf);
|
||||||
}
|
}
|
||||||
if(doc.getStyleSheet().getCellStyleXfs() != null)
|
if(doc.getStyleSheet().getCellStyleXfs() != null)
|
||||||
for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
|
for (CTXf xf : doc.getStyleSheet().getCellStyleXfs().getXfArray()) {
|
||||||
styleXfs.add(xf);
|
styleXfs.add(xf);
|
||||||
}
|
}
|
||||||
// dxf
|
// dxf
|
||||||
if(doc.getStyleSheet().getDxfs() != null)
|
if(doc.getStyleSheet().getDxfs() != null)
|
||||||
for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
|
for (CTDxf dxf : doc.getStyleSheet().getDxfs().getDxfArray()) {
|
||||||
dxfs.add(dxf);
|
dxfs.add(dxf);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (XmlException e) {
|
} catch (XmlException e) {
|
||||||
throw new IOException(e.getLocalizedMessage());
|
throw new IOException(e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
// Start of style related getters and setters
|
// Start of style related getters and setters
|
||||||
// ===========================================================
|
// ===========================================================
|
||||||
|
|
||||||
public String getNumberFormatAt(long idx) {
|
public String getNumberFormatAt(long idx) {
|
||||||
return numberFormats.get(idx);
|
return numberFormats.get(idx);
|
||||||
}
|
}
|
||||||
public synchronized long putNumberFormat(String fmt) {
|
public synchronized long putNumberFormat(String fmt) {
|
||||||
if (numberFormats.containsValue(fmt)) {
|
if (numberFormats.containsValue(fmt)) {
|
||||||
// Find the key, and return that
|
// Find the key, and return that
|
||||||
for(Enumeration<Long> keys = numberFormats.keys(); keys.hasMoreElements();) {
|
for(Enumeration<Long> keys = numberFormats.keys(); keys.hasMoreElements();) {
|
||||||
Long key = keys.nextElement();
|
Long key = keys.nextElement();
|
||||||
if(numberFormats.get(key).equals(fmt)) {
|
if(numberFormats.get(key).equals(fmt)) {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
|
throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find a spare key, and add that
|
// Find a spare key, and add that
|
||||||
long newKey = FIRST_CUSTOM_STYLE_ID;
|
long newKey = FIRST_CUSTOM_STYLE_ID;
|
||||||
while(numberFormats.containsKey(newKey)) {
|
while(numberFormats.containsKey(newKey)) {
|
||||||
newKey++;
|
newKey++;
|
||||||
}
|
}
|
||||||
numberFormats.put(newKey, fmt);
|
numberFormats.put(newKey, fmt);
|
||||||
return newKey;
|
return newKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Font getFontAt(long idx) {
|
public Font getFontAt(long idx) {
|
||||||
return new XSSFFont(fonts.get((int) idx));
|
return new XSSFFont(fonts.get((int) idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized long putFont(Font font) {
|
public synchronized long putFont(Font font) {
|
||||||
return putFont((XSSFFont)font, fonts);
|
return putFont((XSSFFont)font, fonts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFCellStyle getStyleAt(long idx) {
|
public XSSFCellStyle getStyleAt(long idx) {
|
||||||
int styleXfId = 0;
|
int styleXfId = 0;
|
||||||
|
|
||||||
// 0 is the empty default
|
// 0 is the empty default
|
||||||
if(xfs.get((int) idx).getXfId() > 0) {
|
if(xfs.get((int) idx).getXfId() > 0) {
|
||||||
styleXfId = (int) xfs.get((int) idx).getXfId();
|
styleXfId = (int) xfs.get((int) idx).getXfId();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new XSSFCellStyle((int) idx, styleXfId, this);
|
return new XSSFCellStyle((int) idx, styleXfId, this);
|
||||||
}
|
}
|
||||||
public synchronized long putStyle(CellStyle style) {
|
public synchronized long putStyle(CellStyle style) {
|
||||||
XSSFCellStyle xStyle = (XSSFCellStyle)style;
|
XSSFCellStyle xStyle = (XSSFCellStyle)style;
|
||||||
CTXf mainXF = xStyle.getCoreXf();
|
CTXf mainXF = xStyle.getCoreXf();
|
||||||
|
|
||||||
if(! xfs.contains(mainXF)) {
|
if(! xfs.contains(mainXF)) {
|
||||||
xfs.add(mainXF);
|
xfs.add(mainXF);
|
||||||
}
|
}
|
||||||
return xfs.indexOf(mainXF);
|
return xfs.indexOf(mainXF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFCellBorder getBorderAt(long idx) {
|
public XSSFCellBorder getBorderAt(long idx) {
|
||||||
return new XSSFCellBorder(borders.get((int)idx));
|
return new XSSFCellBorder(borders.get((int)idx));
|
||||||
@ -209,7 +209,7 @@ public class StylesTable implements StylesSource, XSSFModel {
|
|||||||
return new XSSFCellFill(fills.get((int) idx));
|
return new XSSFCellFill(fills.get((int) idx));
|
||||||
}
|
}
|
||||||
public long putFill(XSSFCellFill fill) {
|
public long putFill(XSSFCellFill fill) {
|
||||||
return putFill(fill, fills);
|
return fill.putFill(fills);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CTXf getCellXfAt(long idx) {
|
public CTXf getCellXfAt(long idx) {
|
||||||
@ -227,228 +227,217 @@ public class StylesTable implements StylesSource, XSSFModel {
|
|||||||
styleXfs.add(cellStyleXf);
|
styleXfs.add(cellStyleXf);
|
||||||
return styleXfs.size();
|
return styleXfs.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* get the size of cell styles
|
* get the size of cell styles
|
||||||
*/
|
*/
|
||||||
public int getNumCellStyles(){
|
public int getNumCellStyles(){
|
||||||
return styleXfs.size();
|
return styleXfs.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* get the size of fonts
|
* get the size of fonts
|
||||||
*/
|
*/
|
||||||
public int getNumberOfFonts(){
|
public int getNumberOfFonts(){
|
||||||
return this.fonts.size();
|
return this.fonts.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getNumberFormatSize() {
|
public int _getNumberFormatSize() {
|
||||||
return numberFormats.size();
|
return numberFormats.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getFontsSize() {
|
public int _getFontsSize() {
|
||||||
return fonts.size();
|
return fonts.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getFillsSize() {
|
public int _getFillsSize() {
|
||||||
return fills.size();
|
return fills.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getBordersSize() {
|
public int _getBordersSize() {
|
||||||
return borders.size();
|
return borders.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getXfsSize() {
|
public int _getXfsSize() {
|
||||||
return xfs.size();
|
return xfs.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only
|
* For unit testing only
|
||||||
*/
|
*/
|
||||||
public int _getStyleXfsSize() {
|
public int _getStyleXfsSize() {
|
||||||
return styleXfs.size();
|
return styleXfs.size();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* For unit testing only!
|
* For unit testing only!
|
||||||
*/
|
*/
|
||||||
public CTStylesheet _getRawStylesheet() {
|
public CTStylesheet _getRawStylesheet() {
|
||||||
return doc.getStyleSheet();
|
return doc.getStyleSheet();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write this table out as XML.
|
|
||||||
*
|
|
||||||
* @param out The stream to write to.
|
|
||||||
* @throws IOException if an error occurs while writing.
|
|
||||||
*/
|
|
||||||
public void writeTo(OutputStream out) throws IOException {
|
|
||||||
XmlOptions options = new XmlOptions();
|
|
||||||
options.setSaveOuter();
|
|
||||||
options.setUseDefaultNamespace();
|
|
||||||
|
|
||||||
// Requests use of whitespace for easier reading
|
|
||||||
options.setSavePrettyPrint();
|
|
||||||
|
|
||||||
|
|
||||||
// Work on the current one
|
|
||||||
// Need to do this, as we don't handle
|
|
||||||
// all the possible entries yet
|
|
||||||
|
|
||||||
// Formats
|
|
||||||
CTNumFmts formats = CTNumFmts.Factory.newInstance();
|
|
||||||
formats.setCount(numberFormats.size());
|
|
||||||
for (Entry<Long, String> fmt : numberFormats.entrySet()) {
|
|
||||||
CTNumFmt ctFmt = formats.addNewNumFmt();
|
|
||||||
ctFmt.setNumFmtId(fmt.getKey());
|
|
||||||
ctFmt.setFormatCode(fmt.getValue());
|
|
||||||
}
|
|
||||||
doc.getStyleSheet().setNumFmts(formats);
|
|
||||||
|
|
||||||
// Fonts
|
|
||||||
CTFonts ctFonts = CTFonts.Factory.newInstance();
|
|
||||||
ctFonts.setCount(fonts.size());
|
|
||||||
ctFonts.setFontArray(
|
|
||||||
fonts.toArray(new CTFont[fonts.size()])
|
|
||||||
);
|
|
||||||
doc.getStyleSheet().setFonts(ctFonts);
|
|
||||||
|
|
||||||
// Fills
|
|
||||||
CTFills ctFills = CTFills.Factory.newInstance();
|
|
||||||
ctFills.setCount(fills.size());
|
|
||||||
ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
|
|
||||||
doc.getStyleSheet().setFills(ctFills);
|
|
||||||
|
|
||||||
// Borders
|
|
||||||
CTBorders ctBorders = CTBorders.Factory.newInstance();
|
|
||||||
ctBorders.setCount(borders.size());
|
|
||||||
ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
|
|
||||||
doc.getStyleSheet().setBorders(ctBorders);
|
|
||||||
|
|
||||||
// Xfs
|
|
||||||
if(xfs.size() > 0) {
|
|
||||||
CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
|
|
||||||
ctXfs.setCount(xfs.size());
|
|
||||||
ctXfs.setXfArray(
|
|
||||||
xfs.toArray(new CTXf[xfs.size()])
|
|
||||||
);
|
|
||||||
doc.getStyleSheet().setCellXfs(ctXfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Style xfs
|
|
||||||
if(styleXfs.size() > 0) {
|
|
||||||
CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
|
|
||||||
ctSXfs.setCount(styleXfs.size());
|
|
||||||
ctSXfs.setXfArray(
|
|
||||||
styleXfs.toArray(new CTXf[styleXfs.size()])
|
|
||||||
);
|
|
||||||
doc.getStyleSheet().setCellStyleXfs(ctSXfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Style dxfs
|
|
||||||
if(dxfs.size() > 0) {
|
|
||||||
CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
|
|
||||||
ctDxfs.setCount(dxfs.size());
|
|
||||||
ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
|
|
||||||
);
|
|
||||||
doc.getStyleSheet().setDxfs(ctDxfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save
|
|
||||||
doc.save(out, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
private long putBorder(XSSFCellBorder border, LinkedList<CTBorder> borders) {
|
|
||||||
return border.putBorder(borders);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private long putFill(XSSFCellFill fill, LinkedList<CTFill> fills) {
|
|
||||||
return fill.putFill(fills);
|
/**
|
||||||
|
* Write this table out as XML.
|
||||||
|
*
|
||||||
|
* @param out The stream to write to.
|
||||||
|
* @throws IOException if an error occurs while writing.
|
||||||
|
*/
|
||||||
|
public void writeTo(OutputStream out) throws IOException {
|
||||||
|
XmlOptions options = new XmlOptions();
|
||||||
|
options.setSaveOuter();
|
||||||
|
options.setUseDefaultNamespace();
|
||||||
|
|
||||||
|
// Requests use of whitespace for easier reading
|
||||||
|
options.setSavePrettyPrint();
|
||||||
|
|
||||||
|
|
||||||
|
// Work on the current one
|
||||||
|
// Need to do this, as we don't handle
|
||||||
|
// all the possible entries yet
|
||||||
|
|
||||||
|
// Formats
|
||||||
|
CTNumFmts formats = CTNumFmts.Factory.newInstance();
|
||||||
|
formats.setCount(numberFormats.size());
|
||||||
|
for (Entry<Long, String> fmt : numberFormats.entrySet()) {
|
||||||
|
CTNumFmt ctFmt = formats.addNewNumFmt();
|
||||||
|
ctFmt.setNumFmtId(fmt.getKey());
|
||||||
|
ctFmt.setFormatCode(fmt.getValue());
|
||||||
|
}
|
||||||
|
doc.getStyleSheet().setNumFmts(formats);
|
||||||
|
|
||||||
|
// Fonts
|
||||||
|
CTFonts ctFonts = CTFonts.Factory.newInstance();
|
||||||
|
ctFonts.setCount(fonts.size());
|
||||||
|
ctFonts.setFontArray(
|
||||||
|
fonts.toArray(new CTFont[fonts.size()])
|
||||||
|
);
|
||||||
|
doc.getStyleSheet().setFonts(ctFonts);
|
||||||
|
|
||||||
|
// Fills
|
||||||
|
CTFills ctFills = CTFills.Factory.newInstance();
|
||||||
|
ctFills.setCount(fills.size());
|
||||||
|
ctFills.setFillArray(fills.toArray(new CTFill[fills.size()]));
|
||||||
|
doc.getStyleSheet().setFills(ctFills);
|
||||||
|
|
||||||
|
// Borders
|
||||||
|
CTBorders ctBorders = CTBorders.Factory.newInstance();
|
||||||
|
ctBorders.setCount(borders.size());
|
||||||
|
ctBorders.setBorderArray(borders.toArray(new CTBorder[borders.size()]));
|
||||||
|
doc.getStyleSheet().setBorders(ctBorders);
|
||||||
|
|
||||||
|
// Xfs
|
||||||
|
if(xfs.size() > 0) {
|
||||||
|
CTCellXfs ctXfs = CTCellXfs.Factory.newInstance();
|
||||||
|
ctXfs.setCount(xfs.size());
|
||||||
|
ctXfs.setXfArray(
|
||||||
|
xfs.toArray(new CTXf[xfs.size()])
|
||||||
|
);
|
||||||
|
doc.getStyleSheet().setCellXfs(ctXfs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style xfs
|
||||||
|
if(styleXfs.size() > 0) {
|
||||||
|
CTCellStyleXfs ctSXfs = CTCellStyleXfs.Factory.newInstance();
|
||||||
|
ctSXfs.setCount(styleXfs.size());
|
||||||
|
ctSXfs.setXfArray(
|
||||||
|
styleXfs.toArray(new CTXf[styleXfs.size()])
|
||||||
|
);
|
||||||
|
doc.getStyleSheet().setCellStyleXfs(ctSXfs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style dxfs
|
||||||
|
if(dxfs.size() > 0) {
|
||||||
|
CTDxfs ctDxfs = CTDxfs.Factory.newInstance();
|
||||||
|
ctDxfs.setCount(dxfs.size());
|
||||||
|
ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
|
||||||
|
);
|
||||||
|
doc.getStyleSheet().setDxfs(ctDxfs);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save
|
||||||
|
doc.save(out, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long putFont(XSSFFont font, ArrayList<CTFont> fonts) {
|
private long putBorder(XSSFCellBorder border, List<CTBorder> borders) {
|
||||||
return font.putFont(fonts);
|
return border.putBorder((LinkedList<CTBorder>) borders); // TODO - use List instead of LinkedList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private long putFont(XSSFFont font, List<CTFont> fonts) {
|
||||||
|
return font.putFont((ArrayList<CTFont>) fonts); // TODO - use List instead of ArrayList
|
||||||
|
}
|
||||||
|
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
//CTFont ctFont = createDefaultFont();
|
//CTFont ctFont = createDefaultFont();
|
||||||
XSSFFont xssfFont = createDefaultFont();
|
XSSFFont xssfFont = createDefaultFont();
|
||||||
fonts.add(xssfFont.getCTFont());
|
fonts.add(xssfFont.getCTFont());
|
||||||
|
|
||||||
CTFill[] ctFill = createDefaultFills();
|
CTFill[] ctFill = createDefaultFills();
|
||||||
fills.add(ctFill[0]);
|
fills.add(ctFill[0]);
|
||||||
fills.add(ctFill[1]);
|
fills.add(ctFill[1]);
|
||||||
|
|
||||||
CTBorder ctBorder = createDefaultBorder();
|
CTBorder ctBorder = createDefaultBorder();
|
||||||
borders.add(ctBorder);
|
borders.add(ctBorder);
|
||||||
|
|
||||||
CTXf styleXf = createDefaultXf();
|
CTXf styleXf = createDefaultXf();
|
||||||
styleXfs.add(styleXf);
|
styleXfs.add(styleXf);
|
||||||
CTXf xf = createDefaultXf();
|
CTXf xf = createDefaultXf();
|
||||||
xf.setXfId(0);
|
xf.setXfId(0);
|
||||||
xfs.add(xf);
|
xfs.add(xf);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CTXf createDefaultXf() {
|
private CTXf createDefaultXf() {
|
||||||
CTXf ctXf = CTXf.Factory.newInstance();
|
CTXf ctXf = CTXf.Factory.newInstance();
|
||||||
ctXf.setNumFmtId(0);
|
ctXf.setNumFmtId(0);
|
||||||
ctXf.setFontId(0);
|
ctXf.setFontId(0);
|
||||||
ctXf.setFillId(0);
|
ctXf.setFillId(0);
|
||||||
ctXf.setBorderId(0);
|
ctXf.setBorderId(0);
|
||||||
return ctXf;
|
return ctXf;
|
||||||
}
|
}
|
||||||
private CTBorder createDefaultBorder() {
|
private CTBorder createDefaultBorder() {
|
||||||
CTBorder ctBorder = CTBorder.Factory.newInstance();
|
CTBorder ctBorder = CTBorder.Factory.newInstance();
|
||||||
ctBorder.addNewBottom();
|
ctBorder.addNewBottom();
|
||||||
ctBorder.addNewTop();
|
ctBorder.addNewTop();
|
||||||
ctBorder.addNewLeft();
|
ctBorder.addNewLeft();
|
||||||
ctBorder.addNewRight();
|
ctBorder.addNewRight();
|
||||||
ctBorder.addNewDiagonal();
|
ctBorder.addNewDiagonal();
|
||||||
return ctBorder;
|
return ctBorder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private CTFill[] createDefaultFills() {
|
private CTFill[] createDefaultFills() {
|
||||||
CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
|
CTFill[] ctFill = new CTFill[]{CTFill.Factory.newInstance(),CTFill.Factory.newInstance()};
|
||||||
ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
|
ctFill[0].addNewPatternFill().setPatternType(STPatternType.NONE);
|
||||||
ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
|
ctFill[1].addNewPatternFill().setPatternType(STPatternType.DARK_GRAY);
|
||||||
return ctFill;
|
return ctFill;
|
||||||
}
|
}
|
||||||
|
|
||||||
private XSSFFont createDefaultFont() {
|
private XSSFFont createDefaultFont() {
|
||||||
CTFont ctFont = CTFont.Factory.newInstance();
|
CTFont ctFont = CTFont.Factory.newInstance();
|
||||||
XSSFFont xssfFont=new XSSFFont(ctFont);
|
XSSFFont xssfFont=new XSSFFont(ctFont);
|
||||||
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
|
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
|
||||||
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
|
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
|
||||||
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
|
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
|
||||||
xssfFont.setFamily(FontFamily.SWISS);
|
xssfFont.setFamily(FontFamily.SWISS);
|
||||||
xssfFont.setScheme(FontScheme.MINOR);
|
xssfFont.setScheme(FontScheme.MINOR);
|
||||||
return xssfFont;
|
return xssfFont;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public CTDxf getDxf(long idx) {
|
public CTDxf getDxf(long idx) {
|
||||||
if(dxfs.size()==0)
|
if(dxfs.size()==0)
|
||||||
return CTDxf.Factory.newInstance();
|
return CTDxf.Factory.newInstance();
|
||||||
else
|
else
|
||||||
return dxfs.get((int) idx);
|
return dxfs.get((int) idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public long putDxf(CTDxf dxf) {
|
public long putDxf(CTDxf dxf) {
|
||||||
this.dxfs.add(dxf);
|
this.dxfs.add(dxf);
|
||||||
return this.dxfs.size();
|
return this.dxfs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -77,12 +77,14 @@ public enum IndexedColors {
|
|||||||
BROWN(60),
|
BROWN(60),
|
||||||
PLUM(61),
|
PLUM(61),
|
||||||
INDIGO(62),
|
INDIGO(62),
|
||||||
GREY_80_PERCENT(63);
|
GREY_80_PERCENT(63),
|
||||||
|
AUTOMATIC(64),
|
||||||
|
;
|
||||||
|
|
||||||
private short index;
|
private int index;
|
||||||
|
|
||||||
IndexedColors(int idx){
|
IndexedColors(int idx){
|
||||||
index = (short)idx;
|
index = idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,6 +93,6 @@ public enum IndexedColors {
|
|||||||
* @return index of this color
|
* @return index of this color
|
||||||
*/
|
*/
|
||||||
public short getIndex(){
|
public short getIndex(){
|
||||||
return index;
|
return (short)index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,80 +16,94 @@
|
|||||||
==================================================================== */
|
==================================================================== */
|
||||||
package org.apache.poi.xssf.usermodel.extensions;
|
package org.apache.poi.xssf.usermodel.extensions;
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.poi.xssf.usermodel.IndexedColors;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType.Enum;
|
||||||
|
|
||||||
public class XSSFCellFill {
|
public final class XSSFCellFill {
|
||||||
|
|
||||||
private CTFill fill;
|
private CTFill _fill;
|
||||||
|
|
||||||
public XSSFCellFill(CTFill fill) {
|
public XSSFCellFill(CTFill fill) {
|
||||||
this.fill = fill;
|
_fill = fill;
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFCellFill() {
|
public XSSFCellFill() {
|
||||||
this.fill = CTFill.Factory.newInstance();
|
_fill = CTFill.Factory.newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFColor getFillBackgroundColor() {
|
public XSSFColor getFillBackgroundColor() {
|
||||||
return new XSSFColor(getPatternFill().getBgColor());
|
CTColor ctColor = getPatternFill().getBgColor();
|
||||||
|
if (ctColor == null) {
|
||||||
|
XSSFColor result = new XSSFColor();
|
||||||
|
result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return new XSSFColor(ctColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFColor getFillForegroundColor() {
|
public XSSFColor getFillForegroundColor() {
|
||||||
return new XSSFColor(getPatternFill().getFgColor());
|
CTColor ctColor = getPatternFill().getFgColor();
|
||||||
|
if (ctColor == null) {
|
||||||
|
XSSFColor result = new XSSFColor();
|
||||||
|
result.setIndexed(IndexedColors.AUTOMATIC.getIndex());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return new XSSFColor(ctColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Enum getPatternType() {
|
public Enum getPatternType() {
|
||||||
return getPatternFill().getPatternType();
|
return getPatternFill().getPatternType();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long putFill(LinkedList<CTFill> fills) {
|
/**
|
||||||
if (fills.contains(fill)) {
|
* @return the index of the just added fill
|
||||||
return fills.indexOf(fill);
|
*/
|
||||||
|
public int putFill(List<CTFill> fills) {
|
||||||
|
if (fills.contains(_fill)) {
|
||||||
|
return fills.indexOf(_fill);
|
||||||
}
|
}
|
||||||
fills.add(fill);
|
fills.add(_fill);
|
||||||
return fills.size() - 1;
|
return fills.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private CTPatternFill getPatternFill() {
|
private CTPatternFill getPatternFill() {
|
||||||
CTPatternFill patternFill = fill.getPatternFill();
|
CTPatternFill patternFill = _fill.getPatternFill();
|
||||||
if (patternFill == null) {
|
if (patternFill == null) {
|
||||||
patternFill = fill.addNewPatternFill();
|
patternFill = _fill.addNewPatternFill();
|
||||||
}
|
}
|
||||||
return patternFill;
|
return patternFill;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CTFill getCTFill() {
|
public CTFill getCTFill() {
|
||||||
return this.fill;
|
return _fill;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFillBackgroundColor(long index) {
|
public void setFillBackgroundColor(long index) {
|
||||||
CTColor ctColor=getPatternFill().addNewBgColor();
|
CTColor ctColor=getPatternFill().addNewBgColor();
|
||||||
ctColor.setIndexed(index);
|
ctColor.setIndexed(index);
|
||||||
fill.getPatternFill().setBgColor(ctColor);
|
_fill.getPatternFill().setBgColor(ctColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFillForegroundColor(long index) {
|
public void setFillForegroundColor(long index) {
|
||||||
CTColor ctColor=getPatternFill().addNewFgColor();
|
CTColor ctColor=getPatternFill().addNewFgColor();
|
||||||
ctColor.setIndexed(index);
|
ctColor.setIndexed(index);
|
||||||
fill.getPatternFill().setFgColor(ctColor);
|
_fill.getPatternFill().setFgColor(ctColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFillBackgroundRgbColor(XSSFColor color) {
|
public void setFillBackgroundRgbColor(XSSFColor color) {
|
||||||
fill.getPatternFill().setBgColor(color.getCTColor());
|
_fill.getPatternFill().setBgColor(color.getCTColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFillForegroundRgbColor(XSSFColor color) {
|
public void setFillForegroundRgbColor(XSSFColor color) {
|
||||||
fill.getPatternFill().setFgColor(color.getCTColor());
|
_fill.getPatternFill().setFgColor(color.getCTColor());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPatternType(Enum patternType) {
|
public void setPatternType(Enum patternType) {
|
||||||
getPatternFill().setPatternType(patternType);
|
getPatternFill().setPatternType(patternType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
|
import junit.framework.AssertionFailedError;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
@ -30,328 +31,362 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
|
|||||||
|
|
||||||
public class TestXSSFCellStyle extends TestCase {
|
public class TestXSSFCellStyle extends TestCase {
|
||||||
|
|
||||||
private StylesTable stylesTable;
|
private static final int AUTO_COLOR_INDEX = 64;
|
||||||
private CTBorder ctBorderA;
|
private StylesTable stylesTable;
|
||||||
private CTFill ctFill;
|
private CTBorder ctBorderA;
|
||||||
private CTFont ctFont;
|
private CTFill ctFill;
|
||||||
private CTXf cellStyleXf;
|
private CTFont ctFont;
|
||||||
private CTXf cellXf;
|
private CTXf cellStyleXf;
|
||||||
private CTCellXfs cellXfs;
|
private CTXf cellXf;
|
||||||
private XSSFCellStyle cellStyle;
|
private CTCellXfs cellXfs;
|
||||||
private CTStylesheet ctStylesheet;
|
private XSSFCellStyle cellStyle;
|
||||||
|
private CTStylesheet ctStylesheet;
|
||||||
|
|
||||||
public void setUp() {
|
@Override
|
||||||
stylesTable = new StylesTable();
|
protected void setUp() {
|
||||||
|
stylesTable = new StylesTable();
|
||||||
ctStylesheet = stylesTable._getRawStylesheet();
|
|
||||||
|
ctStylesheet = stylesTable._getRawStylesheet();
|
||||||
// Until we do XSSFBorder properly, cheat
|
|
||||||
ctBorderA = CTBorder.Factory.newInstance();
|
// Until we do XSSFBorder properly, cheat
|
||||||
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
|
ctBorderA = CTBorder.Factory.newInstance();
|
||||||
long borderId = stylesTable.putBorder(borderA);
|
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
|
||||||
assertEquals(1, borderId);
|
long borderId = stylesTable.putBorder(borderA);
|
||||||
|
assertEquals(1, borderId);
|
||||||
|
|
||||||
XSSFCellBorder borderB = new XSSFCellBorder();
|
XSSFCellBorder borderB = new XSSFCellBorder();
|
||||||
assertEquals(2, stylesTable.putBorder(borderB));
|
assertEquals(2, stylesTable.putBorder(borderB));
|
||||||
|
|
||||||
ctFill = CTFill.Factory.newInstance();
|
ctFill = CTFill.Factory.newInstance();
|
||||||
XSSFCellFill fill = new XSSFCellFill(ctFill);
|
XSSFCellFill fill = new XSSFCellFill(ctFill);
|
||||||
long fillId = stylesTable.putFill(fill);
|
long fillId = stylesTable.putFill(fill);
|
||||||
assertEquals(2, fillId);
|
assertEquals(2, fillId);
|
||||||
|
|
||||||
ctFont = CTFont.Factory.newInstance();
|
ctFont = CTFont.Factory.newInstance();
|
||||||
XSSFFont font = new XSSFFont(ctFont);
|
XSSFFont font = new XSSFFont(ctFont);
|
||||||
long fontId = stylesTable.putFont(font);
|
long fontId = stylesTable.putFont(font);
|
||||||
assertEquals(1, fontId);
|
assertEquals(1, fontId);
|
||||||
|
|
||||||
cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
|
cellStyleXf = ctStylesheet.addNewCellStyleXfs().addNewXf();
|
||||||
cellStyleXf.setBorderId(1);
|
cellStyleXf.setBorderId(1);
|
||||||
cellStyleXf.setFillId(1);
|
cellStyleXf.setFillId(1);
|
||||||
cellStyleXf.setFontId(1);
|
cellStyleXf.setFontId(1);
|
||||||
|
|
||||||
cellXfs = ctStylesheet.addNewCellXfs();
|
cellXfs = ctStylesheet.addNewCellXfs();
|
||||||
cellXf = cellXfs.addNewXf();
|
cellXf = cellXfs.addNewXf();
|
||||||
cellXf.setXfId(1);
|
cellXf.setXfId(1);
|
||||||
cellXf.setBorderId(1);
|
cellXf.setBorderId(1);
|
||||||
cellXf.setFillId(1);
|
cellXf.setFillId(1);
|
||||||
cellXf.setFontId(1);
|
cellXf.setFontId(1);
|
||||||
stylesTable.putCellStyleXf(cellStyleXf);
|
stylesTable.putCellStyleXf(cellStyleXf);
|
||||||
long id=stylesTable.putCellXf(cellXf);
|
stylesTable.putCellXf(cellXf);
|
||||||
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
|
cellStyle = new XSSFCellStyle(1, 1, stylesTable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetBorderBottom() {
|
public void testGetSetBorderBottom() {
|
||||||
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
|
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
|
||||||
assertEquals((short)1, cellStyle.getBorderBottom());
|
assertEquals((short)1, cellStyle.getBorderBottom());
|
||||||
cellStyle.setBorderBottom((short) 2);
|
cellStyle.setBorderBottom((short) 2);
|
||||||
assertEquals(STBorderStyle.THIN, ctBorderA.getBottom().getStyle());
|
assertEquals(STBorderStyle.THIN, ctBorderA.getBottom().getStyle());
|
||||||
cellStyle.setBorderBottomEnum(STBorderStyle.THICK);
|
cellStyle.setBorderBottomEnum(STBorderStyle.THICK);
|
||||||
assertEquals(6, ctBorderA.getBottom().getStyle().intValue());
|
assertEquals(6, ctBorderA.getBottom().getStyle().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetBorderBottomAsString() {
|
public void testGetBorderBottomAsString() {
|
||||||
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
|
ctBorderA.addNewBottom().setStyle(STBorderStyle.THIN);
|
||||||
assertEquals("thin", cellStyle.getBorderBottomAsString());
|
assertEquals("thin", cellStyle.getBorderBottomAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetBorderRight() {
|
public void testGetSetBorderRight() {
|
||||||
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
|
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
|
||||||
assertEquals((short)2, cellStyle.getBorderRight());
|
assertEquals((short)2, cellStyle.getBorderRight());
|
||||||
cellStyle.setBorderRight((short) 2);
|
cellStyle.setBorderRight((short) 2);
|
||||||
assertEquals(STBorderStyle.THIN, ctBorderA.getRight().getStyle());
|
assertEquals(STBorderStyle.THIN, ctBorderA.getRight().getStyle());
|
||||||
cellStyle.setBorderRightEnum(STBorderStyle.THICK);
|
cellStyle.setBorderRightEnum(STBorderStyle.THICK);
|
||||||
assertEquals(6, ctBorderA.getRight().getStyle().intValue());
|
assertEquals(6, ctBorderA.getRight().getStyle().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetBorderRightAsString() {
|
public void testGetBorderRightAsString() {
|
||||||
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
|
ctBorderA.addNewRight().setStyle(STBorderStyle.MEDIUM);
|
||||||
assertEquals("medium", cellStyle.getBorderRightAsString());
|
assertEquals("medium", cellStyle.getBorderRightAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetBorderLeft() {
|
public void testGetSetBorderLeft() {
|
||||||
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
|
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
|
||||||
assertEquals((short)3, cellStyle.getBorderLeft());
|
assertEquals((short)3, cellStyle.getBorderLeft());
|
||||||
cellStyle.setBorderLeft((short) 2);
|
cellStyle.setBorderLeft((short) 2);
|
||||||
assertEquals(STBorderStyle.THIN, ctBorderA.getLeft().getStyle());
|
assertEquals(STBorderStyle.THIN, ctBorderA.getLeft().getStyle());
|
||||||
cellStyle.setBorderLeftEnum(STBorderStyle.THICK);
|
cellStyle.setBorderLeftEnum(STBorderStyle.THICK);
|
||||||
assertEquals(6, ctBorderA.getLeft().getStyle().intValue());
|
assertEquals(6, ctBorderA.getLeft().getStyle().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetBorderLeftAsString() {
|
public void testGetBorderLeftAsString() {
|
||||||
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
|
ctBorderA.addNewLeft().setStyle(STBorderStyle.DASHED);
|
||||||
assertEquals("dashed", cellStyle.getBorderLeftAsString());
|
assertEquals("dashed", cellStyle.getBorderLeftAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetBorderTop() {
|
public void testGetSetBorderTop() {
|
||||||
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
|
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
|
||||||
assertEquals((short)7, cellStyle.getBorderTop());
|
assertEquals((short)7, cellStyle.getBorderTop());
|
||||||
cellStyle.setBorderTop((short) 2);
|
cellStyle.setBorderTop((short) 2);
|
||||||
assertEquals(STBorderStyle.THIN, ctBorderA.getTop().getStyle());
|
assertEquals(STBorderStyle.THIN, ctBorderA.getTop().getStyle());
|
||||||
cellStyle.setBorderTopEnum(STBorderStyle.THICK);
|
cellStyle.setBorderTopEnum(STBorderStyle.THICK);
|
||||||
assertEquals(6, ctBorderA.getTop().getStyle().intValue());
|
assertEquals(6, ctBorderA.getTop().getStyle().intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetBorderTopAsString() {
|
public void testGetBorderTopAsString() {
|
||||||
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
|
ctBorderA.addNewTop().setStyle(STBorderStyle.HAIR);
|
||||||
assertEquals("hair", cellStyle.getBorderTopAsString());
|
assertEquals("hair", cellStyle.getBorderTopAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSetBottomBorderColor() {
|
public void testGetSetBottomBorderColor() {
|
||||||
CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
|
CTColor ctColor = ctBorderA.addNewBottom().addNewColor();
|
||||||
ctColor.setIndexed(2);
|
ctColor.setIndexed(2);
|
||||||
XSSFColor color = new XSSFColor(ctColor);
|
assertEquals((short)2, cellStyle.getBottomBorderColor());
|
||||||
assertEquals((short)2, cellStyle.getBottomBorderColor());
|
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
||||||
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
anotherCtColor.setIndexed(4);
|
||||||
anotherCtColor.setIndexed(4);
|
anotherCtColor.setTheme(3);
|
||||||
anotherCtColor.setTheme(3);
|
anotherCtColor.setRgb("1234".getBytes());
|
||||||
anotherCtColor.setRgb("1234".getBytes());
|
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
||||||
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
|
||||||
cellStyle.setBorderColor(BorderSide.BOTTOM, anotherColor);
|
assertEquals((short)4, cellStyle.getBottomBorderColor());
|
||||||
assertEquals((short)4, cellStyle.getBottomBorderColor());
|
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
|
||||||
assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.BOTTOM).getRgb()));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetTopBorderColor() {
|
public void testGetSetTopBorderColor() {
|
||||||
CTColor ctColor = ctBorderA.addNewTop().addNewColor();
|
CTColor ctColor = ctBorderA.addNewTop().addNewColor();
|
||||||
ctColor.setIndexed(5);
|
ctColor.setIndexed(5);
|
||||||
XSSFColor color = new XSSFColor(ctColor);
|
assertEquals((short)5, cellStyle.getTopBorderColor());
|
||||||
assertEquals((short)5, cellStyle.getTopBorderColor());
|
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
||||||
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
anotherCtColor.setIndexed(7);
|
||||||
anotherCtColor.setIndexed(7);
|
anotherCtColor.setTheme(3);
|
||||||
anotherCtColor.setTheme(3);
|
anotherCtColor.setRgb("abcd".getBytes());
|
||||||
anotherCtColor.setRgb("abcd".getBytes());
|
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
||||||
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
|
||||||
cellStyle.setBorderColor(BorderSide.TOP, anotherColor);
|
assertEquals((short)7, cellStyle.getTopBorderColor());
|
||||||
assertEquals((short)7, cellStyle.getTopBorderColor());
|
assertEquals("abcd", new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
|
||||||
assertEquals(new String("abcd".getBytes()), new String(cellStyle.getBorderColor(BorderSide.TOP).getRgb()));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetLeftBorderColor() {
|
public void testGetSetLeftBorderColor() {
|
||||||
CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
|
CTColor ctColor = ctBorderA.addNewLeft().addNewColor();
|
||||||
ctColor.setIndexed(2);
|
ctColor.setIndexed(2);
|
||||||
XSSFColor color = new XSSFColor(ctColor);
|
assertEquals((short)2, cellStyle.getLeftBorderColor());
|
||||||
assertEquals((short)2, cellStyle.getLeftBorderColor());
|
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
||||||
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
anotherCtColor.setIndexed(4);
|
||||||
anotherCtColor.setIndexed(4);
|
anotherCtColor.setTheme(3);
|
||||||
anotherCtColor.setTheme(3);
|
anotherCtColor.setRgb("1234".getBytes());
|
||||||
anotherCtColor.setRgb("1234".getBytes());
|
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
||||||
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
|
||||||
cellStyle.setBorderColor(BorderSide.LEFT, anotherColor);
|
assertEquals((short)4, cellStyle.getLeftBorderColor());
|
||||||
assertEquals((short)4, cellStyle.getLeftBorderColor());
|
assertEquals("1234", new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
|
||||||
assertEquals(new String("1234".getBytes()), new String(cellStyle.getBorderColor(BorderSide.LEFT).getRgb()));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetRightBorderColor() {
|
public void testGetSetRightBorderColor() {
|
||||||
CTColor ctColor = ctBorderA.addNewRight().addNewColor();
|
CTColor ctColor = ctBorderA.addNewRight().addNewColor();
|
||||||
ctColor.setIndexed(8);
|
ctColor.setIndexed(8);
|
||||||
XSSFColor color = new XSSFColor(ctColor);
|
assertEquals((short)8, cellStyle.getRightBorderColor());
|
||||||
assertEquals((short)8, cellStyle.getRightBorderColor());
|
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
||||||
CTColor anotherCtColor = CTColor.Factory.newInstance();
|
anotherCtColor.setIndexed(14);
|
||||||
anotherCtColor.setIndexed(14);
|
anotherCtColor.setTheme(3);
|
||||||
anotherCtColor.setTheme(3);
|
anotherCtColor.setRgb("af67".getBytes());
|
||||||
anotherCtColor.setRgb("af67".getBytes());
|
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
||||||
XSSFColor anotherColor = new XSSFColor(anotherCtColor);
|
cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
|
||||||
cellStyle.setBorderColor(BorderSide.RIGHT, anotherColor);
|
assertEquals((short)14, cellStyle.getRightBorderColor());
|
||||||
assertEquals((short)14, cellStyle.getRightBorderColor());
|
assertEquals("af67", new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
|
||||||
assertEquals(new String("af67".getBytes()), new String(cellStyle.getBorderColor(BorderSide.RIGHT).getRgb()));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetFillBackgroundColor() {
|
public void testGetFillBackgroundColor() {
|
||||||
setUp();
|
|
||||||
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
|
||||||
CTColor ctBgColor = ctPatternFill.addNewBgColor();
|
|
||||||
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
|
|
||||||
ctPatternFill.setBgColor(ctBgColor);
|
|
||||||
|
|
||||||
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
||||||
long index=stylesTable.putFill(cellFill);
|
CTColor ctBgColor = ctPatternFill.addNewBgColor();
|
||||||
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
|
ctBgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
|
||||||
|
ctPatternFill.setBgColor(ctBgColor);
|
||||||
|
|
||||||
assertEquals(2,cellStyle.getCoreXf().getFillId());
|
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
||||||
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
|
long index=stylesTable.putFill(cellFill);
|
||||||
|
cellStyle.getCoreXf().setFillId(index);
|
||||||
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
|
|
||||||
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
|
|
||||||
|
|
||||||
//test rgb color - XSSFColor
|
|
||||||
CTColor ctColor=CTColor.Factory.newInstance();
|
|
||||||
ctColor.setRgb("FFFFFF".getBytes());
|
|
||||||
ctPatternFill.setBgColor(ctColor);
|
|
||||||
assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
|
|
||||||
|
|
||||||
cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
|
|
||||||
assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
|
|
||||||
assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
|
|
||||||
assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
|
|
||||||
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetFillForegroundColor() {
|
assertEquals(2,cellStyle.getCoreXf().getFillId());
|
||||||
setUp();
|
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillBackgroundColor());
|
||||||
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
|
||||||
CTColor ctFgColor = ctPatternFill.addNewFgColor();
|
cellStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex());
|
||||||
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
|
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getBgColor().getIndexed());
|
||||||
ctPatternFill.setFgColor(ctFgColor);
|
|
||||||
|
//test rgb color - XSSFColor
|
||||||
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
CTColor ctColor=CTColor.Factory.newInstance();
|
||||||
long index=stylesTable.putFill(cellFill);
|
ctColor.setRgb("FFFFFF".getBytes());
|
||||||
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
|
ctPatternFill.setBgColor(ctColor);
|
||||||
|
assertEquals(ctColor.toString(), cellStyle.getFillBackgroundRgbColor().getCTColor().toString());
|
||||||
assertEquals(2,cellStyle.getCoreXf().getFillId());
|
|
||||||
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
|
cellStyle.setFillBackgroundRgbColor(new XSSFColor(ctColor));
|
||||||
|
assertEquals(ctColor.getRgb()[0], ctPatternFill.getBgColor().getRgb()[0]);
|
||||||
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
|
assertEquals(ctColor.getRgb()[1], ctPatternFill.getBgColor().getRgb()[1]);
|
||||||
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
|
assertEquals(ctColor.getRgb()[2], ctPatternFill.getBgColor().getRgb()[2]);
|
||||||
|
assertEquals(ctColor.getRgb()[3], ctPatternFill.getBgColor().getRgb()[3]);
|
||||||
//test rgb color - XSSFColor
|
}
|
||||||
CTColor ctColor=CTColor.Factory.newInstance();
|
|
||||||
ctColor.setRgb("FFFFFF".getBytes());
|
|
||||||
ctPatternFill.setFgColor(ctColor);
|
|
||||||
assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
|
|
||||||
|
|
||||||
cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
|
|
||||||
assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
|
|
||||||
assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
|
|
||||||
assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
|
|
||||||
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetFillPattern() {
|
|
||||||
setUp();
|
|
||||||
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
|
||||||
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
|
|
||||||
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
|
||||||
long index=stylesTable.putFill(cellFill);
|
|
||||||
((XSSFCellStyle)cellStyle).getCoreXf().setFillId(index);
|
|
||||||
|
|
||||||
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
|
public void testGetFillBackgroundColor_default() {
|
||||||
|
|
||||||
cellStyle.setFillPattern(CellStyle.BRICKS);
|
|
||||||
assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetFont() {
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
assertNotNull(cellStyle.getFont());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetHidden() {
|
XSSFCellStyle style = wb.createCellStyle();
|
||||||
assertFalse(cellStyle.getHidden());
|
|
||||||
cellXf.getProtection().setHidden(true);
|
|
||||||
assertTrue(cellStyle.getHidden());
|
|
||||||
cellStyle.setHidden(false);
|
|
||||||
assertFalse(cellStyle.getHidden());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetLocked() {
|
short color;
|
||||||
assertFalse(cellStyle.getLocked());
|
try {
|
||||||
cellXf.getProtection().setLocked(true);
|
color = style.getFillBackgroundColor();
|
||||||
assertTrue(cellStyle.getLocked());
|
} catch (NullPointerException e) {
|
||||||
cellStyle.setLocked(false);
|
throw new AssertionFailedError("Identified bug 45898");
|
||||||
assertFalse(cellStyle.getLocked());
|
}
|
||||||
}
|
assertEquals(AUTO_COLOR_INDEX, color);
|
||||||
|
XSSFColor xcolor=style.getFillBackgroundRgbColor();
|
||||||
|
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testGetSetIndent() {
|
public void testGetFillForegroundColor() {
|
||||||
assertEquals((short)0, cellStyle.getIndention());
|
|
||||||
cellXf.getAlignment().setIndent(3);
|
|
||||||
assertEquals((short)3, cellStyle.getIndention());
|
|
||||||
cellStyle.setIndention((short) 13);
|
|
||||||
assertEquals((short)13, cellXf.getAlignment().getIndent());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetAlignement() {
|
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
||||||
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
CTColor ctFgColor = ctPatternFill.addNewFgColor();
|
||||||
assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
|
ctFgColor.setIndexed(IndexedColors.BRIGHT_GREEN.getIndex());
|
||||||
|
ctPatternFill.setFgColor(ctFgColor);
|
||||||
|
|
||||||
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
|
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
||||||
assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
|
long index=stylesTable.putFill(cellFill);
|
||||||
assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
|
cellStyle.getCoreXf().setFillId(index);
|
||||||
assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
||||||
|
|
||||||
cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
|
assertEquals(2,cellStyle.getCoreXf().getFillId());
|
||||||
assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
|
assertEquals(IndexedColors.BRIGHT_GREEN.getIndex(), cellStyle.getFillForegroundColor());
|
||||||
assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
|
|
||||||
assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
cellStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex());
|
||||||
|
assertEquals(IndexedColors.BLUE.getIndex(), ctFill.getPatternFill().getFgColor().getIndexed());
|
||||||
|
|
||||||
|
//test rgb color - XSSFColor
|
||||||
|
CTColor ctColor=CTColor.Factory.newInstance();
|
||||||
|
ctColor.setRgb("FFFFFF".getBytes());
|
||||||
|
ctPatternFill.setFgColor(ctColor);
|
||||||
|
assertEquals(ctColor.toString(), cellStyle.getFillForegroundRgbColor().getCTColor().toString());
|
||||||
|
|
||||||
|
cellStyle.setFillForegroundRgbColor(new XSSFColor(ctColor));
|
||||||
|
assertEquals(ctColor.getRgb()[0], ctPatternFill.getFgColor().getRgb()[0]);
|
||||||
|
assertEquals(ctColor.getRgb()[1], ctPatternFill.getFgColor().getRgb()[1]);
|
||||||
|
assertEquals(ctColor.getRgb()[2], ctPatternFill.getFgColor().getRgb()[2]);
|
||||||
|
assertEquals(ctColor.getRgb()[3], ctPatternFill.getFgColor().getRgb()[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetFillForegroundColor_default() {
|
||||||
|
|
||||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
|
|
||||||
assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
|
|
||||||
assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetVerticalAlignment() {
|
XSSFCellStyle style = wb.createCellStyle();
|
||||||
assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
|
|
||||||
assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
|
|
||||||
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
|
||||||
|
|
||||||
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
|
short color;
|
||||||
assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
|
try {
|
||||||
assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
|
color = style.getFillForegroundColor();
|
||||||
assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
} catch (NullPointerException e) {
|
||||||
|
throw new AssertionFailedError("Identified bug 45898");
|
||||||
|
}
|
||||||
|
assertEquals(AUTO_COLOR_INDEX, color);
|
||||||
|
XSSFColor xcolor=style.getFillForegroundRgbColor();
|
||||||
|
assertEquals(xcolor.getIndexed(), AUTO_COLOR_INDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
|
public void testGetFillPattern() {
|
||||||
assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
|
|
||||||
assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
|
|
||||||
assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testGetSetWrapText() {
|
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
|
||||||
assertFalse(cellStyle.getWrapText());
|
ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
|
||||||
cellXf.getAlignment().setWrapText(true);
|
XSSFCellFill cellFill=new XSSFCellFill(ctFill);
|
||||||
assertTrue(cellStyle.getWrapText());
|
long index=stylesTable.putFill(cellFill);
|
||||||
cellStyle.setWrapText(false);
|
cellStyle.getCoreXf().setFillId(index);
|
||||||
assertFalse(cellXf.getAlignment().getWrapText());
|
|
||||||
}
|
assertEquals(CellStyle.THICK_FORWARD_DIAG, cellStyle.getFillPattern());
|
||||||
|
|
||||||
|
cellStyle.setFillPattern(CellStyle.BRICKS);
|
||||||
|
assertEquals(STPatternType.INT_DARK_TRELLIS,ctPatternFill.getPatternType().intValue());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
public void testGetFont() {
|
||||||
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
|
assertNotNull(cellStyle.getFont());
|
||||||
*/
|
}
|
||||||
public void testCloneStyleSameWB() throws Exception {
|
|
||||||
// TODO
|
public void testGetSetHidden() {
|
||||||
}
|
assertFalse(cellStyle.getHidden());
|
||||||
/**
|
cellXf.getProtection().setHidden(true);
|
||||||
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
|
assertTrue(cellStyle.getHidden());
|
||||||
*/
|
cellStyle.setHidden(false);
|
||||||
public void testCloneStyleDiffWB() throws Exception {
|
assertFalse(cellStyle.getHidden());
|
||||||
// TODO
|
}
|
||||||
}
|
|
||||||
|
public void testGetSetLocked() {
|
||||||
|
assertFalse(cellStyle.getLocked());
|
||||||
|
cellXf.getProtection().setLocked(true);
|
||||||
|
assertTrue(cellStyle.getLocked());
|
||||||
|
cellStyle.setLocked(false);
|
||||||
|
assertFalse(cellStyle.getLocked());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetSetIndent() {
|
||||||
|
assertEquals((short)0, cellStyle.getIndention());
|
||||||
|
cellXf.getAlignment().setIndent(3);
|
||||||
|
assertEquals((short)3, cellStyle.getIndention());
|
||||||
|
cellStyle.setIndention((short) 13);
|
||||||
|
assertEquals((short)13, cellXf.getAlignment().getIndent());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetSetAlignement() {
|
||||||
|
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
||||||
|
assertEquals(HorizontalAlignment.GENERAL, cellStyle.getAlignmentEnum());
|
||||||
|
|
||||||
|
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
|
||||||
|
assertEquals(XSSFCellStyle.ALIGN_LEFT, cellStyle.getAlignment());
|
||||||
|
assertEquals(HorizontalAlignment.LEFT, cellStyle.getAlignmentEnum());
|
||||||
|
assertEquals(STHorizontalAlignment.LEFT, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
||||||
|
|
||||||
|
cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
|
||||||
|
assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
|
||||||
|
assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
|
||||||
|
assertEquals(STHorizontalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
||||||
|
|
||||||
|
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
assertEquals(XSSFCellStyle.ALIGN_CENTER, cellStyle.getAlignment());
|
||||||
|
assertEquals(HorizontalAlignment.CENTER, cellStyle.getAlignmentEnum());
|
||||||
|
assertEquals(STHorizontalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getHorizontal());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetSetVerticalAlignment() {
|
||||||
|
assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
|
||||||
|
assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
|
||||||
|
assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
||||||
|
|
||||||
|
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
|
||||||
|
assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
|
||||||
|
assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
|
||||||
|
assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
||||||
|
|
||||||
|
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
|
||||||
|
assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
|
||||||
|
assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
|
||||||
|
assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testGetSetWrapText() {
|
||||||
|
assertFalse(cellStyle.getWrapText());
|
||||||
|
cellXf.getAlignment().setWrapText(true);
|
||||||
|
assertTrue(cellStyle.getWrapText());
|
||||||
|
cellStyle.setWrapText(false);
|
||||||
|
assertFalse(cellXf.getAlignment().getWrapText());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
|
||||||
|
*/
|
||||||
|
public void testCloneStyleSameWB() throws Exception {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
|
||||||
|
*/
|
||||||
|
public void testCloneStyleDiffWB() throws Exception {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,20 +17,21 @@
|
|||||||
|
|
||||||
package org.apache.poi.xssf.usermodel;
|
package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
import org.apache.poi.ss.usermodel.Font;
|
import org.apache.poi.ss.usermodel.Font;
|
||||||
import org.apache.poi.ss.usermodel.Name;
|
import org.apache.poi.ss.usermodel.Name;
|
||||||
import org.apache.poi.ss.usermodel.RichTextString;
|
import org.apache.poi.ss.usermodel.RichTextString;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.StylesSource;
|
import org.apache.poi.ss.usermodel.StylesSource;
|
||||||
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||||
import org.apache.poi.xssf.model.StylesTable;
|
import org.apache.poi.xssf.model.StylesTable;
|
||||||
import org.openxml4j.opc.ContentTypes;
|
import org.openxml4j.opc.ContentTypes;
|
||||||
import org.openxml4j.opc.Package;
|
import org.openxml4j.opc.Package;
|
||||||
@ -39,323 +40,309 @@ import org.openxml4j.opc.PackagingURIHelper;
|
|||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet;
|
||||||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
|
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook;
|
||||||
|
|
||||||
public class TestXSSFWorkbook extends TestCase {
|
public final class TestXSSFWorkbook extends TestCase {
|
||||||
public TestXSSFWorkbook(String name) {
|
|
||||||
super(name);
|
@Override
|
||||||
|
protected void setUp() throws Exception {
|
||||||
// Use system out logger
|
// Use system out logger
|
||||||
System.setProperty(
|
System.setProperty(
|
||||||
"org.apache.poi.util.POILogger",
|
"org.apache.poi.util.POILogger",
|
||||||
"org.apache.poi.util.SystemOutLogger"
|
"org.apache.poi.util.SystemOutLogger"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSheetIndex() {
|
public void testGetSheetIndex() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
Sheet sheet1 = workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
Sheet sheet2 = workbook.createSheet("sheet2");
|
||||||
assertEquals(0, workbook.getSheetIndex(sheet1));
|
assertEquals(0, workbook.getSheetIndex(sheet1));
|
||||||
assertEquals(0, workbook.getSheetIndex("sheet1"));
|
assertEquals(0, workbook.getSheetIndex("sheet1"));
|
||||||
assertEquals(1, workbook.getSheetIndex(sheet2));
|
assertEquals(1, workbook.getSheetIndex(sheet2));
|
||||||
assertEquals(1, workbook.getSheetIndex("sheet2"));
|
assertEquals(1, workbook.getSheetIndex("sheet2"));
|
||||||
assertEquals(-1, workbook.getSheetIndex("noSheet"));
|
assertEquals(-1, workbook.getSheetIndex("noSheet"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetSheetOrder() throws Exception {
|
public void testSetSheetOrder() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
Sheet sheet1 = workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
Sheet sheet2 = workbook.createSheet("sheet2");
|
||||||
assertSame(sheet1, workbook.getSheetAt(0));
|
assertSame(sheet1, workbook.getSheetAt(0));
|
||||||
assertSame(sheet2, workbook.getSheetAt(1));
|
assertSame(sheet2, workbook.getSheetAt(1));
|
||||||
workbook.setSheetOrder("sheet2", 0);
|
workbook.setSheetOrder("sheet2", 0);
|
||||||
assertSame(sheet2, workbook.getSheetAt(0));
|
assertSame(sheet2, workbook.getSheetAt(0));
|
||||||
assertSame(sheet1, workbook.getSheetAt(1));
|
assertSame(sheet1, workbook.getSheetAt(1));
|
||||||
// Test reordering of CTSheets
|
// Test reordering of CTSheets
|
||||||
CTWorkbook ctwb = workbook.getWorkbook();
|
CTWorkbook ctwb = workbook.getWorkbook();
|
||||||
CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
|
CTSheet[] ctsheets = ctwb.getSheets().getSheetArray();
|
||||||
assertEquals("sheet2", ctsheets[0].getName());
|
assertEquals("sheet2", ctsheets[0].getName());
|
||||||
assertEquals("sheet1", ctsheets[1].getName());
|
assertEquals("sheet1", ctsheets[1].getName());
|
||||||
|
|
||||||
// Borderline case: only one sheet
|
// Borderline case: only one sheet
|
||||||
workbook = new XSSFWorkbook();
|
workbook = new XSSFWorkbook();
|
||||||
sheet1 = workbook.createSheet("sheet1");
|
sheet1 = workbook.createSheet("sheet1");
|
||||||
assertSame(sheet1, workbook.getSheetAt(0));
|
assertSame(sheet1, workbook.getSheetAt(0));
|
||||||
workbook.setSheetOrder("sheet1", 0);
|
workbook.setSheetOrder("sheet1", 0);
|
||||||
assertSame(sheet1, workbook.getSheetAt(0));
|
assertSame(sheet1, workbook.getSheetAt(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetSelectedTab() throws Exception {
|
public void testSetSelectedTab() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
workbook.createSheet("sheet2");
|
||||||
assertEquals(0, workbook.getSelectedTab());
|
assertEquals(0, workbook.getSelectedTab());
|
||||||
workbook.setSelectedTab((short) 0);
|
workbook.setSelectedTab((short) 0);
|
||||||
assertEquals(0, workbook.getSelectedTab());
|
assertEquals(0, workbook.getSelectedTab());
|
||||||
workbook.setSelectedTab((short) 1);
|
workbook.setSelectedTab((short) 1);
|
||||||
assertEquals(1, workbook.getSelectedTab());
|
assertEquals(1, workbook.getSelectedTab());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetSheetName() throws Exception {
|
public void testSetSheetName() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
workbook.createSheet("sheet1");
|
||||||
assertEquals("sheet1", workbook.getSheetName(0));
|
assertEquals("sheet1", workbook.getSheetName(0));
|
||||||
workbook.setSheetName(0, "sheet2");
|
workbook.setSheetName(0, "sheet2");
|
||||||
assertEquals("sheet2", workbook.getSheetName(0));
|
assertEquals("sheet2", workbook.getSheetName(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCloneSheet() throws Exception {
|
public void testCloneSheet() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet");
|
workbook.createSheet("sheet");
|
||||||
Sheet sheet2 = workbook.cloneSheet(0);
|
workbook.cloneSheet(0);
|
||||||
assertEquals(2, workbook.getNumberOfSheets());
|
assertEquals(2, workbook.getNumberOfSheets());
|
||||||
assertEquals("sheet(1)", workbook.getSheetName(1));
|
assertEquals("sheet(1)", workbook.getSheetName(1));
|
||||||
workbook.setSheetName(1, "clonedsheet");
|
workbook.setSheetName(1, "clonedsheet");
|
||||||
Sheet sheet3 = workbook.cloneSheet(1);
|
workbook.cloneSheet(1);
|
||||||
assertEquals(3, workbook.getNumberOfSheets());
|
assertEquals(3, workbook.getNumberOfSheets());
|
||||||
assertEquals("clonedsheet(1)", workbook.getSheetName(2));
|
assertEquals("clonedsheet(1)", workbook.getSheetName(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSheetByName() {
|
public void testGetSheetByName() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
Sheet sheet1 = workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
Sheet sheet2 = workbook.createSheet("sheet2");
|
||||||
assertSame(sheet1, workbook.getSheet("sheet1"));
|
assertSame(sheet1, workbook.getSheet("sheet1"));
|
||||||
assertSame(sheet2, workbook.getSheet("sheet2"));
|
assertSame(sheet2, workbook.getSheet("sheet2"));
|
||||||
assertNull(workbook.getSheet("nosheet"));
|
assertNull(workbook.getSheet("nosheet"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRemoveSheetAt() throws Exception {
|
public void testRemoveSheetAt() {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
workbook.createSheet("sheet2");
|
||||||
Sheet sheet3 = workbook.createSheet("sheet3");
|
workbook.createSheet("sheet3");
|
||||||
workbook.removeSheetAt(1);
|
workbook.removeSheetAt(1);
|
||||||
assertEquals(2, workbook.getNumberOfSheets());
|
assertEquals(2, workbook.getNumberOfSheets());
|
||||||
assertEquals("sheet3", workbook.getSheetName(1));
|
assertEquals("sheet3", workbook.getSheetName(1));
|
||||||
workbook.removeSheetAt(0);
|
workbook.removeSheetAt(0);
|
||||||
assertEquals(1, workbook.getNumberOfSheets());
|
assertEquals(1, workbook.getNumberOfSheets());
|
||||||
assertEquals("sheet3", workbook.getSheetName(0));
|
assertEquals("sheet3", workbook.getSheetName(0));
|
||||||
workbook.removeSheetAt(0);
|
workbook.removeSheetAt(0);
|
||||||
assertEquals(0, workbook.getNumberOfSheets());
|
assertEquals(0, workbook.getNumberOfSheets());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that we can save a new document
|
* Tests that we can save a new document
|
||||||
*/
|
*/
|
||||||
public void testSaveNew() throws Exception {
|
public void testSaveNew() throws IOException {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
workbook.createSheet("sheet2");
|
||||||
Sheet sheet3 = workbook.createSheet("sheet3");
|
workbook.createSheet("sheet3");
|
||||||
File file = File.createTempFile("poi-", ".xlsx");
|
File file = File.createTempFile("poi-", ".xlsx");
|
||||||
System.out.println("Saving newly created file to " + file.getAbsolutePath());
|
System.out.println("Saving newly created file to " + file.getAbsolutePath());
|
||||||
OutputStream out = new FileOutputStream(file);
|
OutputStream out = new FileOutputStream(file);
|
||||||
workbook.write(out);
|
workbook.write(out);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests that we can save, and then re-load a new document
|
* Tests that we can save, and then re-load a new document
|
||||||
*/
|
*/
|
||||||
public void testSaveLoadNew() throws Exception {
|
public void testSaveLoadNew() throws Exception {
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Sheet sheet1 = workbook.createSheet("sheet1");
|
Sheet sheet1 = workbook.createSheet("sheet1");
|
||||||
Sheet sheet2 = workbook.createSheet("sheet2");
|
Sheet sheet2 = workbook.createSheet("sheet2");
|
||||||
Sheet sheet3 = workbook.createSheet("sheet3");
|
workbook.createSheet("sheet3");
|
||||||
|
|
||||||
RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
|
RichTextString rts = workbook.getCreationHelper().createRichTextString("hello world");
|
||||||
|
|
||||||
sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
|
sheet1.createRow(0).createCell((short)0).setCellValue(1.2);
|
||||||
sheet1.createRow(1).createCell((short)0).setCellValue(rts);
|
sheet1.createRow(1).createCell((short)0).setCellValue(rts);
|
||||||
sheet2.createRow(0);
|
sheet2.createRow(0);
|
||||||
|
|
||||||
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
|
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
|
||||||
assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
|
assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
|
||||||
assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
|
assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
|
||||||
assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
|
assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
|
||||||
assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
|
assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
|
||||||
assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
|
assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
|
||||||
|
|
||||||
File file = File.createTempFile("poi-", ".xlsx");
|
File file = File.createTempFile("poi-", ".xlsx");
|
||||||
OutputStream out = new FileOutputStream(file);
|
OutputStream out = new FileOutputStream(file);
|
||||||
workbook.write(out);
|
workbook.write(out);
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
// Check the package contains what we'd expect it to
|
// Check the package contains what we'd expect it to
|
||||||
Package pkg = Package.open(file.toString());
|
Package pkg = Package.open(file.toString());
|
||||||
PackagePart wbRelPart =
|
PackagePart wbRelPart =
|
||||||
pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
|
pkg.getPart(PackagingURIHelper.createPartName("/xl/_rels/workbook.xml.rels"));
|
||||||
assertNotNull(wbRelPart);
|
assertNotNull(wbRelPart);
|
||||||
assertTrue(wbRelPart.isRelationshipPart());
|
assertTrue(wbRelPart.isRelationshipPart());
|
||||||
assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
|
assertEquals(ContentTypes.RELATIONSHIPS_PART, wbRelPart.getContentType());
|
||||||
|
|
||||||
PackagePart wbPart =
|
PackagePart wbPart =
|
||||||
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
|
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
|
||||||
// Links to the three sheets, shared strings and styles
|
// Links to the three sheets, shared strings and styles
|
||||||
assertTrue(wbPart.hasRelationships());
|
assertTrue(wbPart.hasRelationships());
|
||||||
assertEquals(5, wbPart.getRelationships().size());
|
assertEquals(5, wbPart.getRelationships().size());
|
||||||
|
|
||||||
// Load back the XSSFWorkbook
|
// Load back the XSSFWorkbook
|
||||||
workbook = new XSSFWorkbook(pkg);
|
workbook = new XSSFWorkbook(pkg);
|
||||||
assertEquals(3, workbook.getNumberOfSheets());
|
assertEquals(3, workbook.getNumberOfSheets());
|
||||||
assertNotNull(workbook.getSheetAt(0));
|
assertNotNull(workbook.getSheetAt(0));
|
||||||
assertNotNull(workbook.getSheetAt(1));
|
assertNotNull(workbook.getSheetAt(1));
|
||||||
assertNotNull(workbook.getSheetAt(2));
|
assertNotNull(workbook.getSheetAt(2));
|
||||||
|
|
||||||
assertNotNull(workbook.getSharedStringSource());
|
assertNotNull(workbook.getSharedStringSource());
|
||||||
assertNotNull(workbook.getStylesSource());
|
assertNotNull(workbook.getStylesSource());
|
||||||
|
|
||||||
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
|
assertEquals(0, workbook.getSheetAt(0).getFirstRowNum());
|
||||||
assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
|
assertEquals(1, workbook.getSheetAt(0).getLastRowNum());
|
||||||
assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
|
assertEquals(0, workbook.getSheetAt(1).getFirstRowNum());
|
||||||
assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
|
assertEquals(0, workbook.getSheetAt(1).getLastRowNum());
|
||||||
assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
|
assertEquals(-1, workbook.getSheetAt(2).getFirstRowNum());
|
||||||
assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
|
assertEquals(-1, workbook.getSheetAt(2).getLastRowNum());
|
||||||
|
|
||||||
sheet1 = workbook.getSheetAt(0);
|
sheet1 = workbook.getSheetAt(0);
|
||||||
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
|
assertEquals(1.2, sheet1.getRow(0).getCell(0).getNumericCellValue(), 0.0001);
|
||||||
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
|
assertEquals("hello world", sheet1.getRow(1).getCell(0).getRichStringCellValue().getString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testExisting() throws Exception {
|
public void testExisting() throws Exception {
|
||||||
File xml = new File(
|
|
||||||
System.getProperty("HSSF.testdata.path") +
|
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
|
||||||
File.separator + "Formatting.xlsx"
|
|
||||||
);
|
|
||||||
assertTrue(xml.exists());
|
|
||||||
|
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
|
|
||||||
assertNotNull(workbook.getSharedStringSource());
|
assertNotNull(workbook.getSharedStringSource());
|
||||||
assertNotNull(workbook.getStylesSource());
|
assertNotNull(workbook.getStylesSource());
|
||||||
|
|
||||||
// And check a few low level bits too
|
// And check a few low level bits too
|
||||||
Package pkg = Package.open(xml.toString());
|
Package pkg = Package.open(HSSFTestDataSamples.openSampleFileStream("Formatting.xlsx"));
|
||||||
PackagePart wbPart =
|
PackagePart wbPart =
|
||||||
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
|
pkg.getPart(PackagingURIHelper.createPartName("/xl/workbook.xml"));
|
||||||
|
|
||||||
// Links to the three sheets, shared, styles and themes
|
// Links to the three sheets, shared, styles and themes
|
||||||
assertTrue(wbPart.hasRelationships());
|
assertTrue(wbPart.hasRelationships());
|
||||||
assertEquals(6, wbPart.getRelationships().size());
|
assertEquals(6, wbPart.getRelationships().size());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFindFont(){
|
public void testFindFont(){
|
||||||
//get default font and check against default value
|
//get default font and check against default value
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
|
Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
|
||||||
assertNotNull(fontFind);
|
assertNotNull(fontFind);
|
||||||
|
|
||||||
//get default font, then change 2 values and check against different values (height changes)
|
//get default font, then change 2 values and check against different values (height changes)
|
||||||
Font font=workbook.createFont();
|
Font font=workbook.createFont();
|
||||||
((XSSFFont)font).setBold(true);
|
((XSSFFont)font).setBold(true);
|
||||||
font.setUnderline(Font.U_DOUBLE);
|
font.setUnderline(Font.U_DOUBLE);
|
||||||
StylesSource styleSource=new StylesTable();
|
StylesSource styleSource=new StylesTable();
|
||||||
long index=styleSource.putFont(font);
|
long index=styleSource.putFont(font);
|
||||||
System.out.println("index="+index);
|
System.out.println("index="+index);
|
||||||
workbook.setStylesSource(styleSource);
|
workbook.setStylesSource(styleSource);
|
||||||
fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
|
fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
|
||||||
assertNull(fontFind);
|
assertNull(fontFind);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetCellStyleAt(){
|
public void testGetCellStyleAt(){
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
short i = 0;
|
short i = 0;
|
||||||
//get default style
|
//get default style
|
||||||
CellStyle cellStyleAt = workbook.getCellStyleAt(i);
|
CellStyle cellStyleAt = workbook.getCellStyleAt(i);
|
||||||
assertNotNull(cellStyleAt);
|
assertNotNull(cellStyleAt);
|
||||||
|
|
||||||
//get custom style
|
//get custom style
|
||||||
StylesSource styleSource = workbook.getStylesSource();
|
StylesSource styleSource = workbook.getStylesSource();
|
||||||
CellStyle customStyle = new XSSFCellStyle(styleSource);
|
CellStyle customStyle = new XSSFCellStyle(styleSource);
|
||||||
Font font = new XSSFFont();
|
Font font = new XSSFFont();
|
||||||
font.setFontName("Verdana");
|
font.setFontName("Verdana");
|
||||||
customStyle.setFont(font);
|
customStyle.setFont(font);
|
||||||
Long x = styleSource.putStyle(customStyle);
|
Long x = styleSource.putStyle(customStyle);
|
||||||
cellStyleAt = workbook.getCellStyleAt(x.shortValue());
|
cellStyleAt = workbook.getCellStyleAt(x.shortValue());
|
||||||
assertNotNull(cellStyleAt);
|
assertNotNull(cellStyleAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetFontAt(){
|
public void testGetFontAt(){
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
StylesSource styleSource = workbook.getStylesSource();
|
StylesSource styleSource = workbook.getStylesSource();
|
||||||
short i = 0;
|
short i = 0;
|
||||||
//get default font
|
//get default font
|
||||||
Font fontAt = workbook.getFontAt(i);
|
Font fontAt = workbook.getFontAt(i);
|
||||||
assertNotNull(fontAt);
|
assertNotNull(fontAt);
|
||||||
|
|
||||||
//get customized font
|
//get customized font
|
||||||
Font customFont = new XSSFFont();
|
Font customFont = new XSSFFont();
|
||||||
customFont.setItalic(true);
|
customFont.setItalic(true);
|
||||||
Long x = styleSource.putFont(customFont);
|
Long x = styleSource.putFont(customFont);
|
||||||
fontAt = workbook.getFontAt(x.shortValue());
|
fontAt = workbook.getFontAt(x.shortValue());
|
||||||
assertNotNull(fontAt);
|
assertNotNull(fontAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetNumberOfFonts(){
|
public void testGetNumberOfFonts(){
|
||||||
XSSFWorkbook wb = new XSSFWorkbook();
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
|
||||||
XSSFFont f1=wb.createFont();
|
XSSFFont f1=wb.createFont();
|
||||||
f1.setBold(true);
|
f1.setBold(true);
|
||||||
wb.createCellStyle().setFont(f1);
|
wb.createCellStyle().setFont(f1);
|
||||||
|
|
||||||
XSSFFont f2=wb.createFont();
|
XSSFFont f2=wb.createFont();
|
||||||
f2.setUnderline(Font.U_DOUBLE);
|
f2.setUnderline(Font.U_DOUBLE);
|
||||||
wb.createCellStyle().setFont(f2);
|
wb.createCellStyle().setFont(f2);
|
||||||
|
|
||||||
XSSFFont f3=wb.createFont();
|
XSSFFont f3=wb.createFont();
|
||||||
f3.setFontHeightInPoints((short)23);
|
f3.setFontHeightInPoints((short)23);
|
||||||
wb.createCellStyle().setFont(f3);
|
wb.createCellStyle().setFont(f3);
|
||||||
|
|
||||||
assertEquals(4,wb.getNumberOfFonts());
|
assertEquals(4,wb.getNumberOfFonts());
|
||||||
assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
|
assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetNumCellStyles(){
|
public void testGetNumCellStyles(){
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
short i = workbook.getNumCellStyles();
|
short i = workbook.getNumCellStyles();
|
||||||
//get default cellStyles
|
//get default cellStyles
|
||||||
assertEquals(1, i);
|
assertEquals(1, i);
|
||||||
//get wrong value
|
//get wrong value
|
||||||
assertNotSame(2, i);
|
assertNotSame(2, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetDisplayedTab(){
|
public void testGetDisplayedTab(){
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
short i = (short) workbook.getFirstVisibleTab();
|
short i = (short) workbook.getFirstVisibleTab();
|
||||||
//get default diplayedTab
|
//get default diplayedTab
|
||||||
assertEquals(0, i);
|
assertEquals(0, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSetDisplayedTab(){
|
public void testSetDisplayedTab(){
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
workbook.setFirstVisibleTab(new Integer(1).shortValue());
|
workbook.setFirstVisibleTab(new Integer(1).shortValue());
|
||||||
short i = (short) workbook.getFirstVisibleTab();
|
short i = (short) workbook.getFirstVisibleTab();
|
||||||
//0 (defualt value) is not longer set
|
//0 (defualt value) is not longer set
|
||||||
assertNotSame(0, i);
|
assertNotSame(0, i);
|
||||||
//1 is the default tab
|
//1 is the default tab
|
||||||
assertEquals(1, i);
|
assertEquals(1, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testLoadSave() throws Exception {
|
public void testLoadSave() {
|
||||||
File xml = new File(
|
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
|
||||||
System.getProperty("HSSF.testdata.path") +
|
|
||||||
File.separator + "Formatting.xlsx"
|
|
||||||
);
|
|
||||||
assertTrue(xml.exists());
|
|
||||||
|
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
|
|
||||||
assertEquals(3, workbook.getNumberOfSheets());
|
assertEquals(3, workbook.getNumberOfSheets());
|
||||||
assertEquals("dd/mm/yyyy", workbook.getSheetAt(0).getRow(1).getCell(0).getRichStringCellValue().getString());
|
assertEquals("dd/mm/yyyy", workbook.getSheetAt(0).getRow(1).getCell(0).getRichStringCellValue().getString());
|
||||||
assertNotNull(workbook.getSharedStringSource());
|
assertNotNull(workbook.getSharedStringSource());
|
||||||
assertNotNull(workbook.getStylesSource());
|
assertNotNull(workbook.getStylesSource());
|
||||||
|
|
||||||
// Write out, and check
|
// Write out, and check
|
||||||
File tmpFile = File.createTempFile("poi-tmp", ".xlsx");
|
|
||||||
workbook.write(new FileOutputStream(tmpFile));
|
|
||||||
|
|
||||||
// Load up again, check all still there
|
// Load up again, check all still there
|
||||||
XSSFWorkbook wb2 = new XSSFWorkbook(tmpFile.toString());
|
XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(workbook);
|
||||||
assertEquals(3, wb2.getNumberOfSheets());
|
assertEquals(3, wb2.getNumberOfSheets());
|
||||||
assertNotNull(wb2.getSheetAt(0));
|
assertNotNull(wb2.getSheetAt(0));
|
||||||
assertNotNull(wb2.getSheetAt(1));
|
assertNotNull(wb2.getSheetAt(1));
|
||||||
@ -367,16 +354,10 @@ public class TestXSSFWorkbook extends TestCase {
|
|||||||
assertEquals("yy/mm/dd", wb2.getSheetAt(0).getRow(4).getCell(0).getRichStringCellValue().getString());
|
assertEquals("yy/mm/dd", wb2.getSheetAt(0).getRow(4).getCell(0).getRichStringCellValue().getString());
|
||||||
assertNotNull(wb2.getSharedStringSource());
|
assertNotNull(wb2.getSharedStringSource());
|
||||||
assertNotNull(wb2.getStylesSource());
|
assertNotNull(wb2.getStylesSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testStyles() throws Exception {
|
public void testStyles() {
|
||||||
File xml = new File(
|
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
|
||||||
System.getProperty("HSSF.testdata.path") +
|
|
||||||
File.separator + "Formatting.xlsx"
|
|
||||||
);
|
|
||||||
assertTrue(xml.exists());
|
|
||||||
|
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
|
|
||||||
|
|
||||||
StylesSource ss = workbook.getStylesSource();
|
StylesSource ss = workbook.getStylesSource();
|
||||||
assertNotNull(ss);
|
assertNotNull(ss);
|
||||||
@ -403,12 +384,9 @@ public class TestXSSFWorkbook extends TestCase {
|
|||||||
|
|
||||||
|
|
||||||
// Save, load back in again, and check
|
// Save, load back in again, and check
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
|
||||||
workbook.write(baos);
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
ss = workbook.getStylesSource();
|
||||||
workbook = new XSSFWorkbook(Package.open(bais));
|
|
||||||
|
|
||||||
ss = workbook.getStylesSource();
|
|
||||||
assertNotNull(ss);
|
assertNotNull(ss);
|
||||||
assertTrue(ss instanceof StylesTable);
|
assertTrue(ss instanceof StylesTable);
|
||||||
st = (StylesTable)ss;
|
st = (StylesTable)ss;
|
||||||
@ -417,75 +395,96 @@ public class TestXSSFWorkbook extends TestCase {
|
|||||||
assertEquals(2, st._getFontsSize());
|
assertEquals(2, st._getFontsSize());
|
||||||
assertEquals(2, st._getFillsSize());
|
assertEquals(2, st._getFillsSize());
|
||||||
assertEquals(1, st._getBordersSize());
|
assertEquals(1, st._getBordersSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNamedRanges() throws Exception {
|
public void testNamedRanges() {
|
||||||
// First up, a new file
|
// First up, a new file
|
||||||
XSSFWorkbook workbook = new XSSFWorkbook();
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
||||||
assertEquals(0, workbook.getNumberOfNames());
|
assertEquals(0, workbook.getNumberOfNames());
|
||||||
|
|
||||||
Name nameA = workbook.createName();
|
|
||||||
nameA.setReference("A2");
|
|
||||||
nameA.setNameName("ForA2");
|
|
||||||
|
|
||||||
XSSFName nameB = workbook.createName();
|
|
||||||
nameB.setReference("B3");
|
|
||||||
nameB.setNameName("ForB3");
|
|
||||||
nameB.setComment("B3 Comment");
|
|
||||||
|
|
||||||
// Save and re-load
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
workbook.write(baos);
|
|
||||||
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
|
||||||
workbook = new XSSFWorkbook(Package.open(bais));
|
|
||||||
|
|
||||||
assertEquals(2, workbook.getNumberOfNames());
|
|
||||||
assertEquals("A2", workbook.getNameAt(0).getReference());
|
|
||||||
assertEquals("ForA2", workbook.getNameAt(0).getNameName());
|
|
||||||
assertNull(workbook.getNameAt(0).getComment());
|
|
||||||
|
|
||||||
assertEquals("B3", workbook.getNameAt(1).getReference());
|
|
||||||
assertEquals("ForB3", workbook.getNameAt(1).getNameName());
|
|
||||||
assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
|
|
||||||
|
|
||||||
assertEquals("ForA2", workbook.getNameName(0));
|
|
||||||
assertEquals(1, workbook.getNameIndex("ForB3"));
|
|
||||||
assertEquals(-1, workbook.getNameIndex("ForB3!!"));
|
|
||||||
|
|
||||||
|
|
||||||
// Now, an existing file with named ranges
|
|
||||||
File xml = new File(
|
|
||||||
System.getProperty("HSSF.testdata.path") +
|
|
||||||
File.separator + "WithVariousData.xlsx"
|
|
||||||
);
|
|
||||||
assertTrue(xml.exists());
|
|
||||||
|
|
||||||
workbook = new XSSFWorkbook(xml.toString());
|
|
||||||
|
|
||||||
assertEquals(2, workbook.getNumberOfNames());
|
Name nameA = workbook.createName();
|
||||||
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
|
nameA.setReference("A2");
|
||||||
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
|
nameA.setNameName("ForA2");
|
||||||
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
|
|
||||||
|
|
||||||
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
|
|
||||||
assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
|
|
||||||
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
|
|
||||||
|
|
||||||
// Tweak, save, and re-check
|
|
||||||
workbook.getNameAt(1).setNameName("BStringsFun");
|
|
||||||
|
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
workbook.write(baos);
|
|
||||||
bais = new ByteArrayInputStream(baos.toByteArray());
|
|
||||||
workbook = new XSSFWorkbook(Package.open(bais));
|
|
||||||
|
|
||||||
assertEquals(2, workbook.getNumberOfNames());
|
XSSFName nameB = workbook.createName();
|
||||||
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
|
nameB.setReference("B3");
|
||||||
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
|
nameB.setNameName("ForB3");
|
||||||
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
|
nameB.setComment("B3 Comment");
|
||||||
|
|
||||||
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
|
// Save and re-load
|
||||||
assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
|
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
|
||||||
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
|
|
||||||
}
|
assertEquals(2, workbook.getNumberOfNames());
|
||||||
|
assertEquals("A2", workbook.getNameAt(0).getReference());
|
||||||
|
assertEquals("ForA2", workbook.getNameAt(0).getNameName());
|
||||||
|
assertNull(workbook.getNameAt(0).getComment());
|
||||||
|
|
||||||
|
assertEquals("B3", workbook.getNameAt(1).getReference());
|
||||||
|
assertEquals("ForB3", workbook.getNameAt(1).getNameName());
|
||||||
|
assertEquals("B3 Comment", workbook.getNameAt(1).getComment());
|
||||||
|
|
||||||
|
assertEquals("ForA2", workbook.getNameName(0));
|
||||||
|
assertEquals(1, workbook.getNameIndex("ForB3"));
|
||||||
|
assertEquals(-1, workbook.getNameIndex("ForB3!!"));
|
||||||
|
|
||||||
|
|
||||||
|
// Now, an existing file with named ranges
|
||||||
|
workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
|
||||||
|
|
||||||
|
assertEquals(2, workbook.getNumberOfNames());
|
||||||
|
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
|
||||||
|
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
|
||||||
|
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
|
||||||
|
|
||||||
|
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
|
||||||
|
assertEquals("AllBStrings", workbook.getNameAt(1).getNameName());
|
||||||
|
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
|
||||||
|
|
||||||
|
// Tweak, save, and re-check
|
||||||
|
workbook.getNameAt(1).setNameName("BStringsFun");
|
||||||
|
|
||||||
|
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
|
||||||
|
|
||||||
|
assertEquals(2, workbook.getNumberOfNames());
|
||||||
|
assertEquals("Sheet1!$A$2:$A$7", workbook.getNameAt(0).getReference());
|
||||||
|
assertEquals("AllANumbers", workbook.getNameAt(0).getNameName());
|
||||||
|
assertEquals("All the numbers in A", workbook.getNameAt(0).getComment());
|
||||||
|
|
||||||
|
assertEquals("Sheet1!$B$2:$B$7", workbook.getNameAt(1).getReference());
|
||||||
|
assertEquals("BStringsFun", workbook.getNameAt(1).getNameName());
|
||||||
|
assertEquals("All the strings in B", workbook.getNameAt(1).getComment());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDuplicateNames() {
|
||||||
|
|
||||||
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
wb.createSheet("Sheet1");
|
||||||
|
wb.createSheet();
|
||||||
|
wb.createSheet("name1");
|
||||||
|
try {
|
||||||
|
wb.createSheet("name1");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
wb.createSheet();
|
||||||
|
|
||||||
|
try {
|
||||||
|
wb.setSheetName(3, "name1");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
wb.setSheetName(3, "Sheet1");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
assertEquals("The workbook already contains a sheet of this name", e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
wb.setSheetName(3, "name2");
|
||||||
|
wb.setSheetName(3, "Sheet3");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user