Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi.xssf.streaming; |
19 |
|
20 |
import java.util.HashMap; |
21 |
import java.util.HashSet; |
22 |
import java.util.Map; |
23 |
import java.util.Set; |
24 |
|
25 |
import org.apache.poi.ss.usermodel.DataFormatter; |
26 |
import org.apache.poi.ss.usermodel.Row; |
27 |
import org.apache.poi.ss.util.SheetUtil; |
28 |
|
29 |
/** |
30 |
* Tracks maximum column width when adding rows to an SXSSF sheet |
31 |
* so the column width can be calculated although not all rows are |
32 |
* held in memory. |
33 |
*/ |
34 |
public class AutosizeColumnTracker { |
35 |
private SXSSFSheet sheet; |
36 |
private int defaultCharWidth = -1; |
37 |
|
38 |
private final Set<Integer> monitoredColumns = new HashSet<Integer>(); |
39 |
private final Map<Integer, Double> maxColumnWidths = new HashMap<Integer, Double>(); |
40 |
private DataFormatter dataFormatter; |
41 |
|
42 |
public AutosizeColumnTracker(SXSSFSheet sheet) { |
43 |
this.sheet = sheet; |
44 |
} |
45 |
|
46 |
public void monitorColumn(int columnIndex) { |
47 |
monitoredColumns.add(columnIndex); |
48 |
} |
49 |
|
50 |
public void trackRow(Row row) { |
51 |
trackRow(row, false); |
52 |
} |
53 |
|
54 |
public void trackRow(Row row, boolean useMergedCells) { |
55 |
|
56 |
if (defaultCharWidth < 0) { |
57 |
defaultCharWidth = SheetUtil.getDefaultCharWidth(sheet.getWorkbook()); |
58 |
} |
59 |
|
60 |
if (dataFormatter == null) { |
61 |
dataFormatter = new DataFormatter(); |
62 |
} |
63 |
|
64 |
for (int monitoredColumn : monitoredColumns) { |
65 |
final double columnWidth = SheetUtil.getColumnWidthForRow(row, monitoredColumn, defaultCharWidth, |
66 |
dataFormatter, useMergedCells); |
67 |
if (!maxColumnWidths.containsKey(monitoredColumn) || columnWidth > maxColumnWidths.get(monitoredColumn)) { |
68 |
maxColumnWidths.put(monitoredColumn, columnWidth); |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
public void applyAutosizeToColumn(int columnIndex) { |
74 |
if (maxColumnWidths.containsKey(columnIndex)) { |
75 |
|
76 |
double maxColumnWidth = maxColumnWidths.get(columnIndex); |
77 |
maxColumnWidth = Math.min(maxColumnWidth, 255); // The maximum column width for an individual cell is 255 characters |
78 |
sheet.setColumnWidth(columnIndex, (int) (maxColumnWidth*256)); |
79 |
} |
80 |
} |
81 |
|
82 |
} |
83 |
native |