From 9332cdcc18dc0bc62f773bd49b46ea2d82bc35ab Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 7 Nov 2013 22:28:06 +0000 Subject: [PATCH] Start on HSSF/XSSF Shrink To Fit support, see bug #55661 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1539848 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFCellStyle.java | 16 +++++++++++++++- .../org/apache/poi/ss/usermodel/CellStyle.java | 18 +++++++++++++++--- .../poi/xssf/usermodel/XSSFCellStyle.java | 12 +++++++++++- .../extensions/XSSFCellAlignment.java | 17 ++++++++++++----- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 7c401a106c..07de7baf0f 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -18,7 +18,6 @@ package org.apache.poi.hssf.usermodel; -import java.util.Arrays; import java.util.List; import org.apache.poi.hssf.model.InternalWorkbook; @@ -807,6 +806,21 @@ public final class HSSFCellStyle implements CellStyle { sr.setName(styleName); } + /** + * Controls if the Cell should be auto-sized + * to shrink to fit if the text is too long + */ + public void setShrinkToFit(boolean shrinkToFit) { + _format.setShrinkToFit(shrinkToFit); + } + /** + * Should the Cell be auto-sized by Excel to shrink + * it to fit if this text is too long? + */ + public boolean getShrinkToFit() { + return _format.getShrinkToFit(); + } + /** * Verifies that this style belongs to the supplied Workbook. * Will throw an exception if it belongs to a different one. diff --git a/src/java/org/apache/poi/ss/usermodel/CellStyle.java b/src/java/org/apache/poi/ss/usermodel/CellStyle.java index 3eed0604bd..3e22bab10b 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/src/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -113,7 +113,7 @@ public interface CellStyle { * dot border */ - public final static short BORDER_HAIR = 0x7; + public final static short BORDER_HAIR = 0x7; /** * Thick border @@ -131,7 +131,7 @@ public interface CellStyle { * hair-line border */ - public final static short BORDER_DOTTED = 0x4; + public final static short BORDER_DOTTED = 0x4; /** * Medium dashed border @@ -701,4 +701,16 @@ public interface CellStyle { * XSSFCellStyle) */ public void cloneStyleFrom(CellStyle source); -} + + /** + * Controls if the Cell should be auto-sized + * to shrink to fit if the text is too long + */ + public void setShrinkToFit(boolean shrinkToFit); + + /** + * Should the Cell be auto-sized by Excel to shrink + * it to fit if this text is too long? + */ + public boolean getShrinkToFit(); +} \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 6f97d994a0..d1f0759078 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -30,8 +30,8 @@ import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.ThemesTable; import org.apache.poi.xssf.usermodel.extensions.XSSFCellAlignment; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; -import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; +import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr; @@ -668,6 +668,11 @@ public class XSSFCellStyle implements CellStyle { return (short)(align == null ? 0 : align.getTextRotation()); } + public boolean getShrinkToFit() { + CTCellAlignment align = _cellXf.getAlignment(); + return align != null && align.getShrinkToFit(); + } + /** * Get the color to use for the top border * @@ -1390,6 +1395,11 @@ public class XSSFCellStyle implements CellStyle { break; } } + + public void setShrinkToFit(boolean shrinkToFit) { + getCellAlignment().setShrinkToFit(shrinkToFit); + } + private int getFontId() { if (_cellXf.isSetFontId()) { return (int) _cellXf.getFontId(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java index ccc1b30a4f..233616c833 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/extensions/XSSFCellAlignment.java @@ -16,19 +16,18 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel.extensions; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.util.Internal; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STHorizontalAlignment; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignment; /** - * Cell settings avaiable in the Format/Alignment tab + * Cell settings available in the Format/Alignment tab */ public class XSSFCellAlignment { - private CTCellAlignment cellAlignement; /** @@ -158,6 +157,14 @@ public class XSSFCellAlignment { cellAlignement.setWrapText(wrapped); } + public boolean getShrinkToFit() { + return cellAlignement.getShrinkToFit(); + } + + public void setShrinkToFit(boolean shrink) { + cellAlignement.setShrinkToFit(shrink); + } + /** * Access to low-level data */