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

Commit 8ba68bd

Browse files
committed
💩 improved garbage code
1 parent 928a993 commit 8ba68bd

File tree

9 files changed

+71
-190
lines changed

9 files changed

+71
-190
lines changed

docs/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@ excelPlus.export(data).writeAsResponse(ResponseWrapper.create(servletResponse, "
181181

182182
## 模板导出
183183

184-
<b>该功能尚未实现</b>
185-
186184
有时候我们需要导出的 Excel 表格样式比较复杂,可以事先设置好一个模板表格,数据为空,
187185
由程序向模板中填入数据,然后导出即可,这样就满足了美观的需求。
188186

pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@
5050
<version>3.0.1</version>
5151
<scope>provided</scope>
5252
</dependency>
53-
<dependency>
54-
<groupId>io.github.benas</groupId>
55-
<artifactId>random-beans</artifactId>
56-
<version>3.7.0</version>
57-
<scope>test</scope>
58-
</dependency>
5953
<dependency>
6054
<groupId>junit</groupId>
6155
<artifactId>junit</artifactId>

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

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.apache.poi.ss.usermodel.*;
99
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
1010

11-
import java.io.File;
11+
import java.io.InputStream;
1212
import java.io.OutputStream;
1313
import java.util.Collection;
1414
import java.util.Iterator;
@@ -36,58 +36,45 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
3636
throw new ExcelException("Export excel data is empty.");
3737
}
3838
try {
39+
40+
Sheet sheet;
3941
Workbook workbook;
4042
CellStyle headerStyle;
41-
CellStyle columnStyle;
42-
43-
if (null != exporter.getTemplatePath()) {
44-
workbook = WorkbookFactory.create(new File(exporter.getTemplatePath()));
45-
} else {
46-
workbook = exporter.getExcelType().equals(ExcelType.XLSX) ? new XSSFWorkbook() : new HSSFWorkbook();
47-
}
48-
49-
T data0 = data.iterator().next();
50-
Sheet sheet = workbook.createSheet(ExcelUtils.getSheetName(data0));
51-
52-
Row rowHead = sheet.createRow(0);
43+
CellStyle columnStyle = null;
44+
CellStyle rowStyle = null;
5345

46+
T data0 = data.iterator().next();
5447
// Set Excel header
5548
Iterator<T> iterator = data.iterator();
5649
List<String> fieldNames = ExcelUtils.getWriteFieldNames(data0.getClass());
57-
int rows = data.size();
5850
int cols = fieldNames.size();
51+
int startRow = exporter.startRow();
5952

60-
if (null != exporter.getHeaderStyle()) {
61-
headerStyle = exporter.getHeaderStyle().apply(workbook);
62-
} else {
63-
headerStyle = defaultHeaderStyle(workbook);
64-
}
65-
66-
if (null != exporter.getColumnStyle()) {
67-
columnStyle = exporter.getColumnStyle().apply(workbook);
53+
if (null != exporter.getTemplatePath()) {
54+
InputStream in = ExcelWriter.class.getClassLoader().getResourceAsStream(exporter.getTemplatePath());
55+
workbook = WorkbookFactory.create(in);
56+
sheet = workbook.getSheetAt(0);
6857
} else {
69-
columnStyle = defaultColumnStyle(workbook);
70-
}
58+
workbook = exporter.getExcelType().equals(ExcelType.XLSX) ? new XSSFWorkbook() : new HSSFWorkbook();
59+
sheet = workbook.createSheet(ExcelUtils.getSheetName(data0));
7160

72-
for (int col = 0; col < cols; col++) {
73-
Cell cell = rowHead.createCell(col);
74-
cell.setCellStyle(headerStyle);
75-
cell.setCellValue(fieldNames.get(col));
76-
sheet.autoSizeColumn(col);
77-
}
61+
if (null != exporter.getHeaderStyle()) {
62+
headerStyle = exporter.getHeaderStyle().apply(workbook);
63+
} else {
64+
headerStyle = defaultHeaderStyle(workbook);
65+
}
7866

79-
for (int rowNum = 1; iterator.hasNext() && rowNum <= rows; rowNum++) {
80-
T item = iterator.next();
81-
Row row = sheet.createRow(rowNum);
82-
for (int col = 0; col < cols; col++) {
83-
Cell cell = row.createCell(col);
84-
String value = ExcelUtils.getColumnValue(item, col);
85-
cell.setCellStyle(columnStyle);
86-
cell.setCellValue(value);
87-
sheet.autoSizeColumn(col);
67+
if (null != exporter.getColumnStyle()) {
68+
columnStyle = exporter.getColumnStyle().apply(workbook);
69+
} else {
70+
columnStyle = defaultColumnStyle(workbook);
8871
}
72+
73+
this.writeRowHead(headerStyle, sheet, fieldNames, cols);
8974
}
9075

76+
this.writeRows(sheet, columnStyle, rowStyle, iterator, cols, startRow);
77+
9178
workbook.write(outputStream);
9279
outputStream.flush();
9380
outputStream.close();
@@ -96,6 +83,37 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
9683
}
9784
}
9885

86+
default void writeRowHead(CellStyle headerStyle, Sheet sheet, List<String> fieldNames, int cols) {
87+
Row rowHead = sheet.createRow(0);
88+
for (int col = 0; col < cols; col++) {
89+
Cell cell = rowHead.createCell(col);
90+
if (null != headerStyle) {
91+
cell.setCellStyle(headerStyle);
92+
}
93+
cell.setCellValue(fieldNames.get(col));
94+
sheet.autoSizeColumn(col);
95+
}
96+
}
97+
98+
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyle, Iterator<T> iterator, int cols, int startRow) {
99+
for (int rowNum = startRow; iterator.hasNext(); rowNum++) {
100+
T item = iterator.next();
101+
Row row = sheet.createRow(rowNum);
102+
if (null != rowStyle) {
103+
row.setRowStyle(rowStyle);
104+
}
105+
for (int col = 0; col < cols; col++) {
106+
Cell cell = row.createCell(col);
107+
String value = ExcelUtils.getColumnValue(item, col);
108+
if (null != columnStyle) {
109+
cell.setCellStyle(columnStyle);
110+
}
111+
cell.setCellValue(value);
112+
sheet.autoSizeColumn(col);
113+
}
114+
}
115+
}
116+
99117
/**
100118
* The default Excel header style.
101119
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Exporter<T> {
1818
private String templatePath;
1919
private ExcelType excelType;
2020
private Collection<T> data;
21+
private int startRow = 1;
2122

2223
private Function<Workbook, CellStyle> headerStyle;
2324
private Function<Workbook, CellStyle> columnStyle;
@@ -43,6 +44,15 @@ public Exporter<T> byTemplate(String templatePath) {
4344
return this;
4445
}
4546

47+
public Exporter<T> startRow(int startRow) {
48+
this.startRow = startRow;
49+
return this;
50+
}
51+
52+
public int startRow(){
53+
return this.startRow;
54+
}
55+
4656
public Function<Workbook, CellStyle> getHeaderStyle() {
4757
return headerStyle;
4858
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ public void testExportStyle() throws ExcelException {
9797
@Test
9898
public void testExportByTpl() throws ExcelException {
9999
List<CardSecret> cardSecrets = this.buildCardSecrets();
100-
excelPlus.export(Exporter.create(cardSecrets).byTemplate("")).writeAsFile(new File("template_rows.xls"));
100+
excelPlus.export(
101+
Exporter.create(cardSecrets).byTemplate("tpl.xls").startRow(2)
102+
).writeAsFile(new File("template_rows.xls"));
101103
}
102104

103105
@Test

src/test/java/io/github/biezhi/excel/plus/model/CardSecret.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
public class CardSecret implements Serializable {
1717

18-
@ExcelField(order = 0, columnName = "卡密类型", convertType = CardTypeConverter.class)
18+
@ExcelField(order = 0, columnName = "运营商", convertType = CardTypeConverter.class)
1919
private Integer cardType;
2020

2121
@ExcelField(order = 1, columnName = "卡密")

src/test/java/io/github/biezhi/excel/plus/model/Order.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/test/resources/tpl.xls

8.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)