• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / displaytag / 1558

08 Dec 2025 01:09AM UTC coverage: 77.32% (-0.04%) from 77.358%
1558

push

github

web-flow
Merge pull request #1042 from hazendaz/renovate/javax-support-org.apache.commons-commons-text-1.x

Update dependency org.apache.commons:commons-text to v1.15.0 (javax-support)

1435 of 1999 branches covered (71.79%)

Branch coverage included in aggregate %.

4030 of 5069 relevant lines covered (79.5%)

0.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/displaytag/src/main/java/org/displaytag/export/excel/ExcelUtils.java
1
/*
2
 * Copyright (C) 2002-2025 Fabrizio Giustina, the Displaytag team
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to deal
6
 * in the Software without restriction, including without limitation the rights
7
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
 * copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
 * THE SOFTWARE.
21
 */
22
package org.displaytag.export.excel;
23

24
import java.util.HashMap;
25
import java.util.Map;
26

27
import org.apache.commons.lang3.StringUtils;
28
import org.apache.commons.lang3.Strings;
29
import org.apache.commons.text.StringEscapeUtils;
30
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
31
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
32
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
33
import org.apache.poi.ss.usermodel.HorizontalAlignment;
34
import org.displaytag.Messages;
35
import org.displaytag.exception.BaseNestableJspTagException;
36
import org.displaytag.exception.SeverityEnum;
37
import org.displaytag.properties.TableProperties;
38

39
/**
40
 * Convenience methods for the excel export. Contains code extracted from several existing classes.
41
 */
42
public class ExcelUtils {
43

44
    /** The Constant EXCEL_SHEET_NAME. */
45
    public static final String EXCEL_SHEET_NAME = "export.excel.sheetname"; //$NON-NLS-1$
46

47
    /** The Constant EXCEL_FORMAT_INTEGER. */
48
    public static final String EXCEL_FORMAT_INTEGER = "export.excel.format.integer"; //$NON-NLS-1$
49

50
    /** The Constant EXCEL_FORMAT_DATE. */
51
    public static final String EXCEL_FORMAT_DATE = "export.excel.format.date"; //$NON-NLS-1$
52

53
    /** The Constant EXCEL_FORMAT_NUMBER. */
54
    public static final String EXCEL_FORMAT_NUMBER = "export.excel.format.number"; //$NON-NLS-1$
55

56
    /** The Constant EXCEL_WRAPAT. */
57
    public static final String EXCEL_WRAPAT = "export.excel.wraptextlength"; //$NON-NLS-1$
58

59
    /** The cell styles. */
60
    /*
61
     * Available already configured cell styles, as HSSF JavaDoc claims there are limits to cell styles.
62
     */
63
    private final Map<String, HSSFCellStyle> cellStyles = new HashMap<>();
1✔
64

65
    /**
66
     * Max line length for wrapping.
67
     */
68
    private int wrapAt;
69

70
    // public static final Integer
71

72
    /**
73
     * Style constant for looking up cell styles.
74
     */
75
    public static final String STYLE_INTEGER = "integer";
76

77
    /**
78
     * Style constant for looking up cell styles.
79
     */
80
    public static final String STYLE_NUMBER = "number";
81

82
    /**
83
     * Style constant for looking up cell styles.
84
     */
85
    public static final String STYLE_DATE = "date";
86

87
    /**
88
     * Style constant for looking up cell styles.
89
     */
90
    public static final String STYLE_STRING = "string";
91

92
    /**
93
     * Style constant for looking up cell styles.
94
     */
95
    public static final String STYLE_LONGSTRING = "longstring";
96

97
    /**
98
     * Style constant for looking up cell styles.
99
     */
100
    public static final String STYLE_PCT = "pct";
101

102
    /** Workbook. */
103
    private HSSFWorkbook wb;
104

105
    /**
106
     * Instantiates a new excel utils.
107
     *
108
     * @param book
109
     *            the book
110
     */
111
    public ExcelUtils(final HSSFWorkbook book) {
1✔
112
        this.wb = book;
1✔
113
    }
1✔
114

115
    /**
116
     * Gets the wb.
117
     *
118
     * @return the wb
119
     */
120
    public HSSFWorkbook getWb() {
121
        return this.wb;
1✔
122
    }
123

124
    /**
125
     * Sets the wb.
126
     *
127
     * @param wb
128
     *            the new wb
129
     */
130
    public void setWb(final HSSFWorkbook wb) {
131
        this.wb = wb;
×
132
    }
×
133

134
    /**
135
     * We cache the styles; they are expensive to construct.
136
     *
137
     * @param properties
138
     *            props for this run
139
     */
140
    public void initCellStyles(final TableProperties properties) {
141
        // Integer
142
        HSSFCellStyle style = this.getNewCellStyle();
1✔
143
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
144
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_INTEGER)));
1✔
145
        this.cellStyles.put(ExcelUtils.STYLE_INTEGER, style);
1✔
146

147
        // NUMBER
148
        style = this.getNewCellStyle();
1✔
149
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
150
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_NUMBER)));
1✔
151
        this.cellStyles.put(ExcelUtils.STYLE_NUMBER, style);
1✔
152

153
        // Date
154
        style = this.getNewCellStyle();
1✔
155
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
156
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_DATE)));
1✔
157
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
158
        this.cellStyles.put(ExcelUtils.STYLE_DATE, style);
1✔
159

160
        // Long text
161
        style = this.getNewCellStyle(); // http://jakarta.apache.org/poi/hssf/quick-guide.html#NewLinesInCells
1✔
162
        style.setWrapText(true);
1✔
163
        this.cellStyles.put(ExcelUtils.STYLE_LONGSTRING, style);
1✔
164

165
        // Regular text
166
        this.cellStyles.put(ExcelUtils.STYLE_STRING, this.getNewCellStyle());
1✔
167

168
        this.wrapAt = Integer.parseInt(properties.getProperty(ExcelUtils.EXCEL_WRAPAT));
1✔
169
    }
1✔
170

171
    /**
172
     * You can add styles too, but they won't be picked up unless you do so in a subclass.
173
     *
174
     * @param key
175
     *            the key
176
     * @param st
177
     *            the st
178
     */
179
    public void addCellStyle(final String key, final HSSFCellStyle st) {
180
        this.cellStyles.put(key, st);
×
181
    }
×
182

183
    /**
184
     * Gets the new cell style.
185
     *
186
     * @return the new cell style
187
     */
188
    public HSSFCellStyle getNewCellStyle() {
189
        return this.getWb() == null ? null : this.getWb().createCellStyle();
1!
190
    }
191

192
    /**
193
     * Gets the style.
194
     *
195
     * @param clz
196
     *            the clz
197
     *
198
     * @return the style
199
     */
200
    public HSSFCellStyle getStyle(final String clz) {
201
        return this.cellStyles.get(clz);
1✔
202
    }
203

204
    /**
205
     * The Enum CellFormatTypes.
206
     */
207
    public enum CellFormatTypes {
×
208

209
        /** The integer. */
210
        INTEGER,
×
211

212
        /** The number. */
213
        NUMBER,
×
214

215
        /** The date. */
216
        DATE
×
217
    }
218

219
    /**
220
     * Wraps IText-generated exceptions.
221
     */
222
    static class ExcelGenerationException extends BaseNestableJspTagException {
223

224
        /**
225
         * Serial ID.
226
         */
227
        private static final long serialVersionUID = 899149338534L;
228

229
        /**
230
         * Instantiate a new PdfGenerationException with a fixed message and the given cause.
231
         *
232
         * @param cause
233
         *            Previous exception
234
         */
235
        public ExcelGenerationException(final Throwable cause) {
236
            super(ExcelHssfView.class, Messages.getString("ExcelView.errorexporting"), cause); //$NON-NLS-1$
×
237
        }
×
238

239
        /**
240
         * Gets the severity.
241
         *
242
         * @return the severity
243
         *
244
         * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
245
         */
246
        @Override
247
        public SeverityEnum getSeverity() {
248
            return SeverityEnum.ERROR;
×
249
        }
250
    }
251

252
    /**
253
     * Set the cell to wrap if the text is this long.
254
     *
255
     * @return the max length for not wrapping
256
     */
257
    public int getWrapAtLength() {
258
        return this.wrapAt;
1✔
259

260
    }
261

262
    // patch from Karsten Voges
263
    /**
264
     * Escape certain values that are not permitted in excel cells.
265
     *
266
     * @param rawValue
267
     *            the object value
268
     *
269
     * @return the escaped value
270
     */
271
    public static String escapeColumnValue(final Object rawValue) {
272
        if (rawValue == null) {
1!
273
            return null;
×
274
        }
275
        String returnString = rawValue.toString();
1✔
276
        // escape the String to get the tabs, returns, newline explicit as \t \r \n
277
        returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
1✔
278
        // remove tabs, insert four whitespaces instead
279
        returnString = Strings.CS.replace(StringUtils.trim(returnString), "\\t", "    ");
1✔
280
        // remove the return, only newline valid in excel
281
        returnString = Strings.CS.replace(StringUtils.trim(returnString), "\\r", " ");
1✔
282
        return StringEscapeUtils.unescapeJava(returnString);
1✔
283
    }
284

285
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc