supporting change for bug 57450 from Stefan Thurnherr

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1712217 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2015-11-03 07:07:11 +00:00
parent 5230eaf6e1
commit 3b88a60498

View File

@ -207,31 +207,60 @@ public class SheetUtil {
* @return the width in pixels or -1 if cell is empty * @return the width in pixels or -1 if cell is empty
*/ */
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){ public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
Workbook wb = sheet.getWorkbook();
DataFormatter formatter = new DataFormatter(); DataFormatter formatter = new DataFormatter();
int defaultCharWidth = getDefaultCharWidth(sheet.getWorkbook());
double width = -1;
for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
Row row = sheet.getRow(rowIdx);
if( row != null ) {
double cellWidth = getColumnWidthForRow(row, column, defaultCharWidth, formatter, useMergedCells);
width = Math.max(width, cellWidth);
}
}
return width;
}
/**
* Get default character width
*
* @param wb the workbook to get the default character width from
* @return default character width
*/
private static int getDefaultCharWidth(final Workbook wb) {
Font defaultFont = wb.getFontAt((short) 0); Font defaultFont = wb.getFontAt((short) 0);
AttributedString str = new AttributedString(String.valueOf(defaultChar)); AttributedString str = new AttributedString(String.valueOf(defaultChar));
copyAttributes(defaultFont, str, 0, 1); copyAttributes(defaultFont, str, 0, 1);
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
int defaultCharWidth = (int) layout.getAdvance(); int defaultCharWidth = (int) layout.getAdvance();
return defaultCharWidth;
}
double width = -1; /**
for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) { * Compute width of a single cell in a row
Row row = sheet.getRow(rowIdx); * Convenience method for {@link getCellWidth}
if( row != null ) { *
* @param row the row that contains the cell of interest
* @param column the column number of the cell whose width is to be calculated
* @param defaultCharWidth the width of a single character
* @param formatter formatter used to prepare the text to be measured
* @param useMergedCells whether to use merged cells
* @return the width in pixels or -1 if cell is empty
*/
private static double getColumnWidthForRow(
Row row, int column, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
if( row == null ) {
return -1;
}
Cell cell = row.getCell(column); Cell cell = row.getCell(column);
if (cell == null) { if (cell == null) {
continue; return -1;
} }
double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells); return getCellWidth(cell, defaultCharWidth, formatter, useMergedCells);
width = Math.max(width, cellWidth);
}
}
return width;
} }
/** /**