Skip to content

Commit 6543713

Browse files
committed
Chagne term 'contents' -> 'body', Write simple test code
1 parent 9573534 commit 6543713

20 files changed

Lines changed: 206 additions & 71 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea/
22
.gradle/
3+
out/

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies {
3636
// Rendered Field Order is same as Dto field order
3737
public class ExcelDto {
3838

39-
// Annotation Case 1. no headerStyle and contentsStyle
39+
// Annotation Case 1. no headerStyle and bodyStyle
4040
@ExcelColumn(headerName = "User Name")
4141
private String name;
4242

@@ -46,24 +46,24 @@ public class ExcelDto {
4646
)
4747
private int age;
4848

49-
// Annotation Case 3. You can also configure contents style
49+
// Annotation Case 3. You can also configure bodyStyle style
5050
@ExcelColumn(headerName = "Happy BirthDay",
51-
contentsStyle = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "CONTENTS")
51+
bodyStyle = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BODY")
5252
)
5353
private LocalDate birthDay;
5454

5555
}
5656
```
5757

58-
If you want to config default style in class, you should use @DefaultHeaderStyle or @DefaultContentsStyle.
58+
If you want to config default style in class, you should use @DefaultHeaderStyle or @DefaultBodyStyle.
5959
This style will be applied to all fields having not field style in this class.
6060

6161
```java
6262
@DefaultHeaderStyle(
6363
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BLUE_HEADER")
6464
)
65-
@DefaultContentsStyle(
66-
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "CONTENTS")
65+
@DefaultBodyStyle(
66+
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BODY")
6767
)
6868
public class ExcelDto {
6969

@@ -192,7 +192,7 @@ public enum CustomCellStyle implements ExcelCellStyle {
192192
public class ExcelDto {
193193

194194
@ExcelColumn(headerName = "Field Header Title",
195-
contentsStyle = @ExcelColumnStyle(excelCellStyleClass = CustomCellStyle.class, enumName = "CUSTOM_HEADER")
195+
bodyStyle = @ExcelColumnStyle(excelCellStyleClass = CustomCellStyle.class, enumName = "CUSTOM_HEADER")
196196
)
197197
private String field1;
198198

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ repositories {
1414
dependencies {
1515
compile 'org.apache.poi:poi:4.1.2'
1616
compile 'org.apache.poi:poi-ooxml:4.1.2'
17+
18+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
19+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
20+
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
1721
}

src/main/java/com/lannstark/DefaultContentsStyle.java renamed to src/main/java/com/lannstark/DefaultBodyStyle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@Target(ElementType.TYPE)
99
@Retention(RetentionPolicy.RUNTIME)
10-
public @interface DefaultContentsStyle {
10+
public @interface DefaultBodyStyle {
1111

1212
ExcelColumnStyle style();
1313

src/main/java/com/lannstark/ExcelColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
String headerName() default "";
1515

1616
ExcelColumnStyle headerStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);
17-
ExcelColumnStyle contentsStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);
17+
ExcelColumnStyle bodyStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);
1818

1919
}

src/main/java/com/lannstark/excel/ExcelFile.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import java.io.IOException;
44
import java.io.OutputStream;
5+
import java.util.List;
56

6-
public interface ExcelFile {
7+
public interface ExcelFile<T> {
78

89
void write(OutputStream stream) throws IOException;
910

11+
void addRows(List<T> data);
12+
1013
}

src/main/java/com/lannstark/excel/SXSSFExcelFile.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
import java.io.IOException;
1616
import java.io.OutputStream;
1717
import java.lang.reflect.Field;
18+
import java.util.Collections;
1819
import java.util.List;
1920

2021
import static com.lannstark.utils.SuperClassReflectionUtils.getField;
2122

22-
public abstract class SXSSFExcelFile<T> implements ExcelFile {
23+
public abstract class SXSSFExcelFile<T> implements ExcelFile<T> {
2324

2425
protected static final SpreadsheetVersion supplyExcelVersion = SpreadsheetVersion.EXCEL2007;
2526

@@ -28,28 +29,37 @@ public abstract class SXSSFExcelFile<T> implements ExcelFile {
2829
protected ExcelRenderResource resource;
2930

3031
/**
31-
* OneSheetExcelFile
32+
*SXSSFExcelFile
33+
* @param type Class type to be rendered
34+
*/
35+
public SXSSFExcelFile(Class<T> type) {
36+
this(Collections.emptyList(), type, new DefaultDataFormatDecider());
37+
}
38+
39+
/**
40+
* SXSSFExcelFile
3241
* @param data List Data to render excel file. data should have at least one @ExcelColumn on fields
3342
* @param type Class type to be rendered
3443
*/
3544
public SXSSFExcelFile(List<T> data, Class<T> type) {
36-
this.wb = new SXSSFWorkbook();
37-
this.resource = ExcelRenderResourceFactory.prepareRenderResource(type, wb, new DefaultDataFormatDecider());
38-
renderExcel(data);
45+
this(data, type, new DefaultDataFormatDecider());
3946
}
4047

4148
/**
42-
* OneSheetExcelFile
49+
* SXSSFExcelFile
4350
* @param data List Data to render excel file. data should have at least one @ExcelColumn on fields
4451
* @param type Class type to be rendered
4552
* @param dataFormatDecider Custom DataFormatDecider
4653
*/
4754
public SXSSFExcelFile(List<T> data, Class<T> type, DataFormatDecider dataFormatDecider) {
55+
validateData(data);
4856
this.wb = new SXSSFWorkbook();
4957
this.resource = ExcelRenderResourceFactory.prepareRenderResource(type, wb, dataFormatDecider);
5058
renderExcel(data);
5159
}
5260

61+
protected void validateData(List<T> data) { }
62+
5363
protected abstract void renderExcel(List<T> data);
5464

5565
protected void renderHeadersWithNewSheet(Sheet sheet, int rowIndex, int columnStartIndex) {
@@ -62,15 +72,15 @@ protected void renderHeadersWithNewSheet(Sheet sheet, int rowIndex, int columnSt
6272
}
6373
}
6474

65-
protected void renderContent(Object data, int rowIndex, int columnStartIndex) {
75+
protected void renderBody(Object data, int rowIndex, int columnStartIndex) {
6676
Row row = sheet.createRow(rowIndex);
6777
int columnIndex = columnStartIndex;
6878
for (String dataFieldName : resource.getDataFieldNames()) {
6979
Cell cell = row.createCell(columnIndex++);
7080
try {
7181
Field field = getField(data.getClass(), (dataFieldName));
7282
field.setAccessible(true);
73-
cell.setCellStyle(resource.getCellStyle(dataFieldName, ExcelRenderLocation.CONTENTS));
83+
cell.setCellStyle(resource.getCellStyle(dataFieldName, ExcelRenderLocation.BODY));
7484
Object cellValue = field.get(data);
7585
renderCellValue(cell, cellValue);
7686
} catch (Exception e) {

src/main/java/com/lannstark/excel/multiplesheet/MultiSheetExcelFile.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
* - support Excel Version over 2007
1313
* - support multi sheet rendering
1414
* - support Dffierent DataFormat by Class Type
15-
* - support Custom CellStyle according to (header or contents) and data field
15+
* - support Custom CellStyle according to (header or body) and data field
1616
*/
1717
public class MultiSheetExcelFile<T> extends SXSSFExcelFile<T> {
1818

1919
private static final int maxRowCanBeRendered = supplyExcelVersion.getMaxRows() - 1;
2020
private static final int ROW_START_INDEX = 0;
2121
private static final int COLUMN_START_INDEX = 0;
22+
private int currentRowIndex = ROW_START_INDEX;
23+
24+
public MultiSheetExcelFile(Class<T> type) {
25+
super(type);
26+
wb.setZip64Mode(Zip64Mode.Always);
27+
}
2228

2329
/*
2430
* If you use SXSSF with hug data, you need to set zip mode
@@ -42,16 +48,17 @@ protected void renderExcel(List<T> data) {
4248
return ;
4349
}
4450

51+
// 2. Render body
4552
createNewSheetWithHeader();
46-
int renderedDataCnt = 0;
47-
int rowIndex = ROW_START_INDEX + 1;
48-
for (Object renderedData : data) {
49-
renderContent(renderedData, rowIndex++, COLUMN_START_INDEX);
50-
renderedDataCnt ++;
53+
addRows(data);
54+
}
5155

52-
if (renderedDataCnt == maxRowCanBeRendered) {
53-
renderedDataCnt = 0;
54-
rowIndex = 1;
56+
@Override
57+
public void addRows(List<T> data) {
58+
for (Object renderedData : data) {
59+
renderBody(renderedData, currentRowIndex++, COLUMN_START_INDEX);
60+
if (currentRowIndex == maxRowCanBeRendered) {
61+
currentRowIndex = 1;
5562
createNewSheetWithHeader();
5663
}
5764
}
@@ -60,6 +67,7 @@ protected void renderExcel(List<T> data) {
6067
private void createNewSheetWithHeader() {
6168
sheet = wb.createSheet();
6269
renderHeadersWithNewSheet(sheet, ROW_START_INDEX, COLUMN_START_INDEX);
70+
currentRowIndex++;
6371
}
6472

6573
}

src/main/java/com/lannstark/excel/onesheet/OneSheetExcelFile.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,29 @@
1010
*
1111
* - support Excel Version over 2007
1212
* - support one sheet rendering
13-
* - support Dffierent DataFormat by Class Type
14-
* - support Custom CellStyle according to (header or contents) and data field
13+
* - support different DataFormat by Class Type
14+
* - support Custom CellStyle according to (header or body) and data field
1515
*/
1616
public final class OneSheetExcelFile<T> extends SXSSFExcelFile<T> {
1717

1818
private static final int ROW_START_INDEX = 0;
1919
private static final int COLUMN_START_INDEX = 0;
20+
private int currentRowIndex = ROW_START_INDEX;
21+
22+
public OneSheetExcelFile(Class<T> type) {
23+
super(type);
24+
}
2025

2126
public OneSheetExcelFile(List<T> data, Class<T> type) {
2227
super(data, type);
23-
validateMaxRow(data);
2428
}
2529

2630
public OneSheetExcelFile(List<T> data, Class<T> type, DataFormatDecider dataFormatDecider) {
2731
super(data, type, dataFormatDecider);
28-
validateMaxRow(data);
2932
}
3033

31-
private void validateMaxRow(List<?> data) {
34+
@Override
35+
protected void validateData(List<T> data) {
3236
int maxRows = supplyExcelVersion.getMaxRows();
3337
if (data.size() > maxRows) {
3438
throw new IllegalArgumentException(
@@ -40,17 +44,21 @@ private void validateMaxRow(List<?> data) {
4044
public void renderExcel(List<T> data) {
4145
// 1. Create sheet and renderHeader
4246
sheet = wb.createSheet();
43-
renderHeadersWithNewSheet(sheet, ROW_START_INDEX, COLUMN_START_INDEX);
47+
renderHeadersWithNewSheet(sheet, currentRowIndex++, COLUMN_START_INDEX);
4448

4549
if (data.isEmpty()) {
4650
return;
4751
}
4852

49-
// 2. Render Contents
50-
int rowIndex = ROW_START_INDEX + 1;
53+
// 2. Render Body
5154
for (Object renderedData : data) {
52-
renderContent(renderedData, rowIndex++, COLUMN_START_INDEX);
55+
renderBody(renderedData, currentRowIndex++, COLUMN_START_INDEX);
5356
}
5457
}
5558

59+
@Override
60+
public void addRows(List<T> data) {
61+
renderBody(data, currentRowIndex++, COLUMN_START_INDEX);
62+
}
63+
5664
}

src/main/java/com/lannstark/resource/DefaultDataFormatDecider.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,37 @@
88
public class DefaultDataFormatDecider implements DataFormatDecider {
99

1010
private static final String CURRENT_FORMAT = "#,##0";
11+
private static final String FLOAT_FORMAT_2_DECIMAL_PLACES = "#,##0.00";
1112
private static final String DEFAULT_FORMAT = "";
1213

1314
@Override
1415
public short getDataFormat(DataFormat dataFormat, Class<?> type) {
15-
if (isNumericType(type)) {
16+
if (isFloatType(type)) {
17+
return dataFormat.getFormat(FLOAT_FORMAT_2_DECIMAL_PLACES);
18+
}
19+
20+
if (isIntegerType(type)) {
1621
return dataFormat.getFormat(CURRENT_FORMAT);
1722
}
1823
return dataFormat.getFormat(DEFAULT_FORMAT);
1924
}
2025

21-
private boolean isNumericType(Class<?> type) {
22-
List<Class<?>> numericTypes = Arrays.asList(
26+
private boolean isFloatType(Class<?> type) {
27+
List<Class<?>> floatTypes = Arrays.asList(
28+
Float.class, float.class,
29+
Double.class, double.class
30+
);
31+
return floatTypes.contains(type);
32+
}
33+
34+
private boolean isIntegerType(Class<?> type) {
35+
List<Class<?>> integerTypes = Arrays.asList(
2336
Byte.class, byte.class,
2437
Short.class, short.class,
2538
Integer.class, int.class,
26-
Long.class, long.class,
27-
Float.class, float.class,
28-
Double.class, double.class
39+
Long.class, long.class
2940
);
30-
return numericTypes.contains(type);
41+
return integerTypes.contains(type);
3142
}
3243

3344
}

0 commit comments

Comments
 (0)