Skip to content

Commit 41453f3

Browse files
committed
[github-657] SXSSF: support setting an arbitrary extra width value for column widths
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1920107 13f79535-47bb-0310-9956-ffa450edef68
1 parent f33b2cb commit 41453f3

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/AutoSizeColumnTracker.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
6060
// Using a HashSet instead of a TreeSet because we don't care about order.
6161
private final Set<Integer> untrackedColumns = new HashSet<>();
6262
private boolean trackAllColumns;
63+
// arbitraryExtraWidth is the extra width added to the best-fit column width (since POI 5.3.1)
64+
private double arbitraryExtraWidth = 0.0d;
6365

6466
/**
6567
* Tuple to store the column widths considering and not considering merged cells
@@ -116,7 +118,27 @@ public AutoSizeColumnTracker(final Sheet sheet) {
116118
// If sheet needs to be saved, use a java.lang.ref.WeakReference to avoid garbage collector gridlock.
117119
defaultCharWidth = SheetUtil.getDefaultCharWidthAsFloat(sheet.getWorkbook());
118120
}
119-
121+
122+
/**
123+
* Set the extra width added to the best-fit column width (default 0.0).
124+
*
125+
* @param arbitraryExtraWidth the extra width added to the best-fit column width
126+
* @since 5.3.1
127+
*/
128+
public void setArbitraryExtraWidth(final double arbitraryExtraWidth) {
129+
this.arbitraryExtraWidth = arbitraryExtraWidth;
130+
}
131+
132+
/**
133+
* Get the extra width added to the best-fit column width.
134+
*
135+
* @return the extra width added to the best-fit column width
136+
* @since 5.3.1
137+
*/
138+
public double getArbitraryExtraWidth() {
139+
return arbitraryExtraWidth;
140+
}
141+
120142
/**
121143
* Get the currently tracked columns, naturally ordered.
122144
* Note if all columns are tracked, this will only return the columns that have been explicitly or implicitly tracked,
@@ -369,8 +391,10 @@ private void implicitlyTrackColumnsInRow(Row row) {
369391
* @since 3.14beta1
370392
*/
371393
private void updateColumnWidth(final Cell cell, final ColumnWidthPair pair) {
372-
final double unmergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false);
373-
final double mergedWidth = SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true);
394+
final double unmergedWidth =
395+
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, false) + arbitraryExtraWidth;
396+
final double mergedWidth =
397+
SheetUtil.getCellWidth(cell, defaultCharWidth, dataFormatter, true) + arbitraryExtraWidth;
374398
pair.setMaxColumnWidths(unmergedWidth, mergedWidth);
375399
}
376400
}

poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFSheet.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,6 @@ public void setDefaultRowHeightInPoints(float height) {
379379
_sh.setDefaultRowHeightInPoints(height);
380380
}
381381

382-
383382
/**
384383
* Get VML drawing for this sheet (aka 'legacy' drawing).
385384
*
@@ -1452,6 +1451,33 @@ public void setDefaultColumnStyle(int column, CellStyle style) {
14521451
_sh.setDefaultColumnStyle(column, style);
14531452
}
14541453

1454+
/**
1455+
* Set the extra width added to the best-fit column width (default 0.0).
1456+
*
1457+
* @param arbitraryExtraWidth the extra width added to the best-fit column width
1458+
* @throws IllegalStateException if autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)
1459+
* @since 5.3.1
1460+
*/
1461+
public void setArbitraryExtraWidth(final double arbitraryExtraWidth) {
1462+
if (_autoSizeColumnTracker == null) {
1463+
throw new IllegalStateException("Cannot trackColumnForAutoSizing because autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)");
1464+
}
1465+
_autoSizeColumnTracker.setArbitraryExtraWidth(arbitraryExtraWidth);
1466+
}
1467+
1468+
/**
1469+
* Get the extra width added to the best-fit column width.
1470+
*
1471+
* @return the extra width added to the best-fit column width
1472+
* @throws IllegalStateException if autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)
1473+
* @since 5.3.1
1474+
*/
1475+
public double getArbitraryExtraWidth() {
1476+
if (_autoSizeColumnTracker == null) {
1477+
throw new IllegalStateException("Cannot trackColumnForAutoSizing because autoSizeColumnTracker failed to initialize (possibly due to fonts not being installed in your OS)");
1478+
}
1479+
return _autoSizeColumnTracker.getArbitraryExtraWidth();
1480+
}
14551481

14561482
/**
14571483
* Track a column in the sheet for auto-sizing.

0 commit comments

Comments
 (0)