applied patch from Bugzilla 52314: SheetUtil.getColumnWidth could be more flexible
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1215079 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
79a10931dc
commit
2e67e48e71
@ -34,6 +34,7 @@
|
|||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.8-beta6" date="2012-??-??">
|
<release version="3.8-beta6" date="2012-??-??">
|
||||||
|
<action dev="poi-developers" type="fix">52314 - enhanced SheetUtil.getColumnWidth</action>
|
||||||
</release>
|
</release>
|
||||||
<release version="3.8-beta5" date="2011-12-17">
|
<release version="3.8-beta5" date="2011-12-17">
|
||||||
<action dev="poi-developers" type="fix">52204 - Deprecated XSSFWorkbook(String path) constructor because it does not close underlying .zip file</action>
|
<action dev="poi-developers" type="fix">52204 - Deprecated XSSFWorkbook(String path) constructor because it does not close underlying .zip file</action>
|
||||||
|
@ -75,34 +75,20 @@ public class SheetUtil {
|
|||||||
private static final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
|
private static final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compute width of a column and return the result
|
* Compute width of a single cell
|
||||||
*
|
*
|
||||||
* @param sheet the sheet to calculate
|
* @param cell the cell whose width is to be calculated
|
||||||
* @param column 0-based index of the column
|
* @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
|
* @param useMergedCells whether to use merged cells
|
||||||
* @return the width in pixels
|
* @return the width in pixels
|
||||||
*/
|
*/
|
||||||
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){
|
public static double getCellWidth(Cell cell, int defaultCharWidth, DataFormatter formatter, boolean useMergedCells) {
|
||||||
AttributedString str;
|
|
||||||
TextLayout layout;
|
|
||||||
|
|
||||||
|
Sheet sheet = cell.getSheet();
|
||||||
Workbook wb = sheet.getWorkbook();
|
Workbook wb = sheet.getWorkbook();
|
||||||
DataFormatter formatter = new DataFormatter();
|
Row row = cell.getRow();
|
||||||
Font defaultFont = wb.getFontAt((short) 0);
|
int column = cell.getColumnIndex();
|
||||||
|
|
||||||
str = new AttributedString(String.valueOf(defaultChar));
|
|
||||||
copyAttributes(defaultFont, str, 0, 1);
|
|
||||||
layout = new TextLayout(str.getIterator(), fontRenderContext);
|
|
||||||
int defaultCharWidth = (int)layout.getAdvance();
|
|
||||||
|
|
||||||
double width = -1;
|
|
||||||
rows:
|
|
||||||
for (Row row : sheet) {
|
|
||||||
Cell cell = row.getCell(column);
|
|
||||||
|
|
||||||
if (cell == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int colspan = 1;
|
int colspan = 1;
|
||||||
for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) {
|
for (int i = 0 ; i < sheet.getNumMergedRegions(); i++) {
|
||||||
@ -110,7 +96,7 @@ public class SheetUtil {
|
|||||||
if (containsCell(region, row.getRowNum(), column)) {
|
if (containsCell(region, row.getRowNum(), column)) {
|
||||||
if (!useMergedCells) {
|
if (!useMergedCells) {
|
||||||
// If we're not using merged cells, skip this one and move on to the next.
|
// If we're not using merged cells, skip this one and move on to the next.
|
||||||
continue rows;
|
return -1;
|
||||||
}
|
}
|
||||||
cell = row.getCell(region.getFirstColumn());
|
cell = row.getCell(region.getFirstColumn());
|
||||||
colspan = 1 + region.getLastColumn() - region.getFirstColumn();
|
colspan = 1 + region.getLastColumn() - region.getFirstColumn();
|
||||||
@ -125,6 +111,10 @@ public class SheetUtil {
|
|||||||
|
|
||||||
Font font = wb.getFontAt(style.getFontIndex());
|
Font font = wb.getFontAt(style.getFontIndex());
|
||||||
|
|
||||||
|
AttributedString str;
|
||||||
|
TextLayout layout;
|
||||||
|
|
||||||
|
double width = -1;
|
||||||
if (cellType == Cell.CELL_TYPE_STRING) {
|
if (cellType == Cell.CELL_TYPE_STRING) {
|
||||||
RichTextString rt = cell.getRichStringCellValue();
|
RichTextString rt = cell.getRichStringCellValue();
|
||||||
String[] lines = rt.getString().split("\\n");
|
String[] lines = rt.getString().split("\\n");
|
||||||
@ -192,7 +182,81 @@ public class SheetUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute width of a column and return the result
|
||||||
|
*
|
||||||
|
* @param sheet the sheet to calculate
|
||||||
|
* @param column 0-based index of the column
|
||||||
|
* @param useMergedCells whether to use merged cells
|
||||||
|
* @return the width in pixels
|
||||||
|
*/
|
||||||
|
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells){
|
||||||
|
AttributedString str;
|
||||||
|
TextLayout layout;
|
||||||
|
|
||||||
|
Workbook wb = sheet.getWorkbook();
|
||||||
|
DataFormatter formatter = new DataFormatter();
|
||||||
|
Font defaultFont = wb.getFontAt((short) 0);
|
||||||
|
|
||||||
|
str = new AttributedString(String.valueOf(defaultChar));
|
||||||
|
copyAttributes(defaultFont, str, 0, 1);
|
||||||
|
layout = new TextLayout(str.getIterator(), fontRenderContext);
|
||||||
|
int defaultCharWidth = (int)layout.getAdvance();
|
||||||
|
|
||||||
|
double width = -1;
|
||||||
|
for (Row row : sheet) {
|
||||||
|
Cell cell = row.getCell(column);
|
||||||
|
|
||||||
|
if (cell == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells);
|
||||||
|
width = Math.max(width, cellWidth);
|
||||||
|
}
|
||||||
|
return width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute width of a column based on a subset of the rows and return the result
|
||||||
|
*
|
||||||
|
* @param sheet the sheet to calculate
|
||||||
|
* @param column 0-based index of the column
|
||||||
|
* @param useMergedCells whether to use merged cells
|
||||||
|
* @param firstRow 0-based index of the first row to consider (inclusive)
|
||||||
|
* @param lastRow 0-based index of the last row to consider (inclusive)
|
||||||
|
* @return the width in pixels
|
||||||
|
*/
|
||||||
|
public static double getColumnWidth(Sheet sheet, int column, boolean useMergedCells, int firstRow, int lastRow){
|
||||||
|
AttributedString str;
|
||||||
|
TextLayout layout;
|
||||||
|
|
||||||
|
Workbook wb = sheet.getWorkbook();
|
||||||
|
DataFormatter formatter = new DataFormatter();
|
||||||
|
Font defaultFont = wb.getFontAt((short) 0);
|
||||||
|
|
||||||
|
str = new AttributedString(String.valueOf(defaultChar));
|
||||||
|
copyAttributes(defaultFont, str, 0, 1);
|
||||||
|
layout = new TextLayout(str.getIterator(), fontRenderContext);
|
||||||
|
int defaultCharWidth = (int)layout.getAdvance();
|
||||||
|
|
||||||
|
double width = -1;
|
||||||
|
for (int rowIdx = firstRow; rowIdx <= lastRow; ++rowIdx) {
|
||||||
|
Row row = sheet.getRow(rowIdx);
|
||||||
|
if( row != null ) {
|
||||||
|
|
||||||
|
Cell cell = row.getCell(column);
|
||||||
|
|
||||||
|
if (cell == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
double cellWidth = getCellWidth(cell, defaultCharWidth, formatter, useMergedCells);
|
||||||
|
width = Math.max(width, cellWidth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user