1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.converter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Arrays;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hwpf.converter.AbstractWordUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Internal;
/**
* Common class for {@link ExcelToFoConverter} and {@link ExcelToHtmlConverter}
*
* @see AbstractWordUtils
* @since POI 3.8 beta 5
*/
@Internal
class AbstractExcelUtils {
/*package*/ static final String EMPTY = "";
private static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
private static final int UNIT_OFFSET_LENGTH = 7;
public static String getAlign(HorizontalAlignment alignment) {
switch (alignment) {
case CENTER:
case CENTER_SELECTION:
return "center";
case FILL:
// XXX: shall we support fill?
return "";
case JUSTIFY:
return "justify";
case LEFT:
return "left";
case RIGHT:
return "right";
default:
case GENERAL:
return "";
}
}
public static String getBorderStyle(BorderStyle xlsBorder) {
final String borderStyle;
switch (xlsBorder) {
case NONE:
borderStyle = "none";
break;
case DASH_DOT:
case DASH_DOT_DOT:
case DOTTED:
case HAIR:
case MEDIUM_DASH_DOT:
case MEDIUM_DASH_DOT_DOT:
case SLANTED_DASH_DOT:
borderStyle = "dotted";
break;
case DASHED:
case MEDIUM_DASHED:
borderStyle = "dashed";
break;
case DOUBLE:
borderStyle = "double";
break;
default:
borderStyle = "solid";
break;
}
return borderStyle;
}
public static String getBorderWidth(BorderStyle xlsBorder) {
final String borderWidth;
switch (xlsBorder) {
case MEDIUM_DASH_DOT:
case MEDIUM_DASH_DOT_DOT:
case MEDIUM_DASHED:
borderWidth = "2pt";
break;
case THICK:
borderWidth = "thick";
break;
default:
borderWidth = "thin";
break;
}
return borderWidth;
}
public static String getColor(HSSFColor color) {
StringBuilder stringBuilder = new StringBuilder(7);
stringBuilder.append('#');
for (short s : color.getTriplet()) {
String hex = Integer.toHexString(s);
if (hex.length() == 1) {
stringBuilder.append('0');
}
stringBuilder.append(hex);
}
String result = stringBuilder.toString();
if (result.equals("#ffffff")) {
return "white";
}
if (result.equals("#c0c0c0")) {
return "silver";
}
if (result.equals("#808080")) {
return "gray";
}
if (result.equals("#000000")) {
return "black";
}
return result;
}
/**
* See <a href=
* "http://apache-poi.1045710.n5.nabble.com/Excel-Column-Width-Unit-Converter-pixels-excel-column-width-units-td2301481.html"
* >here</a> for Xio explanation and details
*/
public static int getColumnWidthInPx(int widthUnits) {
int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR)
* UNIT_OFFSET_LENGTH;
int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
pixels += Math.round(offsetWidthUnits
/ ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
return pixels;
}
/**
* @param mergedRanges map of sheet merged ranges built with
* {@link #buildMergedRangesMap(Sheet)}
* @return {@link CellRangeAddress} from map if cell with specified row and
* column numbers contained in found range, {@code null} otherwise
*/
public static CellRangeAddress getMergedRange(
CellRangeAddress[][] mergedRanges, int rowNumber, int columnNumber) {
CellRangeAddress[] mergedRangeRowInfo = rowNumber < mergedRanges.length ? mergedRanges[rowNumber]
: null;
return mergedRangeRowInfo != null
&& columnNumber < mergedRangeRowInfo.length ? mergedRangeRowInfo[columnNumber]
: null;
}
static boolean isEmpty(String str) {
return str == null || str.isEmpty();
}
static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static HSSFWorkbook loadXls(File xlsFile) throws IOException {
try (final InputStream inputStream = Files.newInputStream(xlsFile.toPath())) {
return new HSSFWorkbook(inputStream);
}
}
public static void appendAlign(StringBuilder style, HorizontalAlignment alignment) {
String cssAlign = getAlign(alignment);
if (isEmpty(cssAlign)) {
return;
}
style.append("text-align:");
style.append(cssAlign);
style.append(";");
}
/**
* Creates a map (i.e. two-dimensional array) filled with ranges. Allow fast
* retrieving {@link CellRangeAddress} of any cell, if cell is contained in
* range.
*
* @see #getMergedRange(CellRangeAddress[][], int, int)
*/
public static CellRangeAddress[][] buildMergedRangesMap(Sheet sheet) {
CellRangeAddress[][] mergedRanges = new CellRangeAddress[1][];
for (final CellRangeAddress cellRangeAddress : sheet.getMergedRegions()) {
final int requiredHeight = cellRangeAddress.getLastRow() + 1;
if (mergedRanges.length < requiredHeight) {
mergedRanges = Arrays.copyOf(mergedRanges, requiredHeight, CellRangeAddress[][].class);
}
for (int r = cellRangeAddress.getFirstRow(); r <= cellRangeAddress
.getLastRow(); r++) {
final int requiredWidth = cellRangeAddress.getLastColumn() + 1;
CellRangeAddress[] rowMerged = mergedRanges[r];
if (rowMerged == null) {
rowMerged = new CellRangeAddress[requiredWidth];
mergedRanges[r] = rowMerged;
} else {
final int rowMergedLength = rowMerged.length;
if (rowMergedLength < requiredWidth) {
rowMerged = mergedRanges[r] =
Arrays.copyOf(rowMerged, requiredWidth, CellRangeAddress[].class);
}
}
Arrays.fill(rowMerged, cellRangeAddress.getFirstColumn(),
cellRangeAddress.getLastColumn() + 1, cellRangeAddress);
}
}
return mergedRanges;
}
}
|