Browse Source

Bug 50681 - Fixed autosizing columns beyond 255 character limit

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1136330 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_8_BETA4
Yegor Kozlov 13 years ago
parent
commit
64208474aa

+ 1
- 0
src/documentation/content/xdocs/status.xml View File

@@ -34,6 +34,7 @@

<changes>
<release version="3.8-beta4" date="2011-??-??">
<action dev="poi-developers" type="add">50681 - Fixed autosizing columns beyond 255 character limit </action>
<action dev="poi-developers" type="add">51374 - Fixed incorrect setting of lastPrinted OOXML core property </action>
<action dev="poi-developers" type="add">51351 - Word to XSL-FO converter</action>
<action dev="poi-developers" type="add">50458 - Fixed missing shapeId in XSSF drawings </action>

+ 10
- 1
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java View File

@@ -1764,7 +1764,16 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*/
public void autoSizeColumn(int column, boolean useMergedCells) {
double width = SheetUtil.getColumnWidth(this, column, useMergedCells);
if(width != -1) setColumnWidth(column, (int) (256*width));

if (width != -1) {
width *= 256;
int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters
if (width > maxColumnWidth) {
width = maxColumnWidth;
}
setColumnWidth(column, (int)(width));
}

}

/**

+ 8
- 3
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java View File

@@ -381,10 +381,15 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
*/
public void autoSizeColumn(int column, boolean useMergedCells) {
double width = SheetUtil.getColumnWidth(this, column, useMergedCells);
if(width != -1){

if (width != -1) {
width *= 256;
int maxColumnWidth = 255*256; // The maximum column width for an individual cell is 255 characters
if (width > maxColumnWidth) {
width = maxColumnWidth;
}
setColumnWidth(column, (int)(width));
columnHelper.setColBestFit(column, true);
columnHelper.setCustomWidth(column, true);
columnHelper.setColWidth(column, width);
}
}


+ 20
- 0
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java View File

@@ -306,4 +306,24 @@ public abstract class BaseTestBugzillaIssues extends TestCase {
fmla.append(")");
return fmla.toString();
}

public final void testAutoSize_bug506819() {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell0 = row.createCell(0);

String longValue = "www.hostname.com, www.hostname.com, " +
"www.hostname.com, www.hostname.com, www.hostname.com, " +
"www.hostname.com, www.hostname.com, www.hostname.com, " +
"www.hostname.com, www.hostname.com, www.hostname.com, " +
"www.hostname.com, www.hostname.com, www.hostname.com, " +
"www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com";

cell0.setCellValue(longValue);

sheet.autoSizeColumn(0);
assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
}
}

Loading…
Cancel
Save