* the Excel spreadsheet file this code will create.\r
*/\r
public void demonstrateMethodCalls(String outputFilename) throws IOException {\r
- HSSFWorkbook workbook = null;\r
- HSSFSheet sheet = null;\r
- HSSFRow row = null;\r
- HSSFCell cell = null;\r
- ArrayList<MultiLevelListItem> multiLevelListItems = null;\r
- ArrayList<String> listItems = null;\r
+ HSSFWorkbook workbook = new HSSFWorkbook();\r
try {\r
- workbook = new HSSFWorkbook();\r
- sheet = workbook.createSheet("In Cell Lists");\r
- row = sheet.createRow(0);\r
+ HSSFSheet sheet = workbook.createSheet("In Cell Lists");\r
+ HSSFRow row = sheet.createRow(0);\r
\r
// Create a cell at A1 and insert a single, bulleted, item into\r
// that cell.\r
- cell = row.createCell(0);\r
+ HSSFCell cell = row.createCell(0);\r
this.bulletedItemInCell(workbook, "List Item", cell);\r
\r
// Create a cell at A2 and insert a plain list - that is one\r
// whose items are neither bulleted or numbered - into that cell.\r
row = sheet.createRow(1);\r
cell = row.createCell(0);\r
- listItems = new ArrayList<String>();\r
+ ArrayList<String> listItems = new ArrayList<String>();\r
listItems.add("List Item One.");\r
listItems.add("List Item Two.");\r
listItems.add("List Item Three.");\r
// to preserve order.\r
row = sheet.createRow(4);\r
cell = row.createCell(0);\r
- multiLevelListItems = new ArrayList<MultiLevelListItem>();\r
+ ArrayList<MultiLevelListItem> multiLevelListItems = new ArrayList<MultiLevelListItem>();\r
listItems = new ArrayList<String>();\r
listItems.add("ML List Item One - Sub Item One.");\r
listItems.add("ML List Item One - Sub Item Two.");\r
ioEx.printStackTrace(System.out);\r
}\r
finally {\r
- if (workbook != null) {\r
- workbook.close();\r
- }\r
+ workbook.close();\r
}\r
}\r
\r
* will be written.\r
*/\r
public void listInCell(HSSFWorkbook workbook, ArrayList<String> listItems, HSSFCell cell) {\r
- StringBuffer buffer = new StringBuffer();\r
+ StringBuilder buffer = new StringBuilder();\r
HSSFCellStyle wrapStyle = workbook.createCellStyle();\r
wrapStyle.setWrapText(true);\r
for(String listItem : listItems) {\r
HSSFCell cell,\r
int startingValue,\r
int increment) {\r
- StringBuffer buffer = new StringBuffer();\r
+ StringBuilder buffer = new StringBuilder();\r
int itemNumber = startingValue;\r
// Note that again, an HSSFCellStye object is required and that\r
// it's wrap text property should be set to 'true'\r
// Note that the basic method is identical to the listInCell() method\r
// with one difference; a number prefixed to the items text.\r
for(String listItem : listItems) {\r
- buffer.append(String.valueOf(itemNumber) + ". ");\r
+ buffer.append(itemNumber).append(". ");\r
buffer.append(listItem);\r
buffer.append("\n");\r
itemNumber += increment;\r
public void bulletedListInCell(HSSFWorkbook workbook,\r
ArrayList<String> listItems,\r
HSSFCell cell) {\r
- StringBuffer buffer = new StringBuffer();\r
+ StringBuilder buffer = new StringBuilder();\r
// Note that again, an HSSFCellStye object is required and that\r
// it's wrap text property should be set to 'true'\r
HSSFCellStyle wrapStyle = workbook.createCellStyle();\r
public void multiLevelListInCell(HSSFWorkbook workbook,\r
ArrayList<MultiLevelListItem> multiLevelListItems,\r
HSSFCell cell) {\r
- StringBuffer buffer = new StringBuffer();\r
- ArrayList<String> lowerLevelItems = null;\r
+ StringBuilder buffer = new StringBuilder();\r
// Note that again, an HSSFCellStye object is required and that\r
// it's wrap text property should be set to 'true'\r
HSSFCellStyle wrapStyle = workbook.createCellStyle();\r
buffer.append("\n");\r
// and then an ArrayList whose elements encapsulate the text\r
// for the lower level list items.\r
- lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
+ ArrayList<String> lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {\r
for(String item : lowerLevelItems) {\r
buffer.append(InCellLists.TAB);\r
int highLevelIncrement,\r
int lowLevelStartingValue,\r
int lowLevelIncrement) {\r
- StringBuffer buffer = new StringBuffer();\r
+ StringBuilder buffer = new StringBuilder();\r
int highLevelItemNumber = highLevelStartingValue;\r
- int lowLevelItemNumber = 0;\r
- ArrayList<String> lowerLevelItems = null;\r
// Note that again, an HSSFCellStye object is required and that\r
// it's wrap text property should be set to 'true'\r
HSSFCellStyle wrapStyle = workbook.createCellStyle();\r
for(MultiLevelListItem multiLevelListItem : multiLevelListItems) {\r
// For each element in the ArrayList, get the text for the high\r
// level list item......\r
- buffer.append(String.valueOf(highLevelItemNumber));\r
+ buffer.append(highLevelItemNumber);\r
buffer.append(". ");\r
buffer.append(multiLevelListItem.getItemText());\r
buffer.append("\n");\r
// and then an ArrayList whose elements encapsulate the text\r
// for the lower level list items.\r
- lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
+ ArrayList<String> lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {\r
- lowLevelItemNumber = lowLevelStartingValue;\r
+ int lowLevelItemNumber = lowLevelStartingValue;\r
for(String item : lowerLevelItems) {\r
buffer.append(InCellLists.TAB);\r
- buffer.append(String.valueOf(highLevelItemNumber));\r
+ buffer.append(highLevelItemNumber);\r
buffer.append(".");\r
- buffer.append(String.valueOf(lowLevelItemNumber));\r
+ buffer.append(lowLevelItemNumber);\r
buffer.append(" ");\r
buffer.append(item);\r
buffer.append("\n");\r
public void multiLevelBulletedListInCell(HSSFWorkbook workbook,\r
ArrayList<MultiLevelListItem> multiLevelListItems,\r
HSSFCell cell) {\r
- StringBuffer buffer = new StringBuffer();\r
- ArrayList<String> lowerLevelItems = null;\r
+ StringBuilder buffer = new StringBuilder();\r
// Note that again, an HSSFCellStye object is required and that\r
// it's wrap text property should be set to 'true'\r
HSSFCellStyle wrapStyle = workbook.createCellStyle();\r
buffer.append("\n");\r
// and then an ArrayList whose elements encapsulate the text\r
// for the lower level list items.\r
- lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
+ ArrayList<String> lowerLevelItems = multiLevelListItem.getLowerLevelItems();\r
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {\r
for(String item : lowerLevelItems) {\r
buffer.append(InCellLists.TAB);\r
// We should only be checking merged regions if useMergedCells is true. Why are we doing this for-loop?
int colspan = 1;
for (CellRangeAddress region : sheet.getMergedRegions()) {
- if (containsCell(region, row.getRowNum(), column)) {
+ if (region.isInRange(row.getRowNum(), column)) {
if (!useMergedCells) {
// If we're not using merged cells, skip this one and move on to the next.
return -1;
if (cellType == CellType.STRING) {
RichTextString rt = cell.getRichStringCellValue();
String[] lines = rt.getString().split("\\n");
- for (int i = 0; i < lines.length; i++) {
- String txt = lines[i] + defaultChar;
+ for (String line : lines) {
+ String txt = line + defaultChar;
AttributedString str = new AttributedString(txt);
copyAttributes(font, str, 0, txt.length());
* @param defaultCharWidth the width of a character using the default font in a workbook
* @param colspan the number of columns that is spanned by the cell (1 if the cell is not part of a merged region)
* @param style the cell style, which contains text rotation and indention information needed to compute the cell width
- * @param width the minimum best-fit width. This algorithm will only return values greater than or equal to the minimum width.
+ * @param minWidth the minimum best-fit width. This algorithm will only return values greater than or equal to the minimum width.
* @param str the text contained in the cell
* @return the best fit cell width
*/
}
// frameWidth accounts for leading spaces which is excluded from bounds.getWidth()
final double frameWidth = bounds.getX() + bounds.getWidth();
- final double width = Math.max(minWidth, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
- return width;
+ return Math.max(minWidth, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
}
/**
AttributedString str = new AttributedString(String.valueOf(defaultChar));
copyAttributes(defaultFont, str, 0, 1);
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
- int defaultCharWidth = (int) layout.getAdvance();
- return defaultCharWidth;
+ return (int) layout.getAdvance();
}
/**
* Compute width of a single cell in a row
- * Convenience method for {@link getCellWidth}
+ * Convenience method for {@link #getCellWidth}
*
* @param row the row that contains the cell of interest
* @param column the column number of the cell whose width is to be calculated
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {
str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx);
str.addAttribute(TextAttribute.SIZE, (float)font.getFontHeightInPoints());
- if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
+ if (font.getBold()) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx);
if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx);
if (font.getUnderline() == Font.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx);
}
* @return true if the range contains the cell [rowIx, colIx]
* @deprecated 3.15 beta 2. Use {@link CellRangeAddressBase#isInRange(int, int)}.
*/
+ @Deprecated
public static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) {
return cr.isInRange(rowIx, colIx);
}