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

hazendaz / displaytag / 1753

12 Feb 2026 03:17AM UTC coverage: 77.321% (-0.01%) from 77.334%
1753

push

github

web-flow
Merge pull request #1102 from hazendaz/renovate/javax-support-logback-monorepo

Update dependency ch.qos.logback:logback-classic to v1.5.29 (javax-support)

1438 of 2003 branches covered (71.79%)

Branch coverage included in aggregate %.

4034 of 5074 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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2002-2026 Fabrizio Giustina, the Displaytag team
6
 */
7
package org.displaytag.export.excel;
8

9
import java.util.HashMap;
10
import java.util.Map;
11

12
import org.apache.commons.lang3.StringUtils;
13
import org.apache.commons.lang3.Strings;
14
import org.apache.commons.text.StringEscapeUtils;
15
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
16
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
17
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
18
import org.apache.poi.ss.usermodel.HorizontalAlignment;
19
import org.displaytag.Messages;
20
import org.displaytag.exception.BaseNestableJspTagException;
21
import org.displaytag.exception.SeverityEnum;
22
import org.displaytag.properties.TableProperties;
23

24
/**
25
 * Convenience methods for the excel export. Contains code extracted from several existing classes.
26
 */
27
public class ExcelUtils {
28

29
    /** The Constant EXCEL_SHEET_NAME. */
30
    public static final String EXCEL_SHEET_NAME = "export.excel.sheetname"; //$NON-NLS-1$
31

32
    /** The Constant EXCEL_FORMAT_INTEGER. */
33
    public static final String EXCEL_FORMAT_INTEGER = "export.excel.format.integer"; //$NON-NLS-1$
34

35
    /** The Constant EXCEL_FORMAT_DATE. */
36
    public static final String EXCEL_FORMAT_DATE = "export.excel.format.date"; //$NON-NLS-1$
37

38
    /** The Constant EXCEL_FORMAT_NUMBER. */
39
    public static final String EXCEL_FORMAT_NUMBER = "export.excel.format.number"; //$NON-NLS-1$
40

41
    /** The Constant EXCEL_WRAPAT. */
42
    public static final String EXCEL_WRAPAT = "export.excel.wraptextlength"; //$NON-NLS-1$
43

44
    /** The cell styles. */
45
    /*
46
     * Available already configured cell styles, as HSSF JavaDoc claims there are limits to cell styles.
47
     */
48
    private final Map<String, HSSFCellStyle> cellStyles = new HashMap<>();
1✔
49

50
    /**
51
     * Max line length for wrapping.
52
     */
53
    private int wrapAt;
54

55
    // public static final Integer
56

57
    /**
58
     * Style constant for looking up cell styles.
59
     */
60
    public static final String STYLE_INTEGER = "integer";
61

62
    /**
63
     * Style constant for looking up cell styles.
64
     */
65
    public static final String STYLE_NUMBER = "number";
66

67
    /**
68
     * Style constant for looking up cell styles.
69
     */
70
    public static final String STYLE_DATE = "date";
71

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

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

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

87
    /** Workbook. */
88
    private HSSFWorkbook wb;
89

90
    /**
91
     * Instantiates a new excel utils.
92
     *
93
     * @param book
94
     *            the book
95
     */
96
    public ExcelUtils(final HSSFWorkbook book) {
1✔
97
        this.wb = book;
1✔
98
    }
1✔
99

100
    /**
101
     * Gets the wb.
102
     *
103
     * @return the wb
104
     */
105
    public HSSFWorkbook getWb() {
106
        return this.wb;
1✔
107
    }
108

109
    /**
110
     * Sets the wb.
111
     *
112
     * @param wb
113
     *            the new wb
114
     */
115
    public void setWb(final HSSFWorkbook wb) {
116
        this.wb = wb;
×
117
    }
×
118

119
    /**
120
     * We cache the styles; they are expensive to construct.
121
     *
122
     * @param properties
123
     *            props for this run
124
     */
125
    public void initCellStyles(final TableProperties properties) {
126
        // Integer
127
        HSSFCellStyle style = this.getNewCellStyle();
1✔
128
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
129
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_INTEGER)));
1✔
130
        this.cellStyles.put(ExcelUtils.STYLE_INTEGER, style);
1✔
131

132
        // NUMBER
133
        style = this.getNewCellStyle();
1✔
134
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
135
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_NUMBER)));
1✔
136
        this.cellStyles.put(ExcelUtils.STYLE_NUMBER, style);
1✔
137

138
        // Date
139
        style = this.getNewCellStyle();
1✔
140
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
141
        style.setDataFormat(HSSFDataFormat.getBuiltinFormat(properties.getProperty(ExcelUtils.EXCEL_FORMAT_DATE)));
1✔
142
        style.setAlignment(HorizontalAlignment.RIGHT);
1✔
143
        this.cellStyles.put(ExcelUtils.STYLE_DATE, style);
1✔
144

145
        // Long text
146
        style = this.getNewCellStyle(); // http://jakarta.apache.org/poi/hssf/quick-guide.html#NewLinesInCells
1✔
147
        style.setWrapText(true);
1✔
148
        this.cellStyles.put(ExcelUtils.STYLE_LONGSTRING, style);
1✔
149

150
        // Regular text
151
        this.cellStyles.put(ExcelUtils.STYLE_STRING, this.getNewCellStyle());
1✔
152

153
        this.wrapAt = Integer.parseInt(properties.getProperty(ExcelUtils.EXCEL_WRAPAT));
1✔
154
    }
1✔
155

156
    /**
157
     * You can add styles too, but they won't be picked up unless you do so in a subclass.
158
     *
159
     * @param key
160
     *            the key
161
     * @param st
162
     *            the st
163
     */
164
    public void addCellStyle(final String key, final HSSFCellStyle st) {
165
        this.cellStyles.put(key, st);
×
166
    }
×
167

168
    /**
169
     * Gets the new cell style.
170
     *
171
     * @return the new cell style
172
     */
173
    public HSSFCellStyle getNewCellStyle() {
174
        return this.getWb() == null ? null : this.getWb().createCellStyle();
1!
175
    }
176

177
    /**
178
     * Gets the style.
179
     *
180
     * @param clz
181
     *            the clz
182
     *
183
     * @return the style
184
     */
185
    public HSSFCellStyle getStyle(final String clz) {
186
        return this.cellStyles.get(clz);
1✔
187
    }
188

189
    /**
190
     * The Enum CellFormatTypes.
191
     */
192
    public enum CellFormatTypes {
×
193

194
        /** The integer. */
195
        INTEGER,
×
196

197
        /** The number. */
198
        NUMBER,
×
199

200
        /** The date. */
201
        DATE
×
202
    }
203

204
    /**
205
     * Wraps IText-generated exceptions.
206
     */
207
    static class ExcelGenerationException extends BaseNestableJspTagException {
208

209
        /**
210
         * Serial ID.
211
         */
212
        private static final long serialVersionUID = 899149338534L;
213

214
        /**
215
         * Instantiate a new PdfGenerationException with a fixed message and the given cause.
216
         *
217
         * @param cause
218
         *            Previous exception
219
         */
220
        public ExcelGenerationException(final Throwable cause) {
221
            super(ExcelHssfView.class, Messages.getString("ExcelView.errorexporting"), cause); //$NON-NLS-1$
×
222
        }
×
223

224
        /**
225
         * Gets the severity.
226
         *
227
         * @return the severity
228
         *
229
         * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
230
         */
231
        @Override
232
        public SeverityEnum getSeverity() {
233
            return SeverityEnum.ERROR;
×
234
        }
235
    }
236

237
    /**
238
     * Set the cell to wrap if the text is this long.
239
     *
240
     * @return the max length for not wrapping
241
     */
242
    public int getWrapAtLength() {
243
        return this.wrapAt;
1✔
244

245
    }
246

247
    // patch from Karsten Voges
248
    /**
249
     * Escape certain values that are not permitted in excel cells.
250
     *
251
     * @param rawValue
252
     *            the object value
253
     *
254
     * @return the escaped value
255
     */
256
    public static String escapeColumnValue(final Object rawValue) {
257
        if (rawValue == null) {
1!
258
            return null;
×
259
        }
260
        String returnString = rawValue.toString();
1✔
261
        // escape the String to get the tabs, returns, newline explicit as \t \r \n
262
        returnString = StringEscapeUtils.escapeJava(StringUtils.trimToEmpty(returnString));
1✔
263
        // remove tabs, insert four whitespaces instead
264
        returnString = Strings.CS.replace(StringUtils.trim(returnString), "\\t", "    ");
1✔
265
        // remove the return, only newline valid in excel
266
        returnString = Strings.CS.replace(StringUtils.trim(returnString), "\\r", " ");
1✔
267
        return StringEscapeUtils.unescapeJava(returnString);
1✔
268
    }
269

270
}
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