return createCell(row, column, value, null);
}
-
/**
* Take a cell, and align it.
*
- *@param cell the cell to set the alignment for
- *@param workbook The workbook that is being worked with.
- *@param align the column alignment to use.
+ * @param cell the cell to set the alignment for
+ * @param workbook The workbook that is being worked with.
+ * @param align the column alignment to use.
+ *
+ * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setAlignment(Cell, short)} instead.
*
* @see CellStyle for alignment options
*/
public static void setAlignment(Cell cell, Workbook workbook, short align) {
- setCellStyleProperty(cell, workbook, ALIGNMENT, Short.valueOf(align));
+ setAlignment(cell, align);
}
+ /**
+ * Take a cell, and align it.
+ *
+ * @param cell the cell to set the alignment for
+ * @param align the column alignment to use.
+ *
+ * @see CellStyle for alignment options
+ */
+ public static void setAlignment(Cell cell, short align) {
+ setCellStyleProperty(cell, ALIGNMENT, Short.valueOf(align));
+ }
+
/**
* Take a cell, and apply a font to it
*
- *@param cell the cell to set the alignment for
- *@param workbook The workbook that is being worked with.
- *@param font The Font that you want to set...
+ * @param cell the cell to set the alignment for
+ * @param workbook The workbook that is being worked with.
+ * @param font The Font that you want to set.
+ * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
+ *
+ * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setFont(Cell, Font)} instead.
*/
public static void setFont(Cell cell, Workbook workbook, Font font) {
+ setFont(cell, font);
+ }
+
+ /**
+ * Take a cell, and apply a font to it
+ *
+ * @param cell the cell to set the alignment for
+ * @param font The Font that you want to set.
+ * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
+ */
+ public static void setFont(Cell cell, Font font) {
// Check if font belongs to workbook
+ Workbook wb = cell.getSheet().getWorkbook();
final short fontIndex = font.getIndex();
- if (!workbook.getFontAt(fontIndex).equals(font)) {
+ if (!wb.getFontAt(fontIndex).equals(font)) {
throw new IllegalArgumentException("Font does not belong to this workbook");
}
// Check if cell belongs to workbook
// (checked in setCellStyleProperty)
- setCellStyleProperty(cell, workbook, FONT, fontIndex);
+ setCellStyleProperty(cell, FONT, fontIndex);
}
/**
* @since POI 3.14 beta 2
*/
public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) {
- @SuppressWarnings("resource")
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle originalStyle = cell.getCellStyle();
CellStyle newStyle = null;
* {@link #setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)},
* which is faster and does not add unnecessary intermediate CellStyles to the workbook.</p>
*
+ * @param cell The cell that is to be changed.
* @param workbook The workbook that is being worked with.
* @param propertyName The name of the property that is to be changed.
* @param propertyValue The value of the property that is to be changed.
- * @param cell The cell that needs it's style changes
+ *
+ * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setCellStyleProperty(Cell, String, Object)} instead.
*/
public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName,
Object propertyValue) {
- if (cell.getSheet().getWorkbook() != workbook) {
- throw new IllegalArgumentException("Cannot set cell style property. Cell does not belong to workbook.");
- }
+ setCellStyleProperty(cell, propertyName, propertyValue);
+ }
- Map<String, Object> values = Collections.singletonMap(propertyName, propertyValue);
- setCellStyleProperties(cell, values);
+ /**
+ * <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
+ * current style plus a single style property <code>propertyName</code> with value
+ * <code>propertyValue</code>.
+ * A new style is created if the workbook does not contain a matching style.</p>
+ *
+ * <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
+ * same style.</p>
+ *
+ * <p>If setting more than one cell style property on a cell, use
+ * {@link #setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)},
+ * which is faster and does not add unnecessary intermediate CellStyles to the workbook.</p>
+ *
+ * @param cell The cell that is to be changed.
+ * @param propertyName The name of the property that is to be changed.
+ * @param propertyValue The value of the property that is to be changed.
+ */
+ public static void setCellStyleProperty(Cell cell, String propertyName, Object propertyValue) {
+ Map<String, Object> property = Collections.singletonMap(propertyName, propertyValue);
+ setCellStyleProperties(cell, property);
}
/**
* Returns a map containing the format properties of the given cell style.
- * The returned map is not tied to <code>style</code>, so subsequent changes
- * to <code>style</code> will not modify the map, and changes to the returned
- * map will not modify the cell style. The returned map is mutable.
+ * The returned map is not tied to <code>style</code>, so subsequent changes
+ * to <code>style</code> will not modify the map, and changes to the returned
+ * map will not modify the cell style. The returned map is mutable.
*
* @param style cell style
* @return map of format properties (String -> Object)
*/
private static final class CellPropertySetter {
- private final Workbook _workbook;
private final String _propertyName;
private final Short _propertyValue;
- public CellPropertySetter(Workbook workbook, String propertyName, int value) {
- _workbook = workbook;
+ public CellPropertySetter(String propertyName, int value) {
_propertyName = propertyName;
_propertyValue = Short.valueOf((short) value);
}
public void setProperty(Row row, int column) {
Cell cell = CellUtil.getCell(row, column);
- CellUtil.setCellStyleProperty(cell, _workbook, _propertyName, _propertyValue);
+ CellUtil.setCellStyleProperty(cell, _propertyName, _propertyValue);
}
}
int rowEnd = region.getLastRow();
int column = region.getFirstColumn();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_LEFT, border);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_LEFT, border);
for (int i = rowStart; i <= rowEnd; i++) {
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
int rowEnd = region.getLastRow();
int column = region.getFirstColumn();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.LEFT_BORDER_COLOR,
- color);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.LEFT_BORDER_COLOR, color);
for (int i = rowStart; i <= rowEnd; i++) {
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
int rowEnd = region.getLastRow();
int column = region.getLastColumn();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_RIGHT, border);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_RIGHT, border);
for (int i = rowStart; i <= rowEnd; i++) {
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
int rowEnd = region.getLastRow();
int column = region.getLastColumn();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.RIGHT_BORDER_COLOR,
- color);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.RIGHT_BORDER_COLOR, color);
for (int i = rowStart; i <= rowEnd; i++) {
cps.setProperty(CellUtil.getRow(i, sheet), column);
}
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
int rowIndex = region.getLastRow();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_BOTTOM, border);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_BOTTOM, border);
Row row = CellUtil.getRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++) {
cps.setProperty(row, i);
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
int rowIndex = region.getLastRow();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BOTTOM_BORDER_COLOR,
- color);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.BOTTOM_BORDER_COLOR, color);
Row row = CellUtil.getRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++) {
cps.setProperty(row, i);
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
int rowIndex = region.getFirstRow();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_TOP, border);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_TOP, border);
Row row = CellUtil.getRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++) {
cps.setProperty(row, i);
int colStart = region.getFirstColumn();
int colEnd = region.getLastColumn();
int rowIndex = region.getFirstRow();
- CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.TOP_BORDER_COLOR, color);
+ CellPropertySetter cps = new CellPropertySetter(CellUtil.TOP_BORDER_COLOR, color);
Row row = CellUtil.getRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++) {
cps.setProperty(row, i);
row2.getCell(1).getCellStyle().setBorderBottom(CellStyle.BORDER_THIN);\r
\r
Cell cell0 = CellUtil.getCell(row3, 0);\r
- CellUtil.setCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
+ CellUtil.setCellStyleProperty(cell0, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
Cell cell1 = CellUtil.getCell(row3, 1);\r
- CellUtil.setCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
+ CellUtil.setCellStyleProperty(cell1, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_THIN);\r
\r
RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, range4, sheet, wb);\r
\r
package org.apache.poi.ss.util;\r
\r
import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotNull;\r
import static org.junit.Assert.assertNotEquals;\r
-import static org.junit.Assert.assertNull;\r
+import static org.junit.Assert.assertNotNull;\r
import static org.junit.Assert.assertSame;\r
import static org.junit.Assert.fail;\r
\r
\r
// Add a border should create a new style\r
int styCnt1 = wb.getNumCellStyles();\r
- CellUtil.setCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
+ CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
int styCnt2 = wb.getNumCellStyles();\r
assertEquals(styCnt2, styCnt1+1);\r
\r
// Add same border to another cell, should not create another style\r
c = r.createCell(1);\r
- CellUtil.setCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
+ CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.THIN);\r
int styCnt3 = wb.getNumCellStyles();\r
assertEquals(styCnt3, styCnt2);\r
\r
props.put(CellUtil.BORDER_RIGHT, BorderStyle.THIN);\r
CellUtil.setCellStyleProperties(c, props);\r
int styCnt2 = wb.getNumCellStyles();\r
- assertEquals(styCnt1+1, styCnt2);\r
+ assertEquals("Only one additional style should have been created", styCnt1 + 1, styCnt2);\r
\r
// Add same border another to same cell, should not create another style\r
c = r.createCell(1);\r
assertEquals(CellStyle.ALIGN_GENERAL, B1.getCellStyle().getAlignment());\r
\r
// get/set alignment modifies the cell's style\r
- CellUtil.setAlignment(A1, wb, CellStyle.ALIGN_RIGHT);\r
+ CellUtil.setAlignment(A1, CellStyle.ALIGN_RIGHT);\r
assertEquals(CellStyle.ALIGN_RIGHT, A1.getCellStyle().getAlignment());\r
\r
// get/set alignment doesn't affect the style of cells with\r
assertEquals(defaultFontIndex, B1.getCellStyle().getFontIndex());\r
\r
// get/set alignment modifies the cell's style\r
- CellUtil.setFont(A1, wb, font);\r
+ CellUtil.setFont(A1, font);\r
assertEquals(customFontIndex, A1.getCellStyle().getFontIndex());\r
\r
// get/set alignment doesn't affect the style of cells with\r
Cell A1 = wb1.createSheet().createRow(0).createCell(0);\r
\r
// okay\r
- CellUtil.setFont(A1, wb1, font1);\r
+ CellUtil.setFont(A1, font1);\r
\r
// font belongs to different workbook\r
try {\r
- CellUtil.setFont(A1, wb1, font2);\r
- fail("setFont not allowed if font belongs to a different workbook"); \r
+ CellUtil.setFont(A1, font2);\r
+ fail("setFont not allowed if font belongs to a different workbook");\r
} catch (final IllegalArgumentException e) {\r
if (e.getMessage().startsWith("Font does not belong to this workbook")) {\r
// expected\r
else {\r
throw e;\r
}\r
- }\r
-\r
- // cell belongs to different workbook\r
- try {\r
- CellUtil.setFont(A1, wb2, font2);\r
- fail("setFont not allowed if cell belongs to a different workbook"); \r
- } catch (final IllegalArgumentException e) {\r
- if (e.getMessage().startsWith("Cannot set cell style property. Cell does not belong to workbook.")) {\r
- // expected\r
- }\r
- else {\r
- throw e;\r
- }\r
+ } finally {\r
+ wb1.close();\r
+ wb2.close();\r
}\r
}\r
}\r