Skip to content

Commit

Permalink
Chagne term 'contents' -> 'body', Write simple test code
Browse files Browse the repository at this point in the history
  • Loading branch information
lannstark committed Sep 16, 2020
1 parent 9573534 commit 6543713
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/
.gradle/
out/
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {
// Rendered Field Order is same as Dto field order
public class ExcelDto {

// Annotation Case 1. no headerStyle and contentsStyle
// Annotation Case 1. no headerStyle and bodyStyle
@ExcelColumn(headerName = "User Name")
private String name;

Expand All @@ -46,24 +46,24 @@ public class ExcelDto {
)
private int age;

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

}
```

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

```java
@DefaultHeaderStyle(
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BLUE_HEADER")
)
@DefaultContentsStyle(
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "CONTENTS")
@DefaultBodyStyle(
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BODY")
)
public class ExcelDto {

Expand Down Expand Up @@ -192,7 +192,7 @@ public enum CustomCellStyle implements ExcelCellStyle {
public class ExcelDto {

@ExcelColumn(headerName = "Field Header Title",
contentsStyle = @ExcelColumnStyle(excelCellStyleClass = CustomCellStyle.class, enumName = "CUSTOM_HEADER")
bodyStyle = @ExcelColumnStyle(excelCellStyleClass = CustomCellStyle.class, enumName = "CUSTOM_HEADER")
)
private String field1;

Expand Down
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ repositories {
dependencies {
compile 'org.apache.poi:poi:4.1.2'
compile 'org.apache.poi:poi-ooxml:4.1.2'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface DefaultContentsStyle {
public @interface DefaultBodyStyle {

ExcelColumnStyle style();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/lannstark/ExcelColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
String headerName() default "";

ExcelColumnStyle headerStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);
ExcelColumnStyle contentsStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);
ExcelColumnStyle bodyStyle() default @ExcelColumnStyle(excelCellStyleClass = NoExcelCellStyle.class);

}
5 changes: 4 additions & 1 deletion src/main/java/com/lannstark/excel/ExcelFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

public interface ExcelFile {
public interface ExcelFile<T> {

void write(OutputStream stream) throws IOException;

void addRows(List<T> data);

}
26 changes: 18 additions & 8 deletions src/main/java/com/lannstark/excel/SXSSFExcelFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

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

public abstract class SXSSFExcelFile<T> implements ExcelFile {
public abstract class SXSSFExcelFile<T> implements ExcelFile<T> {

protected static final SpreadsheetVersion supplyExcelVersion = SpreadsheetVersion.EXCEL2007;

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

/**
* OneSheetExcelFile
*SXSSFExcelFile
* @param type Class type to be rendered
*/
public SXSSFExcelFile(Class<T> type) {
this(Collections.emptyList(), type, new DefaultDataFormatDecider());
}

/**
* SXSSFExcelFile
* @param data List Data to render excel file. data should have at least one @ExcelColumn on fields
* @param type Class type to be rendered
*/
public SXSSFExcelFile(List<T> data, Class<T> type) {
this.wb = new SXSSFWorkbook();
this.resource = ExcelRenderResourceFactory.prepareRenderResource(type, wb, new DefaultDataFormatDecider());
renderExcel(data);
this(data, type, new DefaultDataFormatDecider());
}

/**
* OneSheetExcelFile
* SXSSFExcelFile
* @param data List Data to render excel file. data should have at least one @ExcelColumn on fields
* @param type Class type to be rendered
* @param dataFormatDecider Custom DataFormatDecider
*/
public SXSSFExcelFile(List<T> data, Class<T> type, DataFormatDecider dataFormatDecider) {
validateData(data);
this.wb = new SXSSFWorkbook();
this.resource = ExcelRenderResourceFactory.prepareRenderResource(type, wb, dataFormatDecider);
renderExcel(data);
}

protected void validateData(List<T> data) { }

protected abstract void renderExcel(List<T> data);

protected void renderHeadersWithNewSheet(Sheet sheet, int rowIndex, int columnStartIndex) {
Expand All @@ -62,15 +72,15 @@ protected void renderHeadersWithNewSheet(Sheet sheet, int rowIndex, int columnSt
}
}

protected void renderContent(Object data, int rowIndex, int columnStartIndex) {
protected void renderBody(Object data, int rowIndex, int columnStartIndex) {
Row row = sheet.createRow(rowIndex);
int columnIndex = columnStartIndex;
for (String dataFieldName : resource.getDataFieldNames()) {
Cell cell = row.createCell(columnIndex++);
try {
Field field = getField(data.getClass(), (dataFieldName));
field.setAccessible(true);
cell.setCellStyle(resource.getCellStyle(dataFieldName, ExcelRenderLocation.CONTENTS));
cell.setCellStyle(resource.getCellStyle(dataFieldName, ExcelRenderLocation.BODY));
Object cellValue = field.get(data);
renderCellValue(cell, cellValue);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
* - support Excel Version over 2007
* - support multi sheet rendering
* - support Dffierent DataFormat by Class Type
* - support Custom CellStyle according to (header or contents) and data field
* - support Custom CellStyle according to (header or body) and data field
*/
public class MultiSheetExcelFile<T> extends SXSSFExcelFile<T> {

private static final int maxRowCanBeRendered = supplyExcelVersion.getMaxRows() - 1;
private static final int ROW_START_INDEX = 0;
private static final int COLUMN_START_INDEX = 0;
private int currentRowIndex = ROW_START_INDEX;

public MultiSheetExcelFile(Class<T> type) {
super(type);
wb.setZip64Mode(Zip64Mode.Always);
}

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

// 2. Render body
createNewSheetWithHeader();
int renderedDataCnt = 0;
int rowIndex = ROW_START_INDEX + 1;
for (Object renderedData : data) {
renderContent(renderedData, rowIndex++, COLUMN_START_INDEX);
renderedDataCnt ++;
addRows(data);
}

if (renderedDataCnt == maxRowCanBeRendered) {
renderedDataCnt = 0;
rowIndex = 1;
@Override
public void addRows(List<T> data) {
for (Object renderedData : data) {
renderBody(renderedData, currentRowIndex++, COLUMN_START_INDEX);
if (currentRowIndex == maxRowCanBeRendered) {
currentRowIndex = 1;
createNewSheetWithHeader();
}
}
Expand All @@ -60,6 +67,7 @@ protected void renderExcel(List<T> data) {
private void createNewSheetWithHeader() {
sheet = wb.createSheet();
renderHeadersWithNewSheet(sheet, ROW_START_INDEX, COLUMN_START_INDEX);
currentRowIndex++;
}

}
26 changes: 17 additions & 9 deletions src/main/java/com/lannstark/excel/onesheet/OneSheetExcelFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@
*
* - support Excel Version over 2007
* - support one sheet rendering
* - support Dffierent DataFormat by Class Type
* - support Custom CellStyle according to (header or contents) and data field
* - support different DataFormat by Class Type
* - support Custom CellStyle according to (header or body) and data field
*/
public final class OneSheetExcelFile<T> extends SXSSFExcelFile<T> {

private static final int ROW_START_INDEX = 0;
private static final int COLUMN_START_INDEX = 0;
private int currentRowIndex = ROW_START_INDEX;

public OneSheetExcelFile(Class<T> type) {
super(type);
}

public OneSheetExcelFile(List<T> data, Class<T> type) {
super(data, type);
validateMaxRow(data);
}

public OneSheetExcelFile(List<T> data, Class<T> type, DataFormatDecider dataFormatDecider) {
super(data, type, dataFormatDecider);
validateMaxRow(data);
}

private void validateMaxRow(List<?> data) {
@Override
protected void validateData(List<T> data) {
int maxRows = supplyExcelVersion.getMaxRows();
if (data.size() > maxRows) {
throw new IllegalArgumentException(
Expand All @@ -40,17 +44,21 @@ private void validateMaxRow(List<?> data) {
public void renderExcel(List<T> data) {
// 1. Create sheet and renderHeader
sheet = wb.createSheet();
renderHeadersWithNewSheet(sheet, ROW_START_INDEX, COLUMN_START_INDEX);
renderHeadersWithNewSheet(sheet, currentRowIndex++, COLUMN_START_INDEX);

if (data.isEmpty()) {
return;
}

// 2. Render Contents
int rowIndex = ROW_START_INDEX + 1;
// 2. Render Body
for (Object renderedData : data) {
renderContent(renderedData, rowIndex++, COLUMN_START_INDEX);
renderBody(renderedData, currentRowIndex++, COLUMN_START_INDEX);
}
}

@Override
public void addRows(List<T> data) {
renderBody(data, currentRowIndex++, COLUMN_START_INDEX);
}

}
25 changes: 18 additions & 7 deletions src/main/java/com/lannstark/resource/DefaultDataFormatDecider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,37 @@
public class DefaultDataFormatDecider implements DataFormatDecider {

private static final String CURRENT_FORMAT = "#,##0";
private static final String FLOAT_FORMAT_2_DECIMAL_PLACES = "#,##0.00";
private static final String DEFAULT_FORMAT = "";

@Override
public short getDataFormat(DataFormat dataFormat, Class<?> type) {
if (isNumericType(type)) {
if (isFloatType(type)) {
return dataFormat.getFormat(FLOAT_FORMAT_2_DECIMAL_PLACES);
}

if (isIntegerType(type)) {
return dataFormat.getFormat(CURRENT_FORMAT);
}
return dataFormat.getFormat(DEFAULT_FORMAT);
}

private boolean isNumericType(Class<?> type) {
List<Class<?>> numericTypes = Arrays.asList(
private boolean isFloatType(Class<?> type) {
List<Class<?>> floatTypes = Arrays.asList(
Float.class, float.class,
Double.class, double.class
);
return floatTypes.contains(type);
}

private boolean isIntegerType(Class<?> type) {
List<Class<?>> integerTypes = Arrays.asList(
Byte.class, byte.class,
Short.class, short.class,
Integer.class, int.class,
Long.class, long.class,
Float.class, float.class,
Double.class, double.class
Long.class, long.class
);
return numericTypes.contains(type);
return integerTypes.contains(type);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum ExcelRenderLocation {

HEADER, CONTENTS
HEADER, BODY

}
8 changes: 0 additions & 8 deletions src/main/java/com/lannstark/resource/ExcelRenderResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ public String getExcelHeaderName(String dataFieldName) {
return excelHeaderNames.get(dataFieldName);
}

public PreCalculatedCellStyleMap getStyleMap() {
return styleMap;
}

public Map<String, String> getExcelHeaderNames() {
return excelHeaderNames;
}

public List<String> getDataFieldNames() {
return dataFieldNames;
}
Expand Down
Loading

0 comments on commit 6543713

Please sign in to comment.