spring-boot-zleth-poi

Import Java object to excel

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

com.zleth.poi
ArtifactId

ArtifactId

spring-boot-zleth-poi
Last Version

Last Version

1.0.2.RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

spring-boot-zleth-poi
Import Java object to excel
Project URL

Project URL

https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/spring-boot-zleth-poi
Source Code Management

Source Code Management

https://github.com/jeffddjt/spring-boot-zleth-poi

Download spring-boot-zleth-poi

How to add to project

<!-- https://jarcasting.com/artifacts/com.zleth.poi/spring-boot-zleth-poi/ -->
<dependency>
    <groupId>com.zleth.poi</groupId>
    <artifactId>spring-boot-zleth-poi</artifactId>
    <version>1.0.2.RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/com.zleth.poi/spring-boot-zleth-poi/
implementation 'com.zleth.poi:spring-boot-zleth-poi:1.0.2.RELEASE'
// https://jarcasting.com/artifacts/com.zleth.poi/spring-boot-zleth-poi/
implementation ("com.zleth.poi:spring-boot-zleth-poi:1.0.2.RELEASE")
'com.zleth.poi:spring-boot-zleth-poi:jar:1.0.2.RELEASE'
<dependency org="com.zleth.poi" name="spring-boot-zleth-poi" rev="1.0.2.RELEASE">
  <artifact name="spring-boot-zleth-poi" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.zleth.poi', module='spring-boot-zleth-poi', version='1.0.2.RELEASE')
)
libraryDependencies += "com.zleth.poi" % "spring-boot-zleth-poi" % "1.0.2.RELEASE"
[com.zleth.poi/spring-boot-zleth-poi "1.0.2.RELEASE"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 2.2.6.RELEASE
org.projectlombok : lombok Optional jar 1.18.12
org.apache.poi : poi jar 4.1.1

test (1)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 2.2.6.RELEASE

Project Modules

There are no modules declared in this project.

Export Java Object to Excel

Step 1. add dependency to project

maven

<dependency> 
    <groupId>com.zleth.poi</groupId> 
    <artifactId>spring-boot-zleth-poi</artifactId>
    <version>1.0.2.RELEASE</version>
</dependency>

gradle

compile group: 'com.zleth.poi', name: 'spring-boot-zleth-poi', version: '1.0.2.RELEASE'

Step 2. add annotation to Java Class

Annotation ExcelAlias can add on class and properties.
    It means worksheet title when it annotated on class.
    It means column title when it annotated on property.
    
Annotation ExcelIgnore can ignore export this property.
    It means will disappear this colmun in the excel file.
For example:

    @ExcelAlias("user") //worksheet title
    pubblic class UserInfo {
        
        @ExcelAlias("User Identity") //column title
        private String id;
        
        @ExcelAlias("User Name") //column title
        private String username;
        
        @ExcelIgnore //disappear this column
        private String password;
        
        ......//getters and setters
    }
    
Of course you add nothing annotation on the class then it will use classname and property name to generate worksheet title or column title.

Step 3. Use ExcelStream export excel file in Controller

@RestController
pubic class SomeController{

    @GetMapping("export")
    public Object exportExcel(){
        try{
            List<UserInfo> list = this.userInfoService.getAll();
            ExcelStream stream = new ExcelStream(UserInfo.class);
            ExcelTable table = stream.getTable(list);
            byte[] buf = stream.toExcel(table);

            org.springframework.core.io.Resource resource = new ByteArrayResource(buf);
            String contentType = "application/octet-stream";
            return ResponseEntity.ok()
                    .contentType(MediaType.parseMediaType(contentType))
                    .header(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=\"ExportExcel.xls\"")
                    .body(resource);
        }catch (Exception e){
            log.error("",e);
            return ResponseEntity.status(400).body(e.getMessage());
        }
    }
    

}

Versions

Version
1.0.2.RELEASE
1.0.1.RELEASE