Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vaa25 committed Oct 31, 2021
0 parents commit 21b747c
Show file tree
Hide file tree
Showing 21 changed files with 2,611 additions and 0 deletions.
1,547 changes: 1,547 additions & 0 deletions .dcignore

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 vaa25

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
171 changes: 171 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
= spring-boot-starter-web-excel

Provides ability to upload and download excel file as list or stream of java objects. +
Using streams permits to not keep all data in memory, but to process it line by line. +
Uses https://github.com/vaa25/poiji2[Poiji2] as excel parser.

In your Maven/Gradle project, first add the corresponding dependency:

.maven
[source,xml]
----
<dependency>
<groupId>io.github.vaa25</groupId>
<artifactId>spring-boot-starter-web-excel</artifactId>
<version>0.0.1</version>
</dependency>
----

.gradle
[source,groovy]
----
dependencies {
implementation 'io.github.vaa25:spring-boot-starter-web-excel:0.0.1'
}
----

[source]
----
public class RowEntity{
@ExcelCellName("column1")
private Integer column1;
@ExcelCellName("column2")
private String column2;
}
----

To download file enough to send ExcelHttpFile as response from rest controller. +
`Accept` request header must contain one of next values: +

- `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` to get xlsx file
- `application/vnd.ms-excel` to get xls file
- `text/csv` to get csv file

[source]
----
import static io.github.vaa25.excel.ExcelFormat.Constants.*
@RestController
public class Controller{
@GetMapping(value = "/download-from-list", produces = {XLSX_MEDIA_TYPE_VALUE, CSV_MEDIA_TYPE_VALUE, XLS_MEDIA_TYPE_VALUE})
public ExcelHttpFile<RowEntity> downloadList(){
List<RowEntity> content = getContent();
return ExcelHttpFile.of("file_name", content, RowEntity.class);
}
@GetMapping(value = "/download-from-stream", produces = {XLSX_MEDIA_TYPE_VALUE, CSV_MEDIA_TYPE_VALUE, XLS_MEDIA_TYPE_VALUE})
public ExcelHttpFile<RowEntity> downloadStream(){
Stream<RowEntity> content = getContent();
return ExcelHttpFile.of("file_name", content, RowEntity.class);
}
}
----

To upload file enough to replace @RequestPart with @RequestExcelFile and receive List or Stream as data

[source]
----
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
@RestController
public class Controller{
@PostMapping(value = "/upload-to-stream", consumes = MULTIPART_FORM_DATA_VALUE)
public void uploadStream(@RequestExcelFile("fileToUpload") final Stream<RowEntity> data){
...
}
@PostMapping(value = "/upload-to-list", consumes = MULTIPART_FORM_DATA_VALUE)
public void uploadList(@RequestExcelFile("fileToUpload") final List<RowEntity> data){
...
}
}
----

Poiji options supported. You can redefine default options as bean. It will be used in all transformations if explicit options are absent.

[source]
----
import static io.github.vaa25.excel.RequestExcelFile.DEFAULT_EXCEL_OPTIONS_BEAN_NAME;
@Configuration
public class Config{
@Bean(DEFAULT_EXCEL_OPTIONS_BEAN_NAME)
public void defaultExcelOptions(){
return PoijiOptions.PoijiOptionsBuilder.settings().preferNullOverDefault(true).build();
}
}
----

Explicit options can be added as beans and used next way:

[source]
----
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
@RestController
public class Controller{
@PostMapping(value = "/upload-to-stream", consumes = MULTIPART_FORM_DATA_VALUE)
public void uploadStream(
@RequestExcelFile(value = "fileToUpload", optionsBean = "uploadOptions") final Stream<RowEntity> data
){
...
}
@GetMapping(value = "/download-from-list", produces = {XLSX_MEDIA_TYPE_VALUE, CSV_MEDIA_TYPE_VALUE, XLS_MEDIA_TYPE_VALUE})
public ExcelHttpFile<RowEntity> downloadList(){
List<RowEntity> content = getContent();
return ExcelHttpFile.of("file_name", content, RowEntity.class).setOptionsBean("downloadOptions");
}
}
----

Options can be built dynamically for download:

[source]
----
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
@RestController
public class Controller{
@GetMapping(value = "/download-from-list", produces = {XLSX_MEDIA_TYPE_VALUE, CSV_MEDIA_TYPE_VALUE, XLS_MEDIA_TYPE_VALUE})
public ExcelHttpFile<RowEntity> downloadList(){
List<RowEntity> content = getContent();
return ExcelHttpFile.of("file_name", content, RowEntity.class).setOptions(buildOptions());
}
private PoijiOptions buildOptions(){
return PoijiOptions.PoijiOptionsBuilder.settings().build();
}
}
----

Options have next priority:

1. Dynamic options (for download only)
2. Bean options
3. Default options
30 changes: 30 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
plugins {
id 'org.springframework.boot' version '2.5.6'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'io.github.vaa25'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
withJavadocJar()
withSourcesJar()
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'io.github.vaa25:poiji2:1.2.1'
implementation 'org.apache.poi:poi-ooxml:5.0.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
useJUnitPlatform()
}
Loading

0 comments on commit 21b747c

Please sign in to comment.