Skip to content

Commit 9573534

Browse files
committed
Add README
1 parent c0b6c66 commit 9573534

1 file changed

Lines changed: 205 additions & 0 deletions

File tree

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# Excel Module
2+
3+
General Excel Module to implement Excel Download simply and fast.
4+
5+
## Usage - Spring Boot with Rest API
6+
7+
Excel Module uses annotation and reflections to render excel file.
8+
9+
You need to use @ExcelColumn to Object to be rendered in excel file.
10+
11+
See below example.
12+
13+
### Jitpack
14+
15+
Here, only explain how to manage dependency with gradle.
16+
17+
See other build tool usage from [Jitpack Hompage](https://jitpack.io/)
18+
19+
```
20+
allprojects {
21+
repositories {
22+
maven { url 'https://jitpack.io' }
23+
}
24+
}
25+
26+
// Use proper version
27+
dependencies {
28+
implementation('com.github.lannstark:excel-download:0.1.1')
29+
}
30+
```
31+
32+
### Server
33+
34+
```java
35+
// In ExcelDeto
36+
// Rendered Field Order is same as Dto field order
37+
public class ExcelDto {
38+
39+
// Annotation Case 1. no headerStyle and contentsStyle
40+
@ExcelColumn(headerName = "User Name")
41+
private String name;
42+
43+
// Annotation Case 2. use not default style, but use defined style
44+
@ExcelColumn(headerName = "User Age",
45+
headerStyle = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BLUE_HEADER")
46+
)
47+
private int age;
48+
49+
// Annotation Case 3. You can also configure contents style
50+
@ExcelColumn(headerName = "Happy BirthDay",
51+
contentsStyle = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "CONTENTS")
52+
)
53+
private LocalDate birthDay;
54+
55+
}
56+
```
57+
58+
If you want to config default style in class, you should use @DefaultHeaderStyle or @DefaultContentsStyle.
59+
This style will be applied to all fields having not field style in this class.
60+
61+
```java
62+
@DefaultHeaderStyle(
63+
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "BLUE_HEADER")
64+
)
65+
@DefaultContentsStyle(
66+
style = @ExcelColumnStyle(excelCellStyleClass = DefaultExcelCellStyle.class, enumName = "CONTENTS")
67+
)
68+
public class ExcelDto {
69+
70+
private String name;
71+
72+
private int age;
73+
74+
private LocalDate birthDay;
75+
76+
}
77+
```
78+
79+
Additionally, There is customizing use case, not pre-defined excel cell style in DefaultExcelCellStyle.
80+
81+
See Customizing section
82+
83+
84+
```java
85+
// In Controller
86+
@RestController
87+
public class Controller {
88+
89+
@GetMapping("/any-url")
90+
public void methodName(RequestDto requestDto, HttpServletResponse response) throws IOException {
91+
// If you specify response type when you use axios,
92+
// you don't need to set HttpServletResponse contenttype. See #1 in Front with axios section
93+
response.setContentType("application/vnd.ms-excel");
94+
95+
List<ExcelDto> excelDtos = someService.getRenderedData(requestDto);
96+
ExcelFile excelFile = new OneSheetExcelFile<>(excelDtos, ExcelDto.class);
97+
excelFile.write(response.getOutputStream());
98+
}
99+
100+
}
101+
```
102+
103+
### Front with axios
104+
```js
105+
axios({
106+
method: 'GET',
107+
url: 'server-url',
108+
responseType: 'blob' // MUST NEED
109+
})
110+
.then(response => {
111+
// #1 Here, I designate type with response header, however, you can specify 'application/vnd.ms-excel'
112+
const url
113+
= window.URL.createObjectURL(new Blobk([response.data], {type : response.headers['content-type']}));
114+
const link = document.createElement('a');
115+
link.href = url;
116+
link.setAttribute('download', 'ExcelFile.xlsx');
117+
document.body.appendChild(link);
118+
link.click();
119+
});
120+
```
121+
122+
123+
## Customizing
124+
125+
You can create custom ExcelCellStyle with Class or Enum
126+
127+
If you use Class, you don't need to designate enumName of @ExcelColumnStyle
128+
```java
129+
public class BlueHeaderStyle implements ExcelCellStyle {
130+
131+
@Override
132+
public void apply(CellStyle cellStyle) {
133+
// Do anything you want to change style
134+
cellStyle.setBlaBla();
135+
}
136+
137+
}
138+
```
139+
140+
For convenient custom style class, we provide template style class, CustomExcelCellStyle
141+
You can set
142+
- cell color
143+
- 4-side borders type
144+
- cell contents align
145+
Other features will be updated gradually.
146+
147+
```java
148+
public class BlueHeaderStyle extends CustomExcelCellStyle {
149+
150+
@Override
151+
public void configure(ExcelCellStyleConfigurer configurer) {
152+
configurer.foregroundColor(223, 235, 246)
153+
.excelBorders(DefaultExcelBorders.newInstance(ExcelBorderStyle.THIN))
154+
.excelAlign(DefaultExcelAlign.CENTER_CENTER);
155+
}
156+
157+
}
158+
```
159+
160+
```java
161+
@DefaultHeaderStyle(
162+
style = @ExcelColumnStyle(excelCellStyleClass = BlueHeaderStyle.class)
163+
)
164+
public class ExcelDto {
165+
166+
private String field1;
167+
168+
}
169+
```
170+
171+
172+
If you use Enum, you have to specify enumName of @ExcelColumnStyle
173+
```java
174+
public enum CustomCellStyle implements ExcelCellStyle {
175+
176+
CUSTOM_HEADER(field1, field2);
177+
178+
// Whatever fields you need to configure cell style
179+
private final String field1;
180+
private final int field2;
181+
182+
@Override
183+
public void apply(CellStyle cellStyle) {
184+
// Do anything you want to change style with defined enum fields.
185+
cellStyle.setBlaBla();
186+
}
187+
188+
}
189+
```
190+
191+
```java
192+
public class ExcelDto {
193+
194+
@ExcelColumn(headerName = "Field Header Title",
195+
contentsStyle = @ExcelColumnStyle(excelCellStyleClass = CustomCellStyle.class, enumName = "CUSTOM_HEADER")
196+
)
197+
private String field1;
198+
199+
}
200+
```
201+
202+
## Kinds of Excel File
203+
204+
- OneSheetExcelFile
205+
- MultiSheetExcelFile

0 commit comments

Comments
 (0)