Fix inconsistent indents
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1677369 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
602832bda6
commit
c76db74929
@ -62,73 +62,73 @@ 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.
|
||||||
*/
|
*/
|
||||||
public class StylesTable extends POIXMLDocumentPart {
|
public class StylesTable extends POIXMLDocumentPart {
|
||||||
private final Map<Integer, String> numberFormats = new HashMap<Integer,String>();
|
private final Map<Integer, String> numberFormats = new HashMap<Integer,String>();
|
||||||
private final boolean[] usedNumberFormats = new boolean[SpreadsheetVersion.EXCEL2007.getMaxCellStyles()];
|
private final boolean[] usedNumberFormats = new boolean[SpreadsheetVersion.EXCEL2007.getMaxCellStyles()];
|
||||||
private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
|
private final List<XSSFFont> fonts = new ArrayList<XSSFFont>();
|
||||||
private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
|
private final List<XSSFCellFill> fills = new ArrayList<XSSFCellFill>();
|
||||||
private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
|
private final List<XSSFCellBorder> borders = new ArrayList<XSSFCellBorder>();
|
||||||
private final List<CTXf> styleXfs = new ArrayList<CTXf>();
|
private final List<CTXf> styleXfs = new ArrayList<CTXf>();
|
||||||
private final List<CTXf> xfs = new ArrayList<CTXf>();
|
private final List<CTXf> xfs = new ArrayList<CTXf>();
|
||||||
|
|
||||||
private final List<CTDxf> dxfs = new ArrayList<CTDxf>();
|
private final List<CTDxf> dxfs = new ArrayList<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 int FIRST_CUSTOM_STYLE_ID = BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX + 1;
|
public static final int FIRST_CUSTOM_STYLE_ID = BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX + 1;
|
||||||
private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles();
|
private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles();
|
||||||
|
|
||||||
private StyleSheetDocument doc;
|
private StyleSheetDocument doc;
|
||||||
private ThemesTable theme;
|
private ThemesTable theme;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new, empty StylesTable
|
* Create a new, empty StylesTable
|
||||||
*/
|
*/
|
||||||
public StylesTable() {
|
public StylesTable() {
|
||||||
super();
|
super();
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
public StylesTable(PackagePart part, PackageRelationship rel) throws IOException {
|
public StylesTable(PackagePart part, PackageRelationship rel) throws IOException {
|
||||||
super(part, rel);
|
super(part, rel);
|
||||||
readFrom(part.getInputStream());
|
readFrom(part.getInputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
public ThemesTable getTheme() {
|
public ThemesTable getTheme() {
|
||||||
return theme;
|
return theme;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTheme(ThemesTable theme) {
|
public void setTheme(ThemesTable theme) {
|
||||||
this.theme = theme;
|
this.theme = theme;
|
||||||
|
|
||||||
// Pass the themes table along to things which need to
|
// Pass the themes table along to things which need to
|
||||||
// know about it, but have already been created by now
|
// know about it, but have already been created by now
|
||||||
for(XSSFFont font : fonts) {
|
for(XSSFFont font : fonts) {
|
||||||
font.setThemesTable(theme);
|
font.setThemesTable(theme);
|
||||||
}
|
}
|
||||||
for(XSSFCellBorder border : borders) {
|
for(XSSFCellBorder border : borders) {
|
||||||
border.setThemesTable(theme);
|
border.setThemesTable(theme);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
|
@SuppressWarnings("deprecation") //YK: getXYZArray() array accessors are deprecated in xmlbeans with JDK 1.5 support
|
||||||
protected void readFrom(InputStream is) throws IOException {
|
protected void readFrom(InputStream is) throws IOException {
|
||||||
try {
|
try {
|
||||||
doc = StyleSheetDocument.Factory.parse(is);
|
doc = StyleSheetDocument.Factory.parse(is);
|
||||||
|
|
||||||
CTStylesheet styleSheet = doc.getStyleSheet();
|
CTStylesheet styleSheet = doc.getStyleSheet();
|
||||||
|
|
||||||
// Grab all the different bits we care about
|
// Grab all the different bits we care about
|
||||||
CTNumFmts ctfmts = styleSheet.getNumFmts();
|
CTNumFmts ctfmts = styleSheet.getNumFmts();
|
||||||
if( ctfmts != null){
|
if( ctfmts != null){
|
||||||
for (CTNumFmt nfmt : ctfmts.getNumFmtArray()) {
|
for (CTNumFmt nfmt : ctfmts.getNumFmtArray()) {
|
||||||
int formatId = (int)nfmt.getNumFmtId();
|
int formatId = (int)nfmt.getNumFmtId();
|
||||||
@ -139,14 +139,14 @@ public class StylesTable extends POIXMLDocumentPart {
|
|||||||
|
|
||||||
CTFonts ctfonts = styleSheet.getFonts();
|
CTFonts ctfonts = styleSheet.getFonts();
|
||||||
if(ctfonts != null){
|
if(ctfonts != null){
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
for (CTFont font : ctfonts.getFontArray()) {
|
for (CTFont font : ctfonts.getFontArray()) {
|
||||||
// Create the font and save it. Themes Table supplied later
|
// Create the font and save it. Themes Table supplied later
|
||||||
XSSFFont f = new XSSFFont(font, idx);
|
XSSFFont f = new XSSFFont(font, idx);
|
||||||
fonts.add(f);
|
fonts.add(f);
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CTFills ctfills = styleSheet.getFills();
|
CTFills ctfills = styleSheet.getFills();
|
||||||
if(ctfills != null){
|
if(ctfills != null){
|
||||||
for (CTFill fill : ctfills.getFillArray()) {
|
for (CTFill fill : ctfills.getFillArray()) {
|
||||||
@ -168,408 +168,407 @@ public class StylesTable extends POIXMLDocumentPart {
|
|||||||
if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
|
if(cellStyleXfs != null) styleXfs.addAll(Arrays.asList(cellStyleXfs.getXfArray()));
|
||||||
|
|
||||||
CTDxfs styleDxfs = styleSheet.getDxfs();
|
CTDxfs styleDxfs = styleSheet.getDxfs();
|
||||||
if(styleDxfs != null) dxfs.addAll(Arrays.asList(styleDxfs.getDxfArray()));
|
if(styleDxfs != null) dxfs.addAll(Arrays.asList(styleDxfs.getDxfArray()));
|
||||||
|
|
||||||
} 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(int idx) {
|
public String getNumberFormatAt(int idx) {
|
||||||
return numberFormats.get(idx);
|
return numberFormats.get(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int putNumberFormat(String fmt) {
|
public int putNumberFormat(String fmt) {
|
||||||
if (numberFormats.containsValue(fmt)) {
|
if (numberFormats.containsValue(fmt)) {
|
||||||
// Find the key, and return that
|
// Find the key, and return that
|
||||||
for (Entry<Integer,String> numFmt : numberFormats.entrySet()) {
|
for (Entry<Integer,String> numFmt : numberFormats.entrySet()) {
|
||||||
if(numFmt.getValue().equals(fmt)) {
|
if(numFmt.getValue().equals(fmt)) {
|
||||||
return numFmt.getKey();
|
return numFmt.getKey();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
||||||
for (int i=FIRST_CUSTOM_STYLE_ID; i<usedNumberFormats.length; i++) {
|
for (int i=FIRST_CUSTOM_STYLE_ID; i<usedNumberFormats.length; i++) {
|
||||||
if (!usedNumberFormats[i]) {
|
if (!usedNumberFormats[i]) {
|
||||||
usedNumberFormats[i] = true;
|
usedNumberFormats[i] = true;
|
||||||
numberFormats.put(i, fmt);
|
numberFormats.put(i, fmt);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new IllegalStateException("The maximum number of Data Formats was exceeded. " +
|
throw new IllegalStateException("The maximum number of Data Formats was exceeded. " +
|
||||||
"You can define up to " + usedNumberFormats.length + " formats in a .xlsx Workbook");
|
"You can define up to " + usedNumberFormats.length + " formats in a .xlsx Workbook");
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFFont getFontAt(int idx) {
|
public XSSFFont getFontAt(int idx) {
|
||||||
return fonts.get(idx);
|
return fonts.get(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Records the given font in the font table.
|
* Records the given font in the font table.
|
||||||
* Will re-use an existing font index if this
|
* Will re-use an existing font index if this
|
||||||
* font matches another, EXCEPT if forced
|
* font matches another, EXCEPT if forced
|
||||||
* registration is requested.
|
* registration is requested.
|
||||||
* This allows people to create several fonts
|
* This allows people to create several fonts
|
||||||
* then customise them later.
|
* then customise them later.
|
||||||
* Note - End Users probably want to call
|
* Note - End Users probably want to call
|
||||||
* {@link XSSFFont#registerTo(StylesTable)}
|
* {@link XSSFFont#registerTo(StylesTable)}
|
||||||
*/
|
*/
|
||||||
public int putFont(XSSFFont font, boolean forceRegistration) {
|
public int putFont(XSSFFont font, boolean forceRegistration) {
|
||||||
int idx = -1;
|
int idx = -1;
|
||||||
if(!forceRegistration) {
|
if(!forceRegistration) {
|
||||||
idx = fonts.indexOf(font);
|
idx = fonts.indexOf(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (idx != -1) {
|
if (idx != -1) {
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
idx = fonts.size();
|
|
||||||
fonts.add(font);
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
public int putFont(XSSFFont font) {
|
|
||||||
return putFont(font, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public XSSFCellStyle getStyleAt(int idx) {
|
idx = fonts.size();
|
||||||
int styleXfId = 0;
|
fonts.add(font);
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
public int putFont(XSSFFont font) {
|
||||||
|
return putFont(font, false);
|
||||||
|
}
|
||||||
|
|
||||||
// 0 is the empty default
|
public XSSFCellStyle getStyleAt(int idx) {
|
||||||
if(xfs.get(idx).getXfId() > 0) {
|
int styleXfId = 0;
|
||||||
styleXfId = (int) xfs.get(idx).getXfId();
|
|
||||||
}
|
|
||||||
|
|
||||||
return new XSSFCellStyle(idx, styleXfId, this, theme);
|
// 0 is the empty default
|
||||||
}
|
if(xfs.get(idx).getXfId() > 0) {
|
||||||
public int putStyle(XSSFCellStyle style) {
|
styleXfId = (int) xfs.get(idx).getXfId();
|
||||||
CTXf mainXF = style.getCoreXf();
|
}
|
||||||
|
|
||||||
if(! xfs.contains(mainXF)) {
|
return new XSSFCellStyle(idx, styleXfId, this, theme);
|
||||||
xfs.add(mainXF);
|
}
|
||||||
}
|
public int putStyle(XSSFCellStyle style) {
|
||||||
return xfs.indexOf(mainXF);
|
CTXf mainXF = style.getCoreXf();
|
||||||
}
|
|
||||||
|
|
||||||
public XSSFCellBorder getBorderAt(int idx) {
|
if(! xfs.contains(mainXF)) {
|
||||||
return borders.get(idx);
|
xfs.add(mainXF);
|
||||||
}
|
}
|
||||||
|
return xfs.indexOf(mainXF);
|
||||||
|
}
|
||||||
|
|
||||||
public int putBorder(XSSFCellBorder border) {
|
public XSSFCellBorder getBorderAt(int idx) {
|
||||||
int idx = borders.indexOf(border);
|
return borders.get(idx);
|
||||||
if (idx != -1) {
|
}
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
borders.add(border);
|
|
||||||
border.setThemesTable(theme);
|
|
||||||
return borders.size() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public XSSFCellFill getFillAt(int idx) {
|
public int putBorder(XSSFCellBorder border) {
|
||||||
return fills.get(idx);
|
int idx = borders.indexOf(border);
|
||||||
}
|
if (idx != -1) {
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
borders.add(border);
|
||||||
|
border.setThemesTable(theme);
|
||||||
|
return borders.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
public List<XSSFCellBorder> getBorders(){
|
public XSSFCellFill getFillAt(int idx) {
|
||||||
return borders;
|
return fills.get(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<XSSFCellFill> getFills(){
|
public List<XSSFCellBorder> getBorders(){
|
||||||
return fills;
|
return borders;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<XSSFFont> getFonts(){
|
public List<XSSFCellFill> getFills(){
|
||||||
return fonts;
|
return fills;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, String> getNumberFormats(){
|
public List<XSSFFont> getFonts(){
|
||||||
return numberFormats;
|
return fonts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int putFill(XSSFCellFill fill) {
|
public Map<Integer, String> getNumberFormats(){
|
||||||
int idx = fills.indexOf(fill);
|
return numberFormats;
|
||||||
if (idx != -1) {
|
}
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
fills.add(fill);
|
|
||||||
return fills.size() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CTXf getCellXfAt(int idx) {
|
public int putFill(XSSFCellFill fill) {
|
||||||
return xfs.get(idx);
|
int idx = fills.indexOf(fill);
|
||||||
}
|
if (idx != -1) {
|
||||||
public int putCellXf(CTXf cellXf) {
|
return idx;
|
||||||
xfs.add(cellXf);
|
}
|
||||||
return xfs.size();
|
fills.add(fill);
|
||||||
}
|
return fills.size() - 1;
|
||||||
public void replaceCellXfAt(int idx, CTXf cellXf) {
|
}
|
||||||
xfs.set(idx, cellXf);
|
|
||||||
}
|
|
||||||
|
|
||||||
public CTXf getCellStyleXfAt(int idx) {
|
public CTXf getCellXfAt(int idx) {
|
||||||
return idx < styleXfs.size() ? styleXfs.get(idx) : null;
|
return xfs.get(idx);
|
||||||
}
|
}
|
||||||
public int putCellStyleXf(CTXf cellStyleXf) {
|
public int putCellXf(CTXf cellXf) {
|
||||||
styleXfs.add(cellStyleXf);
|
xfs.add(cellXf);
|
||||||
return styleXfs.size();
|
return xfs.size();
|
||||||
}
|
}
|
||||||
public void replaceCellStyleXfAt(int idx, CTXf cellStyleXf) {
|
public void replaceCellXfAt(int idx, CTXf cellXf) {
|
||||||
styleXfs.set(idx, cellStyleXf);
|
xfs.set(idx, cellXf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public CTXf getCellStyleXfAt(int idx) {
|
||||||
* get the size of cell styles
|
return idx < styleXfs.size() ? styleXfs.get(idx) : null;
|
||||||
*/
|
}
|
||||||
public int getNumCellStyles(){
|
public int putCellStyleXf(CTXf cellStyleXf) {
|
||||||
|
styleXfs.add(cellStyleXf);
|
||||||
|
return styleXfs.size();
|
||||||
|
}
|
||||||
|
public void replaceCellStyleXfAt(int idx, CTXf cellStyleXf) {
|
||||||
|
styleXfs.set(idx, cellStyleXf);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the size of cell styles
|
||||||
|
*/
|
||||||
|
public int getNumCellStyles(){
|
||||||
// Each cell style has a unique xfs entry
|
// Each cell style has a unique xfs entry
|
||||||
// Several might share the same styleXfs entry
|
// Several might share the same styleXfs entry
|
||||||
return xfs.size();
|
return xfs.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 _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 getCTStylesheet() {
|
public CTStylesheet getCTStylesheet() {
|
||||||
return doc.getStyleSheet();
|
return doc.getStyleSheet();
|
||||||
}
|
}
|
||||||
public int _getDXfsSize() {
|
public int _getDXfsSize() {
|
||||||
return dxfs.size();
|
return dxfs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write this table out as XML.
|
* Write this table out as XML.
|
||||||
*
|
*
|
||||||
* @param out The stream to write to.
|
* @param out The stream to write to.
|
||||||
* @throws IOException if an error occurs while writing.
|
* @throws IOException if an error occurs while writing.
|
||||||
*/
|
*/
|
||||||
public void writeTo(OutputStream out) throws IOException {
|
public void writeTo(OutputStream out) throws IOException {
|
||||||
XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
|
XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
|
||||||
|
|
||||||
// Work on the current one
|
// Work on the current one
|
||||||
// Need to do this, as we don't handle
|
// Need to do this, as we don't handle
|
||||||
// all the possible entries yet
|
// all the possible entries yet
|
||||||
CTStylesheet styleSheet = doc.getStyleSheet();
|
CTStylesheet styleSheet = doc.getStyleSheet();
|
||||||
|
|
||||||
// Formats
|
// Formats
|
||||||
CTNumFmts formats = CTNumFmts.Factory.newInstance();
|
CTNumFmts formats = CTNumFmts.Factory.newInstance();
|
||||||
formats.setCount(numberFormats.size());
|
formats.setCount(numberFormats.size());
|
||||||
for (Entry<Integer, String> fmt : numberFormats.entrySet()) {
|
for (Entry<Integer, String> fmt : numberFormats.entrySet()) {
|
||||||
CTNumFmt ctFmt = formats.addNewNumFmt();
|
CTNumFmt ctFmt = formats.addNewNumFmt();
|
||||||
ctFmt.setNumFmtId(fmt.getKey());
|
ctFmt.setNumFmtId(fmt.getKey());
|
||||||
ctFmt.setFormatCode(fmt.getValue());
|
ctFmt.setFormatCode(fmt.getValue());
|
||||||
}
|
}
|
||||||
styleSheet.setNumFmts(formats);
|
styleSheet.setNumFmts(formats);
|
||||||
|
|
||||||
int idx;
|
int idx;
|
||||||
// Fonts
|
// Fonts
|
||||||
CTFonts ctFonts = styleSheet.getFonts();
|
CTFonts ctFonts = styleSheet.getFonts();
|
||||||
if (ctFonts == null) {
|
if (ctFonts == null) {
|
||||||
ctFonts = CTFonts.Factory.newInstance();
|
ctFonts = CTFonts.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctFonts.setCount(fonts.size());
|
ctFonts.setCount(fonts.size());
|
||||||
CTFont[] ctfnt = new CTFont[fonts.size()];
|
CTFont[] ctfnt = new CTFont[fonts.size()];
|
||||||
idx = 0;
|
idx = 0;
|
||||||
for(XSSFFont f : fonts) ctfnt[idx++] = f.getCTFont();
|
for(XSSFFont f : fonts) ctfnt[idx++] = f.getCTFont();
|
||||||
ctFonts.setFontArray(ctfnt);
|
ctFonts.setFontArray(ctfnt);
|
||||||
styleSheet.setFonts(ctFonts);
|
styleSheet.setFonts(ctFonts);
|
||||||
|
|
||||||
// Fills
|
// Fills
|
||||||
CTFills ctFills = styleSheet.getFills();
|
CTFills ctFills = styleSheet.getFills();
|
||||||
if (ctFills == null) {
|
if (ctFills == null) {
|
||||||
ctFills = CTFills.Factory.newInstance();
|
ctFills = CTFills.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctFills.setCount(fills.size());
|
ctFills.setCount(fills.size());
|
||||||
CTFill[] ctf = new CTFill[fills.size()];
|
CTFill[] ctf = new CTFill[fills.size()];
|
||||||
idx = 0;
|
idx = 0;
|
||||||
for(XSSFCellFill f : fills) ctf[idx++] = f.getCTFill();
|
for(XSSFCellFill f : fills) ctf[idx++] = f.getCTFill();
|
||||||
ctFills.setFillArray(ctf);
|
ctFills.setFillArray(ctf);
|
||||||
styleSheet.setFills(ctFills);
|
styleSheet.setFills(ctFills);
|
||||||
|
|
||||||
// Borders
|
// Borders
|
||||||
CTBorders ctBorders = styleSheet.getBorders();
|
CTBorders ctBorders = styleSheet.getBorders();
|
||||||
if (ctBorders == null) {
|
if (ctBorders == null) {
|
||||||
ctBorders = CTBorders.Factory.newInstance();
|
ctBorders = CTBorders.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctBorders.setCount(borders.size());
|
ctBorders.setCount(borders.size());
|
||||||
CTBorder[] ctb = new CTBorder[borders.size()];
|
CTBorder[] ctb = new CTBorder[borders.size()];
|
||||||
idx = 0;
|
idx = 0;
|
||||||
for(XSSFCellBorder b : borders) ctb[idx++] = b.getCTBorder();
|
for(XSSFCellBorder b : borders) ctb[idx++] = b.getCTBorder();
|
||||||
ctBorders.setBorderArray(ctb);
|
ctBorders.setBorderArray(ctb);
|
||||||
styleSheet.setBorders(ctBorders);
|
styleSheet.setBorders(ctBorders);
|
||||||
|
|
||||||
// Xfs
|
// Xfs
|
||||||
if(xfs.size() > 0) {
|
if(xfs.size() > 0) {
|
||||||
CTCellXfs ctXfs = styleSheet.getCellXfs();
|
CTCellXfs ctXfs = styleSheet.getCellXfs();
|
||||||
if (ctXfs == null) {
|
if (ctXfs == null) {
|
||||||
ctXfs = CTCellXfs.Factory.newInstance();
|
ctXfs = CTCellXfs.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctXfs.setCount(xfs.size());
|
ctXfs.setCount(xfs.size());
|
||||||
ctXfs.setXfArray(
|
ctXfs.setXfArray(
|
||||||
xfs.toArray(new CTXf[xfs.size()])
|
xfs.toArray(new CTXf[xfs.size()])
|
||||||
);
|
);
|
||||||
styleSheet.setCellXfs(ctXfs);
|
styleSheet.setCellXfs(ctXfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Style xfs
|
// Style xfs
|
||||||
if(styleXfs.size() > 0) {
|
if(styleXfs.size() > 0) {
|
||||||
CTCellStyleXfs ctSXfs = styleSheet.getCellStyleXfs();
|
CTCellStyleXfs ctSXfs = styleSheet.getCellStyleXfs();
|
||||||
if (ctSXfs == null) {
|
if (ctSXfs == null) {
|
||||||
ctSXfs = CTCellStyleXfs.Factory.newInstance();
|
ctSXfs = CTCellStyleXfs.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctSXfs.setCount(styleXfs.size());
|
ctSXfs.setCount(styleXfs.size());
|
||||||
ctSXfs.setXfArray(
|
ctSXfs.setXfArray(
|
||||||
styleXfs.toArray(new CTXf[styleXfs.size()])
|
styleXfs.toArray(new CTXf[styleXfs.size()])
|
||||||
);
|
);
|
||||||
styleSheet.setCellStyleXfs(ctSXfs);
|
styleSheet.setCellStyleXfs(ctSXfs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Style dxfs
|
// Style dxfs
|
||||||
if(dxfs.size() > 0) {
|
if(dxfs.size() > 0) {
|
||||||
CTDxfs ctDxfs = styleSheet.getDxfs();
|
CTDxfs ctDxfs = styleSheet.getDxfs();
|
||||||
if (ctDxfs == null) {
|
if (ctDxfs == null) {
|
||||||
ctDxfs = CTDxfs.Factory.newInstance();
|
ctDxfs = CTDxfs.Factory.newInstance();
|
||||||
}
|
}
|
||||||
ctDxfs.setCount(dxfs.size());
|
ctDxfs.setCount(dxfs.size());
|
||||||
ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()])
|
ctDxfs.setDxfArray(dxfs.toArray(new CTDxf[dxfs.size()]));
|
||||||
);
|
styleSheet.setDxfs(ctDxfs);
|
||||||
styleSheet.setDxfs(ctDxfs);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Save
|
// Save
|
||||||
doc.save(out, options);
|
doc.save(out, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void commit() throws IOException {
|
protected void commit() throws IOException {
|
||||||
PackagePart part = getPackagePart();
|
PackagePart part = getPackagePart();
|
||||||
OutputStream out = part.getOutputStream();
|
OutputStream out = part.getOutputStream();
|
||||||
writeTo(out);
|
writeTo(out);
|
||||||
out.close();
|
out.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
//CTFont ctFont = createDefaultFont();
|
//CTFont ctFont = createDefaultFont();
|
||||||
XSSFFont xssfFont = createDefaultFont();
|
XSSFFont xssfFont = createDefaultFont();
|
||||||
fonts.add(xssfFont);
|
fonts.add(xssfFont);
|
||||||
|
|
||||||
CTFill[] ctFill = createDefaultFills();
|
CTFill[] ctFill = createDefaultFills();
|
||||||
fills.add(new XSSFCellFill(ctFill[0]));
|
fills.add(new XSSFCellFill(ctFill[0]));
|
||||||
fills.add(new XSSFCellFill(ctFill[1]));
|
fills.add(new XSSFCellFill(ctFill[1]));
|
||||||
|
|
||||||
CTBorder ctBorder = createDefaultBorder();
|
CTBorder ctBorder = createDefaultBorder();
|
||||||
borders.add(new XSSFCellBorder(ctBorder));
|
borders.add(new XSSFCellBorder(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 static CTXf createDefaultXf() {
|
private static 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 static CTBorder createDefaultBorder() {
|
private static 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 static CTFill[] createDefaultFills() {
|
private static 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 static XSSFFont createDefaultFont() {
|
private static XSSFFont createDefaultFont() {
|
||||||
CTFont ctFont = CTFont.Factory.newInstance();
|
CTFont ctFont = CTFont.Factory.newInstance();
|
||||||
XSSFFont xssfFont=new XSSFFont(ctFont, 0);
|
XSSFFont xssfFont=new XSSFFont(ctFont, 0);
|
||||||
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
|
xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE);
|
||||||
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
|
xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme
|
||||||
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
|
xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME);
|
||||||
xssfFont.setFamily(FontFamily.SWISS);
|
xssfFont.setFamily(FontFamily.SWISS);
|
||||||
xssfFont.setScheme(FontScheme.MINOR);
|
xssfFont.setScheme(FontScheme.MINOR);
|
||||||
return xssfFont;
|
return xssfFont;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CTDxf getDxfAt(int idx) {
|
public CTDxf getDxfAt(int idx) {
|
||||||
return dxfs.get(idx);
|
return dxfs.get(idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int putDxf(CTDxf dxf) {
|
public int putDxf(CTDxf dxf) {
|
||||||
this.dxfs.add(dxf);
|
this.dxfs.add(dxf);
|
||||||
return this.dxfs.size();
|
return this.dxfs.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public XSSFCellStyle createCellStyle() {
|
public XSSFCellStyle createCellStyle() {
|
||||||
int xfSize = styleXfs.size();
|
int xfSize = styleXfs.size();
|
||||||
if (xfSize > MAXIMUM_STYLE_ID)
|
if (xfSize > MAXIMUM_STYLE_ID)
|
||||||
throw new IllegalStateException("The maximum number of Cell Styles was exceeded. " +
|
throw new IllegalStateException("The maximum number of Cell Styles was exceeded. " +
|
||||||
"You can define up to " + MAXIMUM_STYLE_ID + " style in a .xlsx Workbook");
|
"You can define up to " + MAXIMUM_STYLE_ID + " style in a .xlsx Workbook");
|
||||||
|
|
||||||
CTXf xf = CTXf.Factory.newInstance();
|
|
||||||
xf.setNumFmtId(0);
|
|
||||||
xf.setFontId(0);
|
|
||||||
xf.setFillId(0);
|
|
||||||
xf.setBorderId(0);
|
|
||||||
xf.setXfId(0);
|
|
||||||
int indexXf = putCellXf(xf);
|
|
||||||
return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
CTXf xf = CTXf.Factory.newInstance();
|
||||||
* Finds a font that matches the one with the supplied attributes
|
xf.setNumFmtId(0);
|
||||||
*/
|
xf.setFontId(0);
|
||||||
public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
|
xf.setFillId(0);
|
||||||
for (XSSFFont font : fonts) {
|
xf.setBorderId(0);
|
||||||
if ( (font.getBoldweight() == boldWeight)
|
xf.setXfId(0);
|
||||||
&& font.getColor() == color
|
int indexXf = putCellXf(xf);
|
||||||
&& font.getFontHeight() == fontHeight
|
return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme);
|
||||||
&& font.getFontName().equals(name)
|
}
|
||||||
&& font.getItalic() == italic
|
|
||||||
&& font.getStrikeout() == strikeout
|
/**
|
||||||
&& font.getTypeOffset() == typeOffset
|
* Finds a font that matches the one with the supplied attributes
|
||||||
&& font.getUnderline() == underline)
|
*/
|
||||||
{
|
public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
|
||||||
return font;
|
for (XSSFFont font : fonts) {
|
||||||
}
|
if ( (font.getBoldweight() == boldWeight)
|
||||||
}
|
&& font.getColor() == color
|
||||||
return null;
|
&& font.getFontHeight() == fontHeight
|
||||||
}
|
&& font.getFontName().equals(name)
|
||||||
|
&& font.getItalic() == italic
|
||||||
|
&& font.getStrikeout() == strikeout
|
||||||
|
&& font.getTypeOffset() == typeOffset
|
||||||
|
&& font.getUnderline() == underline)
|
||||||
|
{
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user