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

Commit 0c1013b

Browse files
committed
🐛 fixed export columns are not uniform
1 parent 8ba68bd commit 0c1013b

File tree

5 files changed

+92
-40
lines changed

5 files changed

+92
-40
lines changed

src/main/java/io/github/biezhi/excel/plus/utils/ExcelUtils.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,32 @@ public static String getSheetName(Object item) {
3535
return excelSheet.value();
3636
}
3737

38-
public static List<String> getWriteFieldNames(Class<?> type) {
38+
public static List<Pair<Integer, String>> getWriteFieldNames(Class<?> type) {
3939
List<Field> fields = getAndSaveFields(type);
40-
List<Pair<String, Integer>> pairs = new ArrayList<>(fields.size());
40+
List<Pair<Integer, String>> pairs = new ArrayList<>(fields.size());
4141

4242
for (Field field : fields) {
4343
ExcelField excelField = field.getAnnotation(ExcelField.class);
4444
if (null != excelField) {
45-
Pair<String, Integer> pair = new Pair<>();
46-
pair.setK(excelField.columnName());
45+
Pair<Integer, String> pair = new Pair<>();
46+
pair.setV(excelField.columnName());
4747

4848
WriteField writeField = field.getAnnotation(WriteField.class);
4949
if (null != writeField && writeField.order() != Constant.DEFAULT_ORDER) {
50-
pair.setV(writeField.order());
50+
pair.setK(writeField.order());
5151
} else {
5252
if (excelField.order() != Constant.DEFAULT_ORDER) {
53-
pair.setV(excelField.order());
53+
pair.setK(excelField.order());
5454
} else {
5555
System.err.println(String.format("[%s.%s] order config error, %s", type.getName(), field.getName(), TIP_MSG));
5656
}
5757
}
5858
pairs.add(pair);
5959
}
6060
}
61-
pairs.sort(Comparator.comparingInt(Pair::getV));
62-
return pairs.stream().map(Pair::getK).collect(Collectors.toList());
61+
// pairs.sort(Comparator.comparingInt(Pair::getV));
62+
// return pairs.stream().map(Pair::getK).collect(Collectors.toList());
63+
return pairs;
6364
}
6465

6566
public static String getColumnValue(Object item, int order) {

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

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.github.biezhi.excel.plus.enums.ExcelType;
44
import io.github.biezhi.excel.plus.exception.ExcelException;
55
import io.github.biezhi.excel.plus.utils.ExcelUtils;
6+
import io.github.biezhi.excel.plus.utils.Pair;
67
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
78
import org.apache.poi.hssf.util.HSSFColor;
89
import org.apache.poi.ss.usermodel.*;
@@ -13,6 +14,7 @@
1314
import java.util.Collection;
1415
import java.util.Iterator;
1516
import java.util.List;
17+
import java.util.stream.Collectors;
1618

1719
/**
1820
* Exporter interface
@@ -25,10 +27,10 @@ public interface ExcelWriter {
2527
/**
2628
* Default Export method
2729
*
28-
* @param exporter
29-
* @param outputStream
30-
* @param <T>
31-
* @throws ExcelException
30+
* @param exporter Exporter Object
31+
* @param outputStream OutputStream
32+
* @param <T> Java Type
33+
* @throws ExcelException thrown when exporting Excel to an exception
3234
*/
3335
default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws ExcelException {
3436
Collection<T> data = exporter.getData();
@@ -41,14 +43,14 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
4143
Workbook workbook;
4244
CellStyle headerStyle;
4345
CellStyle columnStyle = null;
44-
CellStyle rowStyle = null;
4546

4647
T data0 = data.iterator().next();
4748
// Set Excel header
48-
Iterator<T> iterator = data.iterator();
49-
List<String> fieldNames = ExcelUtils.getWriteFieldNames(data0.getClass());
50-
int cols = fieldNames.size();
51-
int startRow = exporter.startRow();
49+
Iterator<T> iterator = data.iterator();
50+
51+
List<Pair<Integer, String>> writeFieldNames = ExcelUtils.getWriteFieldNames(data0.getClass());
52+
53+
int startRow = exporter.startRow();
5254

5355
if (null != exporter.getTemplatePath()) {
5456
InputStream in = ExcelWriter.class.getClassLoader().getResourceAsStream(exporter.getTemplatePath());
@@ -63,17 +65,18 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
6365
} else {
6466
headerStyle = defaultHeaderStyle(workbook);
6567
}
66-
6768
if (null != exporter.getColumnStyle()) {
6869
columnStyle = exporter.getColumnStyle().apply(workbook);
6970
} else {
7071
columnStyle = defaultColumnStyle(workbook);
7172
}
7273

73-
this.writeRowHead(headerStyle, sheet, fieldNames, cols);
74+
this.writeRowHead(headerStyle, sheet, writeFieldNames);
7475
}
7576

76-
this.writeRows(sheet, columnStyle, rowStyle, iterator, cols, startRow);
77+
List<Integer> columnIndexes = writeFieldNames.stream().map(Pair::getK).collect(Collectors.toList());
78+
79+
this.writeRows(sheet, columnStyle, null, iterator, startRow, columnIndexes);
7780

7881
workbook.write(outputStream);
7982
outputStream.flush();
@@ -83,26 +86,46 @@ default <T> void export(Exporter<T> exporter, OutputStream outputStream) throws
8386
}
8487
}
8588

86-
default void writeRowHead(CellStyle headerStyle, Sheet sheet, List<String> fieldNames, int cols) {
89+
/**
90+
* Write the header row to Sheet.
91+
*
92+
* @param headerStyle header row cell style
93+
* @param sheet work sheet
94+
* @param columnNames column names
95+
*/
96+
default void writeRowHead(CellStyle headerStyle, Sheet sheet, List<Pair<Integer, String>> columnNames) {
8797
Row rowHead = sheet.createRow(0);
88-
for (int col = 0; col < cols; col++) {
89-
Cell cell = rowHead.createCell(col);
98+
columnNames.forEach(pair -> {
99+
Integer colIndex = pair.getK();
100+
String columnName = pair.getV();
101+
Cell cell = rowHead.createCell(colIndex);
90102
if (null != headerStyle) {
91103
cell.setCellStyle(headerStyle);
92104
}
93-
cell.setCellValue(fieldNames.get(col));
94-
sheet.autoSizeColumn(col);
95-
}
105+
cell.setCellValue(columnName);
106+
});
96107
}
97108

98-
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyle, Iterator<T> iterator, int cols, int startRow) {
109+
/**
110+
* Write line data
111+
*
112+
* @param sheet work sheet
113+
* @param columnStyle each column style in the row.
114+
* @param rowStyle row style
115+
* @param iterator row data iterator
116+
* @param startRow from the beginning of the line, the default is 1
117+
* @param <T> Java Type
118+
*/
119+
default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyle, Iterator<T> iterator, int startRow, List<Integer> columnIndexes) {
99120
for (int rowNum = startRow; iterator.hasNext(); rowNum++) {
100121
T item = iterator.next();
101122
Row row = sheet.createRow(rowNum);
102123
if (null != rowStyle) {
103124
row.setRowStyle(rowStyle);
104125
}
105-
for (int col = 0; col < cols; col++) {
126+
Iterator<Integer> colIt = columnIndexes.iterator();
127+
while (colIt.hasNext()) {
128+
int col = colIt.next();
106129
Cell cell = row.createCell(col);
107130
String value = ExcelUtils.getColumnValue(item, col);
108131
if (null != columnStyle) {
@@ -117,8 +140,8 @@ default <T> void writeRows(Sheet sheet, CellStyle columnStyle, CellStyle rowStyl
117140
/**
118141
* The default Excel header style.
119142
*
120-
* @param workbook
121-
* @return
143+
* @param workbook Excel workbook
144+
* @return header row cell style
122145
*/
123146
default CellStyle defaultHeaderStyle(Workbook workbook) {
124147
CellStyle headerStyle = workbook.createCellStyle();
@@ -143,8 +166,8 @@ default CellStyle defaultHeaderStyle(Workbook workbook) {
143166
/**
144167
* The default Excel column style.
145168
*
146-
* @param workbook
147-
* @return
169+
* @param workbook Excel workbook
170+
* @return row column cell style
148171
*/
149172
default CellStyle defaultColumnStyle(Workbook workbook) {
150173
CellStyle cellStyle = workbook.createCellStyle();
@@ -164,9 +187,9 @@ default CellStyle defaultColumnStyle(Workbook workbook) {
164187
/**
165188
* Export excel
166189
*
167-
* @param exporter
168-
* @param <T>
169-
* @throws ExcelException
190+
* @param exporter Exporter Object
191+
* @param <T> Java Type
192+
* @throws ExcelException thrown when exporting Excel to an exception
170193
*/
171194
<T> void export(Exporter<T> exporter) throws ExcelException;
172195

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public void testExport() throws ExcelException {
6969

7070
private List<CardSecret> buildCardSecrets() {
7171
List<CardSecret> cardSecrets = new ArrayList<>();
72-
cardSecrets.add(new CardSecret(1, "vlfdzepjmlz2y43z7er4", new BigDecimal("20")));
73-
cardSecrets.add(new CardSecret(2, "rasefq2rzotsmx526z6g", new BigDecimal("10")));
74-
cardSecrets.add(new CardSecret(2, "2ru44qut6neykb2380wt", new BigDecimal("50")));
75-
cardSecrets.add(new CardSecret(1, "srcb4c9fdqzuykd6q4zl", new BigDecimal("15")));
72+
cardSecrets.add(new CardSecret(1, "vlfdzepjmlz2y43z7er4", new BigDecimal("20"), true));
73+
cardSecrets.add(new CardSecret(2, "rasefq2rzotsmx526z6g", new BigDecimal("10"), true));
74+
cardSecrets.add(new CardSecret(2, "2ru44qut6neykb2380wt", new BigDecimal("50"), false));
75+
cardSecrets.add(new CardSecret(1, "srcb4c9fdqzuykd6q4zl", new BigDecimal("15"), true));
7676
return cardSecrets;
7777
}
7878

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.github.biezhi.excel.plus.converter;
2+
3+
public class UsedTypeConverter implements Converter<Boolean> {
4+
5+
@Override
6+
public String write(Boolean value) {
7+
return value ? "已使用" : "未使用";
8+
}
9+
10+
@Override
11+
public Boolean read(String value) {
12+
return "已使用".equals(value);
13+
}
14+
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.biezhi.excel.plus.annotation.ExcelField;
44
import io.github.biezhi.excel.plus.converter.CardTypeConverter;
5+
import io.github.biezhi.excel.plus.converter.UsedTypeConverter;
56

67
import java.io.Serializable;
78
import java.math.BigDecimal;
@@ -27,14 +28,18 @@ public class CardSecret implements Serializable {
2728
@ExcelField(order = 3, columnName = "过期时间", datePattern = "yyyy年MM月dd日")
2829
private Date expiredDate;
2930

31+
@ExcelField(order = 5, columnName = "使用情况", convertType = UsedTypeConverter.class)
32+
private Boolean used;
33+
3034
public CardSecret() {
3135
}
3236

33-
public CardSecret(Integer cardType, String secret, BigDecimal amount) {
37+
public CardSecret(Integer cardType, String secret, BigDecimal amount, boolean used) {
3438
this.cardType = cardType;
3539
this.secret = secret;
3640
this.amount = amount;
3741
this.expiredDate = new Date();
42+
this.used = used;
3843
}
3944

4045
public Integer getCardType() {
@@ -69,13 +74,22 @@ public void setExpiredDate(Date expiredDate) {
6974
this.expiredDate = expiredDate;
7075
}
7176

77+
public Boolean getUsed() {
78+
return used;
79+
}
80+
81+
public void setUsed(Boolean used) {
82+
this.used = used;
83+
}
84+
7285
@Override
7386
public String toString() {
7487
return "CardSecret{" +
7588
"cardType='" + new CardTypeConverter().write(this.cardType) + '\'' +
7689
", secret='" + secret + '\'' +
7790
", amount=" + amount +
7891
", expiredDate=" + expiredDate +
92+
", used='" + new UsedTypeConverter().write(this.used) + '\'' +
7993
'}';
8094
}
8195
}

0 commit comments

Comments
 (0)