refactored cell align accessors in xssf and added samples based on the quick guide

git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@695411 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-09-15 10:39:37 +00:00
parent 0db7da83b7
commit b832a6411d
12 changed files with 1191 additions and 700 deletions

View File

@ -0,0 +1,71 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import java.io.FileOutputStream;
/**
* Demonstrates various alignment options
*/
public class AligningCells {
public static void main(String[] args)
throws Exception
{
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);
createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP);
createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("aligning.xlsx");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param halign the horizontal alignment for the cell.
*/
private static void createCell(Workbook wb, Row row, short column, short halign, short valign)
{
Cell cell = row.createCell(column);
cell.setCellValue(new XSSFRichTextString("Align It"));
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
}
}

View File

@ -1,3 +1,19 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples; package org.apache.poi.xssf.usermodel.examples;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -48,7 +64,7 @@ public class CreateNewSpreadsheet {
s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI")); s1.getRow(1).getCell(1).setCellValue(createHelper.createRichTextString("Link to POI"));
// Save // Save
FileOutputStream fout = new FileOutputStream("/tmp/NewFile.xlsx"); FileOutputStream fout = new FileOutputStream("NewFile.xlsx");
wb.write(fout); wb.write(fout);
fout.close(); fout.close();
System.out.println("Done"); System.out.println("Done");

View File

@ -0,0 +1,59 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
/**
* Fills and Colors
*/
public class FillsAndColors {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue(new XSSFRichTextString("X"));
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,44 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
/**
* Iterate over rows and cells
*/
public class IterateCells {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook(args[0]);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
Sheet sheet = wb.getSheetAt(i);
System.out.println(wb.getSheetName(i));
for (Row row : sheet) {
System.out.println("rownum: " + row.getRowNum());
for (Cell cell : row) {
System.out.println(cell.toString());
}
}
}
}
}

View File

@ -0,0 +1,49 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.hssf.util.Region;
import java.io.FileOutputStream;
/**
* Merging cells
*/
public class MergingCells {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short) 1);
Cell cell = row.createCell((short) 1);
cell.setCellValue(new XSSFRichTextString("This is a test of merging"));
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,58 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.util.IndexedColors;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
/**
* Working with borders
*/
public class WorkingWithBorders {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
Cell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
CellStyle style = wb.createCellStyle();
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor((short)IndexedColors.BLACK);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor((short)IndexedColors.GREEN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor((short)IndexedColors.BLUE);
style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor((short)IndexedColors.BLACK);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook_borders.xlsx");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,66 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.apache.poi.xssf.util.IndexedColors;
import java.io.FileOutputStream;
/**
* Working with Fonts
*/
public class WorkingWithFonts {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Create a new font and alter it.
Font font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setColor((short)IndexedColors.RED);
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
Cell cell = row.createCell((short) 1);
cell.setCellValue(1974);
//cell.setCellValue(new XSSFRichTextString("This is a test of fonts"));
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("fonts.xlsx");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,95 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
/**
* The enumeration value indicating horizontal alignment of a cell,
* i.e., whether it is aligned general, left, right, horizontally centered, filled (replicated),
* justified, centered across multiple cells, or distributed.
*/
public enum HorizontalAlignment {
/**
* The horizontal alignment is general-aligned. Text data is left-aligned.
* Numbers, dates, and times are rightaligned. Boolean types are centered.
* Changing the alignment does not change the type of data.
*/
GENERAL,
/**
* The horizontal alignment is left-aligned, even in Rightto-Left mode.
* Aligns contents at the left edge of the cell. If an indent amount is specified, the contents of
* the cell is indented from the left by the specified number of character spaces. The character spaces are
* based on the default font and font size for the workbook.
*/
LEFT,
/**
* The horizontal alignment is centered, meaning the text is centered across the cell.
*/
CENTER,
/**
* The horizontal alignment is right-aligned, meaning that cell contents are aligned at the right edge of the cell,
* even in Right-to-Left mode.
*/
RIGHT,
/**
* Indicates that the value of the cell should be filled
* across the entire width of the cell. If blank cells to the right also have the fill alignment,
* they are also filled with the value, using a convention similar to centerContinuous.
* <p>
* Additional rules:
* <ol>
* <li>Only whole values can be appended, not partial values.</li>
* <li>The column will not be widened to 'best fit' the filled value</li>
* <li>If appending an additional occurrence of the value exceeds the boundary of the cell
* left/right edge, don't append the additional occurrence of the value.</li>
* <li>The display value of the cell is filled, not the underlying raw number.</li>
* </ol>
* </p>
*/
FILL,
/**
* The horizontal alignment is justified (flush left and right).
* For each line of text, aligns each line of the wrapped text in a cell to the right and left
* (except the last line). If no single line of text wraps in the cell, then the text is not justified.
*/
JUSTIFY,
/**
* The horizontal alignment is centered across multiple cells.
* The information about how many cells to span is expressed in the Sheet Part,
* in the row of the cell in question. For each cell that is spanned in the alignment,
* a cell element needs to be written out, with the same style Id which references the centerContinuous alignment.
*/
CENTER_SELECTION,
/**
* Indicates that each 'word' in each line of text inside the cell is evenly distributed
* across the width of the cell, with flush right and left margins.
* <p>
* When there is also an indent value to apply, both the left and right side of the cell
* are padded by the indent value.
* </p>
* <p> A 'word' is a set of characters with no space character in them. </p>
* <p> Two lines inside a cell are separated by a carriage return. </p>
*/
DISTRIBUTED
}

View File

@ -0,0 +1,69 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.xssf.usermodel;
/**
* This enumeration value indicates the type of vertical alignment for a cell, i.e.,
* whether it is aligned top, bottom, vertically centered, justified or distributed.
*/
public enum VerticalAlignment {
/**
* The vertical alignment is aligned-to-top.
*/
TOP,
/**
* The vertical alignment is centered across the height of the cell.
*/
CENTER,
/**
* The vertical alignment is aligned-to-bottom.
*/
BOTTOM,
/**
* <p>
* When text direction is horizontal: the vertical alignment of lines of text is distributed vertically,
* where each line of text inside the cell is evenly distributed across the height of the cell,
* with flush top and bottom margins.
* </p>
* <p>
* When text direction is vertical: similar behavior as horizontal justification.
* The alignment is justified (flush top and bottom in this case). For each line of text, each
* line of the wrapped text in a cell is aligned to the top and bottom (except the last line).
* If no single line of text wraps in the cell, then the text is not justified.
* </p>
*/
JUSTIFY,
/**
* <p>
* When text direction is horizontal: the vertical alignment of lines of text is distributed vertically,
* where each line of text inside the cell is evenly distributed across the height of the cell,
* with flush top
* </p>
* <p>
* When text direction is vertical: behaves exactly as distributed horizontal alignment.
* The first words in a line of text (appearing at the top of the cell) are flush
* with the top edge of the cell, and the last words of a line of text are flush with the bottom edge of the cell,
* and the line of text is distributed evenly from top to bottom.
* </p>
*/
DISTRIBUTED
}

View File

@ -17,10 +17,7 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment; import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
@ -31,74 +28,73 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellProtection;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
public class XSSFCellStyle implements CellStyle { public class XSSFCellStyle implements CellStyle {
private int cellXfId;
private int cellStyleXfId;
private StylesSource stylesSource;
private CTXf cellXf;
private CTXf cellStyleXf;
private XSSFCellBorder cellBorder;
private XSSFCellFill cellFill;
private XSSFFont font;
private XSSFCellAlignment cellAlignment;
/** private int cellXfId;
* Creates a Cell Style from the supplied parts private int cellStyleXfId;
* @param cellXf The main XF for the cell private StylesSource stylesSource;
* @param cellStyleXf Optional, style xf private CTXf cellXf;
* @param stylesSource Styles Source to work off private CTXf cellStyleXf;
*/ private XSSFCellBorder cellBorder;
public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource) { private XSSFCellFill cellFill;
this.cellXfId = cellXfId; private XSSFFont font;
this.cellStyleXfId = cellStyleXfId; private XSSFCellAlignment cellAlignment;
this.stylesSource = stylesSource;
this.cellXf = stylesSource.getCellXfAt(this.cellXfId);
this.cellStyleXf = stylesSource.getCellStyleXfAt(this.cellStyleXfId);
}
/** /**
* Used so that StylesSource can figure out our location * Creates a Cell Style from the supplied parts
*/ * @param cellXf The main XF for the cell
public CTXf getCoreXf() { * @param cellStyleXf Optional, style xf
return cellXf; * @param stylesSource Styles Source to work off
} */
/** public XSSFCellStyle(int cellXfId, int cellStyleXfId, StylesTable stylesSource) {
* Used so that StylesSource can figure out our location this.cellXfId = cellXfId;
*/ this.cellStyleXfId = cellStyleXfId;
public CTXf getStyleXf() { this.stylesSource = stylesSource;
return cellStyleXf; this.cellXf = stylesSource.getCellXfAt(this.cellXfId);
} this.cellStyleXf = stylesSource.getCellStyleXfAt(this.cellStyleXfId);
}
/** /**
* Creates an empty Cell Style * Used so that StylesSource can figure out our location
*/ */
public XSSFCellStyle(StylesSource stylesSource) { public CTXf getCoreXf() {
this.stylesSource = stylesSource; return cellXf;
}
/**
* Used so that StylesSource can figure out our location
*/
public CTXf getStyleXf() {
return cellStyleXf;
}
// We need a new CTXf for the main styles /**
// TODO decide on a style ctxf * Creates an empty Cell Style
cellXf = CTXf.Factory.newInstance(); */
cellStyleXf = null; public XSSFCellStyle(StylesSource stylesSource) {
} this.stylesSource = stylesSource;
/** // We need a new CTXf for the main styles
* Verifies that this style belongs to the supplied Workbook // TODO decide on a style ctxf
* Styles Source. cellXf = CTXf.Factory.newInstance();
* Will throw an exception if it belongs to a different one. cellStyleXf = null;
* This is normally called when trying to assign a style to a }
* cell, to ensure the cell and the style are from the same
* workbook (if they're not, it won't work) /**
* @throws IllegalArgumentException if there's a workbook mis-match * Verifies that this style belongs to the supplied Workbook
*/ * Styles Source.
public void verifyBelongsToStylesSource(StylesSource src) { * Will throw an exception if it belongs to a different one.
if(this.stylesSource != src) { * This is normally called when trying to assign a style to a
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?"); * cell, to ensure the cell and the style are from the same
} * workbook (if they're not, it won't work)
} * @throws IllegalArgumentException if there's a workbook mis-match
*/
public void verifyBelongsToStylesSource(StylesSource src) {
if(this.stylesSource != src) {
throw new IllegalArgumentException("This Style does not belong to the supplied Workbook Stlyes Source. Are you trying to assign a style from one workbook to the cell of a differnt workbook?");
}
}
/** /**
* Clones all the style information from another * Clones all the style information from another
@ -113,328 +109,328 @@ public class XSSFCellStyle implements CellStyle {
* copy styles from one XSSFWorkbook to another. * copy styles from one XSSFWorkbook to another.
*/ */
public void cloneStyleFrom(CellStyle source) { public void cloneStyleFrom(CellStyle source) {
if(source instanceof XSSFCellStyle) { if(source instanceof XSSFCellStyle) {
this.cloneStyleFrom((XSSFCellStyle)source); this.cloneStyleFrom((XSSFCellStyle)source);
} }
throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle"); throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
} }
public void cloneStyleFrom(XSSFCellStyle source) { public void cloneStyleFrom(XSSFCellStyle source) {
throw new IllegalStateException("TODO"); throw new IllegalStateException("TODO");
} }
public short getAlignment() { public short getAlignment() {
return (short)getAlignmentEnum().intValue(); return (short)(getAlignmentEnum().ordinal());
} }
public STHorizontalAlignment.Enum getAlignmentEnum() { public HorizontalAlignment getAlignmentEnum() {
return getCellAlignment().getHorizontal(); return getCellAlignment().getHorizontal();
} }
public short getBorderBottom() { public short getBorderBottom() {
return getBorderStyleAsShort(BorderSide.BOTTOM); return getBorderStyleAsShort(BorderSide.BOTTOM);
} }
public String getBorderBottomAsString() { public String getBorderBottomAsString() {
return getBorderStyleAsString(BorderSide.BOTTOM); return getBorderStyleAsString(BorderSide.BOTTOM);
} }
public short getBorderLeft() { public short getBorderLeft() {
return getBorderStyleAsShort(BorderSide.LEFT); return getBorderStyleAsShort(BorderSide.LEFT);
} }
public String getBorderLeftAsString() { public String getBorderLeftAsString() {
return getBorderStyleAsString(BorderSide.LEFT); return getBorderStyleAsString(BorderSide.LEFT);
} }
public short getBorderRight() { public short getBorderRight() {
return getBorderStyleAsShort(BorderSide.RIGHT); return getBorderStyleAsShort(BorderSide.RIGHT);
} }
public String getBorderRightAsString() { public String getBorderRightAsString() {
return getBorderStyleAsString(BorderSide.RIGHT); return getBorderStyleAsString(BorderSide.RIGHT);
} }
public short getBorderTop() { public short getBorderTop() {
return getBorderStyleAsShort(BorderSide.TOP); return getBorderStyleAsShort(BorderSide.TOP);
} }
public String getBorderTopAsString() { public String getBorderTopAsString() {
return getBorderStyleAsString(BorderSide.TOP); return getBorderStyleAsString(BorderSide.TOP);
} }
public short getBottomBorderColor() { public short getBottomBorderColor() {
return getBorderColorIndexed(BorderSide.BOTTOM); return getBorderColorIndexed(BorderSide.BOTTOM);
} }
public short getDataFormat() { public short getDataFormat() {
return (short)cellXf.getNumFmtId(); return (short)cellXf.getNumFmtId();
} }
public String getDataFormatString() { public String getDataFormatString() {
return stylesSource.getNumberFormatAt(getDataFormat()); return stylesSource.getNumberFormatAt(getDataFormat());
} }
public short getFillBackgroundColor() { public short getFillBackgroundColor() {
return (short) getCellFill().getFillBackgroundColor().getIndexed(); return (short) getCellFill().getFillBackgroundColor().getIndexed();
} }
public short getFillForegroundColor() { public short getFillForegroundColor() {
return (short) getCellFill().getFillForegroundColor().getIndexed(); return (short) getCellFill().getFillForegroundColor().getIndexed();
} }
public short getFillPattern() { public short getFillPattern() {
return (short) getCellFill().getPatternType().intValue(); return (short) getCellFill().getPatternType().intValue();
} }
public Font getFont(Workbook parentWorkbook) { public Font getFont(Workbook parentWorkbook) {
return getFont(); return getFont();
} }
public Font getFont() { public Font getFont() {
if (font == null) { if (font == null) {
font = (XSSFFont) ((StylesTable)stylesSource).getFontAt(getFontId()); font = (XSSFFont) ((StylesTable)stylesSource).getFontAt(getFontId());
} }
return font; return font;
} }
public short getFontIndex() { public short getFontIndex() {
return (short) getFontId(); return (short) getFontId();
} }
public boolean getHidden() { public boolean getHidden() {
return getCellProtection().getHidden(); return getCellProtection().getHidden();
} }
public short getIndention() { public short getIndention() {
return (short) getCellAlignment().getIndent(); return (short) getCellAlignment().getIndent();
} }
public short getIndex() { public short getIndex() {
return (short) this.cellXfId; return (short) this.cellXfId;
} }
public short getLeftBorderColor() { public short getLeftBorderColor() {
return getBorderColorIndexed(BorderSide.LEFT); return getBorderColorIndexed(BorderSide.LEFT);
} }
public boolean getLocked() { public boolean getLocked() {
return getCellProtection().getLocked(); return getCellProtection().getLocked();
} }
public short getRightBorderColor() { public short getRightBorderColor() {
return getBorderColorIndexed(BorderSide.RIGHT); return getBorderColorIndexed(BorderSide.RIGHT);
} }
public short getRotation() { public short getRotation() {
return (short) getCellAlignment().getTextRotation(); return (short) getCellAlignment().getTextRotation();
} }
public short getTopBorderColor() { public short getTopBorderColor() {
return getBorderColorIndexed(BorderSide.TOP); return getBorderColorIndexed(BorderSide.TOP);
} }
public short getVerticalAlignment() { public short getVerticalAlignment() {
return (short) getVerticalAlignmentEnum().intValue(); return (short) (getVerticalAlignmentEnum().ordinal());
} }
public STVerticalAlignment.Enum getVerticalAlignmentEnum() { public VerticalAlignment getVerticalAlignmentEnum() {
return getCellAlignment().getVertical(); return getCellAlignment().getVertical();
} }
public boolean getWrapText() { public boolean getWrapText() {
return getCellAlignment().getWrapText(); return getCellAlignment().getWrapText();
} }
public void setAlignment(short align) { public void setAlignment(short align) {
getCellAlignment().setHorizontal(STHorizontalAlignment.Enum.forInt(align)); getCellAlignment().setHorizontal(HorizontalAlignment.values()[align]);
} }
public void setAlignementEnum(STHorizontalAlignment.Enum align) { public void setAlignment(HorizontalAlignment align) {
getCellAlignment().setHorizontal(align); setAlignment((short)align.ordinal());
} }
public void setBorderBottom(short border) { public void setBorderBottom(short border) {
setBorderBottomEnum(STBorderStyle.Enum.forInt(border)); setBorderBottomEnum(STBorderStyle.Enum.forInt(border));
} }
public void setBorderBottomEnum(STBorderStyle.Enum style) { public void setBorderBottomEnum(STBorderStyle.Enum style) {
getCellBorder().setBorderStyle(BorderSide.BOTTOM, style); getCellBorder().setBorderStyle(BorderSide.BOTTOM, style);
} }
public void setBorderLeft(short border) { public void setBorderLeft(short border) {
setBorderLeftEnum(STBorderStyle.Enum.forInt(border)); setBorderLeftEnum(STBorderStyle.Enum.forInt(border));
} }
public void setBorderLeftEnum(STBorderStyle.Enum style) { public void setBorderLeftEnum(STBorderStyle.Enum style) {
getCellBorder().setBorderStyle(BorderSide.LEFT, style); getCellBorder().setBorderStyle(BorderSide.LEFT, style);
} }
public void setBorderRight(short border) { public void setBorderRight(short border) {
setBorderRightEnum(STBorderStyle.Enum.forInt(border)); setBorderRightEnum(STBorderStyle.Enum.forInt(border));
} }
public void setBorderRightEnum(STBorderStyle.Enum style) { public void setBorderRightEnum(STBorderStyle.Enum style) {
getCellBorder().setBorderStyle(BorderSide.RIGHT, style); getCellBorder().setBorderStyle(BorderSide.RIGHT, style);
} }
public void setBorderTop(short border) { public void setBorderTop(short border) {
setBorderTopEnum(STBorderStyle.Enum.forInt(border)); setBorderTopEnum(STBorderStyle.Enum.forInt(border));
} }
public void setBorderTopEnum(STBorderStyle.Enum style) { public void setBorderTopEnum(STBorderStyle.Enum style) {
getCellBorder().setBorderStyle(BorderSide.TOP, style); getCellBorder().setBorderStyle(BorderSide.TOP, style);
} }
public void setBottomBorderColor(short color) { public void setBottomBorderColor(short color) {
setBorderColorIndexed(BorderSide.BOTTOM, color); setBorderColorIndexed(BorderSide.BOTTOM, color);
} }
public void setDataFormat(short fmt) { public void setDataFormat(short fmt) {
cellXf.setNumFmtId((long)fmt); cellXf.setNumFmtId((long)fmt);
} }
public void setFillBackgroundColor(short bg) { public void setFillBackgroundColor(short bg) {
getCellFill().setFillBackgroundColor(bg); getCellFill().setFillBackgroundColor(bg);
} }
public void setFillForegroundColor(short bg) { public void setFillForegroundColor(short bg) {
getCellFill().setFillForegroundColor(bg); getCellFill().setFillForegroundColor(bg);
} }
public void setFillPattern(short fp) { public void setFillPattern(short fp) {
// TODO Auto-generated method stub // TODO Auto-generated method stub
} }
public void setFont(Font font) { public void setFont(Font font) {
if(font!=null){ if(font!=null){
long index=this.stylesSource.putFont(font); long index=this.stylesSource.putFont(font);
this.cellXf.setFontId(index); this.cellXf.setFontId(index);
} }
} }
public void setHidden(boolean hidden) { public void setHidden(boolean hidden) {
getCellProtection().setHidden(hidden); getCellProtection().setHidden(hidden);
} }
public void setIndention(short indent) { public void setIndention(short indent) {
getCellAlignment().setIndent(indent); getCellAlignment().setIndent(indent);
} }
public void setLeftBorderColor(short color) { public void setLeftBorderColor(short color) {
setBorderColorIndexed(BorderSide.LEFT, color); setBorderColorIndexed(BorderSide.LEFT, color);
} }
public void setLocked(boolean locked) { public void setLocked(boolean locked) {
getCellProtection().setLocked(locked); getCellProtection().setLocked(locked);
} }
public void setRightBorderColor(short color) { public void setRightBorderColor(short color) {
setBorderColorIndexed(BorderSide.RIGHT, color); setBorderColorIndexed(BorderSide.RIGHT, color);
} }
public void setRotation(short rotation) { public void setRotation(short rotation) {
getCellAlignment().setTextRotation(rotation); getCellAlignment().setTextRotation(rotation);
} }
public void setTopBorderColor(short color) { public void setTopBorderColor(short color) {
setBorderColorIndexed(BorderSide.TOP, color); setBorderColorIndexed(BorderSide.TOP, color);
} }
public void setVerticalAlignment(short align) { public void setVerticalAlignment(short align) {
setVerticalAlignmentEnum(STVerticalAlignment.Enum.forInt(align)); getCellAlignment().setVertical(VerticalAlignment.values()[align]);
} }
public void setVerticalAlignmentEnum(STVerticalAlignment.Enum align) { public void setVerticalAlignment(VerticalAlignment align) {
getCellAlignment().setVertical(align); getCellAlignment().setVertical(align);
} }
public void setWrapText(boolean wrapped) { public void setWrapText(boolean wrapped) {
getCellAlignment().setWrapText(wrapped); getCellAlignment().setWrapText(wrapped);
} }
public XSSFColor getBorderColor(BorderSide side) { public XSSFColor getBorderColor(BorderSide side) {
return getCellBorder().getBorderColor(side); return getCellBorder().getBorderColor(side);
} }
public void setBorderColor(BorderSide side, XSSFColor color) { public void setBorderColor(BorderSide side, XSSFColor color) {
getCellBorder().setBorderColor(side, color); getCellBorder().setBorderColor(side, color);
} }
private XSSFCellBorder getCellBorder() { private XSSFCellBorder getCellBorder() {
if (cellBorder == null) { if (cellBorder == null) {
// TODO make a common Cell Border object // TODO make a common Cell Border object
cellBorder = ((StylesTable)stylesSource).getBorderAt(getBorderId()); cellBorder = ((StylesTable)stylesSource).getBorderAt(getBorderId());
} }
return cellBorder; return cellBorder;
} }
private int getBorderId() { private int getBorderId() {
if (cellXf.isSetBorderId()) { if (cellXf.isSetBorderId()) {
return (int) cellXf.getBorderId(); return (int) cellXf.getBorderId();
} }
return (int) cellStyleXf.getBorderId(); return (int) cellStyleXf.getBorderId();
} }
private XSSFCellFill getCellFill() { private XSSFCellFill getCellFill() {
if (cellFill == null) { if (cellFill == null) {
cellFill = ((StylesTable)stylesSource).getFillAt(getFillId()); cellFill = ((StylesTable)stylesSource).getFillAt(getFillId());
} }
return cellFill; return cellFill;
} }
private int getFillId() { private int getFillId() {
if (cellXf.isSetFillId()) { if (cellXf.isSetFillId()) {
return (int) cellXf.getFillId(); return (int) cellXf.getFillId();
} }
return (int) cellStyleXf.getFillId(); return (int) cellStyleXf.getFillId();
} }
private int getFontId() { private int getFontId() {
if (cellXf.isSetFontId()) { if (cellXf.isSetFontId()) {
return (int) cellXf.getFontId(); return (int) cellXf.getFontId();
} }
return (int) cellStyleXf.getFontId(); return (int) cellStyleXf.getFontId();
} }
private CTCellProtection getCellProtection() { private CTCellProtection getCellProtection() {
if (cellXf.getProtection() == null) { if (cellXf.getProtection() == null) {
cellXf.addNewProtection(); cellXf.addNewProtection();
} }
return cellXf.getProtection(); return cellXf.getProtection();
} }
private XSSFCellAlignment getCellAlignment() { public XSSFCellAlignment getCellAlignment() {
if (this.cellAlignment == null) { if (this.cellAlignment == null) {
this.cellAlignment = new XSSFCellAlignment(getCTCellAlignment()); this.cellAlignment = new XSSFCellAlignment(getCTCellAlignment());
} }
return this.cellAlignment; return this.cellAlignment;
} }
private CTCellAlignment getCTCellAlignment() { private CTCellAlignment getCTCellAlignment() {
if (cellXf.getAlignment() == null) { if (cellXf.getAlignment() == null) {
cellXf.setAlignment(CTCellAlignment.Factory.newInstance()); cellXf.setAlignment(CTCellAlignment.Factory.newInstance());
} }
return cellXf.getAlignment(); return cellXf.getAlignment();
} }
private short getBorderColorIndexed(BorderSide side) { private short getBorderColorIndexed(BorderSide side) {
return (short) getBorderColor(side).getIndexed(); return (short) getBorderColor(side).getIndexed();
} }
private void setBorderColorIndexed(BorderSide side, long color) { private void setBorderColorIndexed(BorderSide side, long color) {
getBorderColor(side).setIndexed(color); getBorderColor(side).setIndexed(color);
} }
private short getBorderStyleAsShort(BorderSide side) { private short getBorderStyleAsShort(BorderSide side) {
return (short) (getBorderStyle(side).intValue() - 1); return (short) (getBorderStyle(side).intValue() - 1);
} }
private String getBorderStyleAsString(BorderSide side) { private String getBorderStyleAsString(BorderSide side) {
return getBorderStyle(side).toString(); return getBorderStyle(side).toString();
} }
private STBorderStyle.Enum getBorderStyle(BorderSide side) { private STBorderStyle.Enum getBorderStyle(BorderSide side) {
return getCellBorder().getBorderStyle(side); return getCellBorder().getBorderStyle(side);
} }
} }

View File

@ -19,59 +19,71 @@ package org.apache.poi.xssf.usermodel.extensions;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
import org.apache.poi.xssf.usermodel.HorizontalAlignment;
import org.apache.poi.xssf.usermodel.VerticalAlignment;
/**
* Cell settings avaiable in the Format/Alignment tab
*/
public class XSSFCellAlignment { public class XSSFCellAlignment {
private CTCellAlignment cellAlignement; private CTCellAlignment cellAlignement;
public XSSFCellAlignment(CTCellAlignment cellAlignment) { public XSSFCellAlignment(CTCellAlignment cellAlignment) {
this.cellAlignement = cellAlignment; this.cellAlignement = cellAlignment;
} }
public STVerticalAlignment.Enum getVertical() { public VerticalAlignment getVertical() {
if (cellAlignement.getVertical() == null) { STVerticalAlignment.Enum align = cellAlignement.getVertical();
cellAlignement.setVertical(STVerticalAlignment.TOP); if(align == null) align = STVerticalAlignment.BOTTOM;
}
return cellAlignement.getVertical();
}
public void setVertical(STVerticalAlignment.Enum vertical) { return VerticalAlignment.values()[align.intValue() - 1];
cellAlignement.setVertical(vertical); }
}
public STHorizontalAlignment.Enum getHorizontal() { public void setVertical(VerticalAlignment vertical) {
if (cellAlignement.getHorizontal() == null) { cellAlignement.setVertical(STVerticalAlignment.Enum.forInt(vertical.ordinal() + 1));
cellAlignement.setHorizontal(STHorizontalAlignment.GENERAL); }
}
return cellAlignement.getHorizontal();
}
public void setHorizontal(STHorizontalAlignment.Enum horizontal) { public HorizontalAlignment getHorizontal() {
cellAlignement.setHorizontal(horizontal); STHorizontalAlignment.Enum align = cellAlignement.getHorizontal();
} if(align == null) align = STHorizontalAlignment.GENERAL;
public long getIndent() { return HorizontalAlignment.values()[align.intValue() - 1];
return cellAlignement.getIndent(); }
}
public void setIndent(long indent) { public void setHorizontal(HorizontalAlignment align) {
cellAlignement.setIndent(indent); cellAlignement.setHorizontal(STHorizontalAlignment.Enum.forInt(align.ordinal() + 1));
} }
public long getTextRotation() { public long getIndent() {
return cellAlignement.getTextRotation(); return cellAlignement.getIndent();
} }
public void setTextRotation(long rotation) { public void setIndent(long indent) {
cellAlignement.setTextRotation(rotation); cellAlignement.setIndent(indent);
} }
public boolean getWrapText() { public long getTextRotation() {
return cellAlignement.getWrapText(); return cellAlignement.getTextRotation();
} }
public void setWrapText(boolean wrapped) { public void setTextRotation(long rotation) {
cellAlignement.setWrapText(wrapped); cellAlignement.setTextRotation(rotation);
} }
public boolean getWrapText() {
return cellAlignement.getWrapText();
}
public void setWrapText(boolean wrapped) {
cellAlignement.setWrapText(wrapped);
}
/**
* Access to low-level data
*/
public CTCellAlignment getCTCellAlignment() {
return cellAlignement;
}
} }

View File

@ -19,324 +19,280 @@ package org.apache.poi.xssf.usermodel;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.StylesSource;
import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.poi.xssf.usermodel.extensions.XSSFColor; import org.apache.poi.xssf.usermodel.extensions.XSSFColor;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import org.apache.poi.xssf.util.IndexedColors; import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellXfs;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment;
public class TestXSSFCellStyle extends TestCase { public class TestXSSFCellStyle extends TestCase {
private StylesTable stylesTable; private StylesTable stylesTable;
private CTBorder ctBorderA; private CTBorder ctBorderA;
private CTFill ctFill; private CTFill ctFill;
private CTFont ctFont; private CTFont ctFont;
private CTXf cellStyleXf; private CTXf cellStyleXf;
private CTXf cellXf; private CTXf cellXf;
private CTCellXfs cellXfs; private CTCellXfs cellXfs;
private XSSFCellStyle cellStyle; private XSSFCellStyle cellStyle;
private CTStylesheet ctStylesheet; private CTStylesheet ctStylesheet;
public void setUp() { public void setUp() {
stylesTable = new StylesTable(); stylesTable = new StylesTable();
ctStylesheet = stylesTable._getRawStylesheet(); ctStylesheet = stylesTable._getRawStylesheet();
// Until we do XSSFBorder properly, cheat // Until we do XSSFBorder properly, cheat
ctBorderA = CTBorder.Factory.newInstance(); ctBorderA = CTBorder.Factory.newInstance();
XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA); XSSFCellBorder borderA = new XSSFCellBorder(ctBorderA);
long borderId = stylesTable.putBorder(borderA); long borderId = stylesTable.putBorder(borderA);
assertEquals(1, borderId); 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(1, fillId); assertEquals(1, 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);
stylesTable.putCellStyleXf(cellStyleXf); stylesTable.putCellStyleXf(cellStyleXf);
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); 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(new String("1234".getBytes()), 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); 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(new String("abcd".getBytes()), 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); 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(new String("1234".getBytes()), 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); 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(new String("af67".getBytes()), 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() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor(); CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(4); ctBgColor.setIndexed(4);
assertEquals(4, cellStyle.getFillBackgroundColor()); assertEquals(4, cellStyle.getFillBackgroundColor());
} }
public void testGetFillForegroundColor() { public void testGetFillForegroundColor() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor(); CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5); ctFgColor.setIndexed(5);
assertEquals(5, cellStyle.getFillForegroundColor()); assertEquals(5, cellStyle.getFillForegroundColor());
} }
public void testGetFillPattern() { public void testGetFillPattern() {
CTPatternFill ctPatternFill = ctFill.addNewPatternFill(); CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
ctPatternFill.setPatternType(STPatternType.DARK_DOWN); ctPatternFill.setPatternType(STPatternType.DARK_DOWN);
assertEquals(8, cellStyle.getFillPattern()); assertEquals(8, cellStyle.getFillPattern());
} }
public void testGetSetFont() { public void testGetFont() {
assertNotNull(this.cellStyle.getFont()); assertNotNull(cellStyle.getFont());
}
StylesSource stylesSource=new StylesTable(); public void testGetSetHidden() {
XSSFFont xssfFont=new XSSFFont(); assertFalse(cellStyle.getHidden());
xssfFont.setFontName("Arial"); cellXf.getProtection().setHidden(true);
stylesSource.putFont(xssfFont); assertTrue(cellStyle.getHidden());
XSSFCellStyle cellStyle=new XSSFCellStyle(stylesSource); cellStyle.setHidden(false);
assertFalse(cellStyle.getHidden());
}
XSSFFont xssfFont2=new XSSFFont(); public void testGetSetLocked() {
xssfFont2.setFontName("courier"); assertFalse(cellStyle.getLocked());
xssfFont2.setFontHeightInPoints((short)10); cellXf.getProtection().setLocked(true);
assertTrue(cellStyle.getLocked());
cellStyle.setLocked(false);
assertFalse(cellStyle.getLocked());
}
cellStyle.setFont(xssfFont2); public void testGetSetIndent() {
assertEquals(2,cellStyle.getFontIndex()); assertEquals((short)0, cellStyle.getIndention());
assertEquals(xssfFont2.getFontName(),cellStyle.getFont().getFontName()); cellXf.getAlignment().setIndent(3);
assertEquals(stylesSource.getFontAt(2).getFontHeightInPoints(),cellStyle.getFont().getFontHeightInPoints()); assertEquals((short)3, cellStyle.getIndention());
cellStyle.setIndention((short) 13);
assertEquals((short)13, cellXf.getAlignment().getIndent());
}
cellStyle.setFont(xssfFont); public void testGetSetAlignement() {
assertEquals(1,cellStyle.getFontIndex()); 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());
XSSFFont xssfFont3=new XSSFFont(); cellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
xssfFont3.setFontName("Arial"); assertEquals(XSSFCellStyle.ALIGN_JUSTIFY, cellStyle.getAlignment());
cellStyle.setFont(xssfFont3); assertEquals(HorizontalAlignment.JUSTIFY, cellStyle.getAlignmentEnum());
assertNotSame(1,cellStyle.getFontIndex()); 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 testGetSetHidden() { public void testGetSetVerticalAlignment() {
assertFalse(cellStyle.getHidden()); assertEquals(VerticalAlignment.BOTTOM, cellStyle.getVerticalAlignmentEnum());
cellXf.getProtection().setHidden(true); assertEquals(XSSFCellStyle.VERTICAL_BOTTOM, cellStyle.getVerticalAlignment());
assertTrue(cellStyle.getHidden()); assertNull(cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
cellStyle.setHidden(false);
assertFalse(cellStyle.getHidden());
}
public void testGetSetLocked() { cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
assertFalse(cellStyle.getLocked()); assertEquals(XSSFCellStyle.VERTICAL_CENTER, cellStyle.getVerticalAlignment());
cellXf.getProtection().setLocked(true); assertEquals(VerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum());
assertTrue(cellStyle.getLocked()); assertEquals(STVerticalAlignment.CENTER, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
cellStyle.setLocked(false);
assertFalse(cellStyle.getLocked());
}
public void testGetSetIndent() { cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_JUSTIFY);
assertEquals((short)0, cellStyle.getIndention()); assertEquals(XSSFCellStyle.VERTICAL_JUSTIFY, cellStyle.getVerticalAlignment());
cellXf.getAlignment().setIndent(3); assertEquals(VerticalAlignment.JUSTIFY, cellStyle.getVerticalAlignmentEnum());
assertEquals((short)3, cellStyle.getIndention()); assertEquals(STVerticalAlignment.JUSTIFY, cellStyle.getCellAlignment().getCTCellAlignment().getVertical());
cellStyle.setIndention((short) 13); }
assertEquals((short)13, cellXf.getAlignment().getIndent());
}
public void testGetSetAlignement() { public void testGetSetWrapText() {
assertEquals(1, cellStyle.getAlignment()); assertFalse(cellStyle.getWrapText());
cellStyle.setAlignment((short)2); cellXf.getAlignment().setWrapText(true);
assertEquals(STHorizontalAlignment.LEFT, cellStyle.getAlignmentEnum()); assertTrue(cellStyle.getWrapText());
cellStyle.setAlignementEnum(STHorizontalAlignment.JUSTIFY); cellStyle.setWrapText(false);
assertEquals((short)6, cellStyle.getAlignment()); assertFalse(cellXf.getAlignment().getWrapText());
} }
public void testGetSetVerticalAlignment() { /**
assertEquals(1, cellStyle.getVerticalAlignment()); * Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
cellStyle.setVerticalAlignment((short)2); */
assertEquals(STVerticalAlignment.CENTER, cellStyle.getVerticalAlignmentEnum()); public void testCloneStyleSameWB() throws Exception {
cellStyle.setVerticalAlignmentEnum(STVerticalAlignment.JUSTIFY); // TODO
assertEquals((short)4, cellStyle.getVerticalAlignment()); }
} /**
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
public void testGetSetWrapText() { */
assertFalse(cellStyle.getWrapText()); public void testCloneStyleDiffWB() throws Exception {
cellXf.getAlignment().setWrapText(true); // TODO
assertTrue(cellStyle.getWrapText()); }
cellStyle.setWrapText(false);
assertFalse(cellXf.getAlignment().getWrapText());
}
public void testGetSetFillBackgroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctBgColor = ctPatternFill.addNewBgColor();
ctBgColor.setIndexed(IndexedColors.BLUE);
assertEquals(IndexedColors.BLUE, cellStyle.getFillBackgroundColor());
cellStyle.setFillBackgroundColor((short)IndexedColors.GREEN);
assertEquals(IndexedColors.GREEN,ctFill.getPatternFill().getBgColor().getIndexed());
}
public void testGetSetFillForegroundColor() {
setUp();
CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
CTColor ctFgColor = ctPatternFill.addNewFgColor();
ctFgColor.setIndexed(5);
assertEquals(5, cellStyle.getFillForegroundColor());
ctFgColor.setIndexed(IndexedColors.BLUE);
assertEquals(IndexedColors.BLUE, cellStyle.getFillForegroundColor());
cellStyle.setFillForegroundColor((short)IndexedColors.GREEN);
assertEquals(IndexedColors.GREEN,ctFill.getPatternFill().getFgColor().getIndexed());
}
/**
* Cloning one XSSFCellStyle onto Another, same XSSFWorkbook
*/
public void testCloneStyleSameWB() throws Exception {
// TODO
}
/**
* Cloning one XSSFCellStyle onto Another, different XSSFWorkbooks
*/
public void testCloneStyleDiffWB() throws Exception {
// TODO
}
} }