Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 025a9af

Browse files
committed
🔖 release 0.0.1
1 parent 0c1013b commit 025a9af

File tree

4 files changed

+143
-57
lines changed

4 files changed

+143
-57
lines changed

src/main/java/io/github/biezhi/excel/plus/Constant.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.biezhi.excel.plus;
22

3+
import org.apache.poi.hssf.util.HSSFColor;
4+
import org.apache.poi.ss.usermodel.*;
5+
36
/**
47
* Excel plus constant
58
*
@@ -20,4 +23,75 @@ public interface Constant {
2023
*/
2124
int DEFAULT_ORDER = -1;
2225

26+
default CellStyle defaultTitleStyle(Workbook workbook) {
27+
CellStyle style = workbook.createCellStyle();
28+
29+
style.setAlignment(HorizontalAlignment.CENTER);
30+
style.setVerticalAlignment(VerticalAlignment.CENTER);
31+
32+
style.setBorderTop(BorderStyle.THIN);
33+
style.setBorderRight(BorderStyle.THIN);
34+
style.setBorderBottom(BorderStyle.THIN);
35+
style.setBorderLeft(BorderStyle.THIN);
36+
37+
style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
38+
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
39+
40+
Font font = workbook.createFont();
41+
font.setFontHeightInPoints((short) 18);
42+
font.setBold(true);
43+
font.setFontName("Arial");
44+
style.setFont(font);
45+
return style;
46+
}
47+
48+
/**
49+
* The default Excel header style.
50+
*
51+
* @param workbook Excel workbook
52+
* @return header row cell style
53+
*/
54+
default CellStyle defaultHeaderStyle(Workbook workbook) {
55+
CellStyle headerStyle = workbook.createCellStyle();
56+
57+
headerStyle.setAlignment(HorizontalAlignment.CENTER);
58+
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
59+
headerStyle.setBorderTop(BorderStyle.THIN);
60+
headerStyle.setBorderRight(BorderStyle.THIN);
61+
headerStyle.setBorderBottom(BorderStyle.THIN);
62+
headerStyle.setBorderLeft(BorderStyle.THIN);
63+
64+
headerStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
65+
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
66+
67+
Font font = workbook.createFont();
68+
font.setFontHeightInPoints((short) 12);
69+
font.setBold(true);
70+
font.setFontName("Arial");
71+
headerStyle.setFont(font);
72+
return headerStyle;
73+
}
74+
75+
/**
76+
* The default Excel column style.
77+
*
78+
* @param workbook Excel workbook
79+
* @return row column cell style
80+
*/
81+
default CellStyle defaultColumnStyle(Workbook workbook) {
82+
CellStyle cellStyle = workbook.createCellStyle();
83+
cellStyle.setAlignment(HorizontalAlignment.CENTER);
84+
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
85+
cellStyle.setBorderTop(BorderStyle.THIN);
86+
cellStyle.setBorderRight(BorderStyle.THIN);
87+
cellStyle.setBorderBottom(BorderStyle.THIN);
88+
cellStyle.setBorderLeft(BorderStyle.THIN);
89+
cellStyle.setWrapText(true);
90+
91+
Font font = workbook.createFont();
92+
font.setFontName("Arial");
93+
cellStyle.setFont(font);
94+
return cellStyle;
95+
}
96+
2397
}

src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package io.github.biezhi.excel.plus.writer;
22

3+
import io.github.biezhi.excel.plus.Constant;
34
import io.github.biezhi.excel.plus.enums.ExcelType;
45
import io.github.biezhi.excel.plus.exception.ExcelException;
56
import io.github.biezhi.excel.plus.utils.ExcelUtils;
67
import io.github.biezhi.excel.plus.utils.Pair;
78
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
8-
import org.apache.poi.hssf.util.HSSFColor;
99
import org.apache.poi.ss.usermodel.*;
10+
import org.apache.poi.ss.util.CellRangeAddress;
1011
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1112

1213
import java.io.InputStream;
1314
import java.io.OutputStream;
1415
import java.util.Collection;
16+
import java.util.Comparator;
1517
import java.util.Iterator;
1618
import java.util.List;
1719
import java.util.stream.Collectors;
@@ -22,7 +24,7 @@
2224
* @author biezhi
2325
* @date 2018/2/4
2426
*/
25-
public interface ExcelWriter {
27+
public interface ExcelWriter extends Constant {
2628

2729
/**
2830
* Default Export method
@@ -43,38 +45,56 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
4345
Workbook workbook;
4446
CellStyle headerStyle;
4547
CellStyle columnStyle = null;
48+
CellStyle titleStyle = null;
4649

4750
T data0 = data.iterator().next();
4851
// Set Excel header
4952
Iterator<T> iterator = data.iterator();
5053

5154
List<Pair<Integer, String>> writeFieldNames = ExcelUtils.getWriteFieldNames(data0.getClass());
5255

56+
List<Integer> columnIndexes = writeFieldNames.stream().map(Pair::getK).collect(Collectors.toList());
57+
5358
int startRow = exporter.startRow();
5459

5560
if (null != exporter.getTemplatePath()) {
5661
InputStream in = ExcelWriter.class.getClassLoader().getResourceAsStream(exporter.getTemplatePath());
5762
workbook = WorkbookFactory.create(in);
5863
sheet = workbook.getSheetAt(0);
64+
5965
} else {
6066
workbook = exporter.getExcelType().equals(ExcelType.XLSX) ? new XSSFWorkbook() : new HSSFWorkbook();
6167
sheet = workbook.createSheet(ExcelUtils.getSheetName(data0));
6268

69+
if (null != exporter.getTitleStyle()) {
70+
titleStyle = exporter.getTitleStyle().apply(workbook);
71+
} else {
72+
titleStyle = this.defaultTitleStyle(workbook);
73+
}
74+
6375
if (null != exporter.getHeaderStyle()) {
6476
headerStyle = exporter.getHeaderStyle().apply(workbook);
6577
} else {
66-
headerStyle = defaultHeaderStyle(workbook);
78+
headerStyle = this.defaultHeaderStyle(workbook);
6779
}
80+
6881
if (null != exporter.getColumnStyle()) {
6982
columnStyle = exporter.getColumnStyle().apply(workbook);
7083
} else {
71-
columnStyle = defaultColumnStyle(workbook);
84+
columnStyle = this.defaultColumnStyle(workbook);
7285
}
7386

74-
this.writeRowHead(headerStyle, sheet, writeFieldNames);
75-
}
87+
String headerTitle = exporter.getHeaderTitle();
88+
int colIndex = 0;
89+
if (null != headerTitle) {
90+
colIndex = 1;
91+
int maxColIndex = columnIndexes.stream().max(Comparator.comparingInt(Integer::intValue)).get();
92+
this.writeTitleRow(titleStyle, sheet, headerTitle, maxColIndex);
93+
}
94+
this.writeColumnNames(colIndex, headerStyle, sheet, writeFieldNames);
95+
startRow += colIndex;
7696

77-
List<Integer> columnIndexes = writeFieldNames.stream().map(Pair::getK).collect(Collectors.toList());
97+
}
7898

7999
this.writeRows(sheet, columnStyle, null, iterator, startRow, columnIndexes);
80100

@@ -86,15 +106,28 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
86106
}
87107
}
88108

109+
default void writeTitleRow(CellStyle cellStyle, Sheet sheet, String title, int maxColIndex) {
110+
Row titleRow = sheet.createRow(0);
111+
for (int i = 0; i <= maxColIndex; i++) {
112+
Cell cell = titleRow.createCell(i);
113+
if (i == 0) {
114+
cell.setCellValue(title);
115+
}
116+
cell.setCellStyle(cellStyle);
117+
}
118+
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, maxColIndex));
119+
}
120+
89121
/**
90122
* Write the header row to Sheet.
91123
*
124+
* @param rowIndex start row index
92125
* @param headerStyle header row cell style
93126
* @param sheet work sheet
94127
* @param columnNames column names
95128
*/
96-
default void writeRowHead(CellStyle headerStyle, Sheet sheet, List<Pair<Integer, String>> columnNames) {
97-
Row rowHead = sheet.createRow(0);
129+
default void writeColumnNames(int rowIndex, CellStyle headerStyle, Sheet sheet, List<Pair<Integer, String>> columnNames) {
130+
Row rowHead = sheet.createRow(rowIndex);
98131
columnNames.forEach(pair -> {
99132
Integer colIndex = pair.getK();
100133
String columnName = pair.getV();
@@ -137,53 +170,6 @@ default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyl
137170
}
138171
}
139172

140-
/**
141-
* The default Excel header style.
142-
*
143-
* @param workbook Excel workbook
144-
* @return header row cell style
145-
*/
146-
default CellStyle defaultHeaderStyle(Workbook workbook) {
147-
CellStyle headerStyle = workbook.createCellStyle();
148-
149-
headerStyle.setAlignment(HorizontalAlignment.CENTER);
150-
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
151-
headerStyle.setBorderTop(BorderStyle.THIN);
152-
headerStyle.setBorderRight(BorderStyle.THIN);
153-
headerStyle.setBorderBottom(BorderStyle.THIN);
154-
headerStyle.setBorderLeft(BorderStyle.THIN);
155-
156-
headerStyle.setFillForegroundColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
157-
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
158-
159-
Font headerFont = workbook.createFont();
160-
headerFont.setFontHeightInPoints((short) 12);
161-
headerFont.setBold(true);
162-
headerStyle.setFont(headerFont);
163-
return headerStyle;
164-
}
165-
166-
/**
167-
* The default Excel column style.
168-
*
169-
* @param workbook Excel workbook
170-
* @return row column cell style
171-
*/
172-
default CellStyle defaultColumnStyle(Workbook workbook) {
173-
CellStyle cellStyle = workbook.createCellStyle();
174-
cellStyle.setAlignment(HorizontalAlignment.CENTER);
175-
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
176-
cellStyle.setBorderTop(BorderStyle.THIN);
177-
cellStyle.setBorderRight(BorderStyle.THIN);
178-
cellStyle.setBorderBottom(BorderStyle.THIN);
179-
cellStyle.setBorderLeft(BorderStyle.THIN);
180-
cellStyle.setWrapText(true);
181-
182-
Font cellFont = workbook.createFont();
183-
cellStyle.setFont(cellFont);
184-
return cellStyle;
185-
}
186-
187173
/**
188174
* Export excel
189175
*

src/main/java/io/github/biezhi/excel/plus/writer/Exporter.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
public class Exporter<T> {
1717

18+
private String headerTitle;
1819
private String templatePath;
1920
private ExcelType excelType;
2021
private Collection<T> data;
2122
private int startRow = 1;
2223

24+
private Function<Workbook, CellStyle> titleStyle;
2325
private Function<Workbook, CellStyle> headerStyle;
2426
private Function<Workbook, CellStyle> columnStyle;
2527

@@ -29,6 +31,16 @@ public static <T> Exporter<T> create(Collection<T> data) {
2931
return exporter;
3032
}
3133

34+
public Exporter<T> title(String title) {
35+
this.headerTitle = title;
36+
return this;
37+
}
38+
39+
public Exporter<T> titleStyle(Function<Workbook, CellStyle> function) {
40+
this.titleStyle = function;
41+
return this;
42+
}
43+
3244
public Exporter<T> headerStyle(Function<Workbook, CellStyle> function) {
3345
this.headerStyle = function;
3446
return this;
@@ -49,10 +61,14 @@ public Exporter<T> startRow(int startRow) {
4961
return this;
5062
}
5163

52-
public int startRow(){
64+
public int startRow() {
5365
return this.startRow;
5466
}
5567

68+
public Function<Workbook, CellStyle> getTitleStyle() {
69+
return titleStyle;
70+
}
71+
5672
public Function<Workbook, CellStyle> getHeaderStyle() {
5773
return headerStyle;
5874
}
@@ -69,6 +85,10 @@ public Collection<T> getData() {
6985
return data;
7086
}
7187

88+
public String getHeaderTitle() {
89+
return headerTitle;
90+
}
91+
7292
public void setExcelType(ExcelType excelType) {
7393
this.excelType = excelType;
7494
}

src/test/java/io/github/biezhi/excel/plus/Examples.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public void testExport() throws ExcelException {
6767
excelPlus.export(cardSecrets).writeAsFile(new File("卡密列表.xls"));
6868
}
6969

70+
@Test
71+
public void testExportTitle() throws ExcelException {
72+
List<CardSecret> cardSecrets = this.buildCardSecrets();
73+
excelPlus.export(Exporter.create(cardSecrets).title("卡密列表第一季数据")).writeAsFile(new File("卡密列表.xls"));
74+
}
75+
7076
private List<CardSecret> buildCardSecrets() {
7177
List<CardSecret> cardSecrets = new ArrayList<>();
7278
cardSecrets.add(new CardSecret(1, "vlfdzepjmlz2y43z7er4", new BigDecimal("20"), true));

0 commit comments

Comments
 (0)