Skip to content

Commit d845696

Browse files
author
Olivier Chédru
authored
Merge pull request #124 from dorssar/master
New features for repeating rows and columns in the print setup
2 parents 6052ce3 + 240bb77 commit d845696

File tree

6 files changed

+258
-4
lines changed

6 files changed

+258
-4
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ To freeze the first column on the left and the top three rows:
121121
```java
122122
ws.freezePane(1, 3)
123123
```
124+
Define repeating rows or columns for the print setup.
125+
```java
126+
ws.repeatRows(0, 4) // keep first top 5 rows on each page when printing.
127+
ws.repeatRows(2) // keep the third row on each page when printing.
128+
129+
ws.repeatCols(0, 2) // repeat first three columns (A to C) on each page when printing.
130+
ws.repeatCols(0) // repeat only first column (A) on each page for the print setup.
131+
```
124132
Set header and footer content.\
125133
To set page enumeration in the top right:
126134
```java

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Color.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ public final class Color {
5858
public static final String INDIAN_RED = "CD5C5C";
5959
public static final String ORCHID = "DA70D6";
6060
public static final String RICH_LILAC = "B666D2";
61+
public static final String OPERA_MAUVE = "B98DA8";
62+
public static final String THISTLE = "D0B3C6";
6163
public static final String VIOLET = "EE82EE";
6264
public static final String DARK_VIOLET = "9400D3";
6365
public static final String MEDIUM_VIOLET_RED = "C71585";
6466
public static final String PLUM = "DDA0DD";
6567
public static final String ROSE = "FF0066";
6668
public static final String BRIGHT_LAVENDER = "BF94E4";
69+
public static final String PINK_LAVENDER = "C9A6BC";
6770
public static final String LAVENDER = "967BB6";
6871
public static final String RED = "FF0000";
6972
public static final String CORAL = "FF4040";
@@ -76,6 +79,7 @@ public final class Color {
7679
/*
7780
* All kinds of blue.
7881
*/
82+
public static final String LIGHT_PASTEL_BLUE = "BAD7F2";
7983
public static final String ALICE_BLUE = "F0F8FF";
8084
public static final String AZURE = "007FFF";
8185
public static final String CADET_BLUE = "5F9EA0";
@@ -84,13 +88,15 @@ public final class Color {
8488
public static final String AQUA = "00FFFF";
8589
public static final String AQUAMARINE = "7FFFD4";
8690
public static final String LIGHT_CORNFLOWER_BLUE = "93CCEA";
91+
public static final String PEWTER_BLUE = "99B6C2";
8792
public static final String CORNFLOWER_BLUE = "6495ED";
8893
public static final String LIGHT_TURQUOISE = "AFEEEE";
8994
public static final String TURQUOISE = "40E0dE";
9095
public static final String ROYAL_BLUE = "4169E1";
9196
public static final String DARK_ROYAL_BLUE = "002366";
9297
public static final String NAVY_BLUE = "000080";
9398
public static final String SKY_BLUE = "89CEBB";
99+
public static final String DARK_SKY_BLUE = "A6BFC9";
94100
public static final String SEA_BLUE = "006994";
95101
public static final String PALE_BLUE = "AFEEEE";
96102
public static final String TEAL = "008080";
@@ -106,6 +112,8 @@ public final class Color {
106112
public static final String APPLE_GREEN = "8DB600";
107113
public static final String ANDROID_GREEN = "A4C639";
108114
public static final String SEA_GREEN = "2E8B57";
115+
public static final String LIGHT_LAUREL_GREEN = "C9D8C0";
116+
public static final String LAUREL_GREEN = "A8C19A";
109117
public static final String DARK_GREEN = "006400";
110118
public static final String OLIVE = "808000";
111119
public static final String OLIVE_GREEN = "BAB86C";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 Dhatim.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.dhatim.fastexcel;
17+
18+
/** This class represents a range of columns for the
19+
* repeating columns feature in the print setup. */
20+
class RepeatColRange {
21+
22+
final int from;
23+
final int to;
24+
25+
public RepeatColRange(int from, int to) {
26+
this.from = from;
27+
this.to = to;
28+
}
29+
30+
/**
31+
* Column indexes need to be transformed to the letter form.
32+
*/
33+
public String toString() {
34+
return "$" + Range.colToString(from) + ":$" + Range.colToString(to);
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Dhatim.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.dhatim.fastexcel;
17+
18+
/** This class represents a range of rows for the
19+
* repeating rows feature in the print setup. */
20+
class RepeatRowRange {
21+
22+
final int from;
23+
final int to;
24+
25+
public RepeatRowRange(int from, int to) {
26+
this.from = from;
27+
this.to = to;
28+
}
29+
30+
/**
31+
* Row indexes need to be increased by 1
32+
* (sheet row indexes start from 1 and not from 0)
33+
*/
34+
public String toString() {
35+
return "$" + (1 + from) + ":$" + (1 + to);
36+
}
37+
}

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Workbook.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@
2222
import java.time.LocalDateTime;
2323
import java.time.format.DateTimeFormatter;
2424
import java.util.*;
25+
import java.util.stream.Stream;
2526
import java.util.stream.Collectors;
2627
import java.util.zip.ZipEntry;
2728

29+
2830
/**
2931
* A {@link Workbook} contains one or more {@link Worksheet} objects.
3032
*/
3133
public class Workbook {
3234

35+
private int activeTab = 0;
3336
private final String applicationName;
3437
private final String applicationVersion;
3538
private final List<Worksheet> worksheets = new ArrayList<>();
@@ -79,6 +82,10 @@ public void setCompressionLevel(int level) {
7982
this.os.setLevel(level);
8083
}
8184

85+
public void setActiveTab(int tabIndex) {
86+
this.activeTab = tabIndex;
87+
}
88+
8289
/**
8390
* Sort the current worksheets with the given Comparator
8491
*
@@ -184,14 +191,36 @@ private void writeWorkbookFile() throws IOException {
184191
"xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\">" +
185192
"<workbookPr date1904=\"false\"/>" +
186193
"<bookViews>" +
187-
"<workbookView activeTab=\"0\"/>" +
194+
"<workbookView activeTab=\"" + activeTab + "\"/>" +
188195
"</bookViews>" +
189196
"<sheets>");
190197

191198
for (Worksheet ws : worksheets) {
192199
writeWorkbookSheet(w, ws);
193200
}
194-
w.append("</sheets></workbook>");
201+
w.append("</sheets>");
202+
/** Defining repeating rows and columns for the print setup...
203+
* This is defined for each sheet separately
204+
* (if there are any repeating rows or cols in the sheet at all) **/
205+
206+
for (Worksheet ws : worksheets) {
207+
String defineName = Stream.of(ws.getRepeatingCols(),ws.getRepeatingRows())
208+
.filter(Objects::nonNull)
209+
.map(r -> ws.getName() + "!" + r.toString())
210+
.collect(Collectors.joining(","));
211+
if (!defineName.isEmpty()) {
212+
w.append("<definedNames>");
213+
w.append("<definedName function=\"false\" " +
214+
"hidden=\"false\" " +
215+
"localSheetId=\"0\" " +
216+
"name=\"_xlnm.Print_Titles\" " +
217+
"vbProcedure=\"false\">");
218+
w.append(defineName);
219+
w.append("</definedName>");
220+
w.append("</definedNames>");
221+
}
222+
}
223+
w.append("</workbook>");
195224
});
196225
}
197226

0 commit comments

Comments
 (0)