fishbone

Map objects to placeholder in Excel template

License

License

GroupId

GroupId

com.yo1000
ArtifactId

ArtifactId

fishbone
Last Version

Last Version

1.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

fishbone
Map objects to placeholder in Excel template
Project URL

Project URL

https://github.com/yo1000/fishbone
Source Code Management

Source Code Management

https://github.com/yo1000/fishbone

Download fishbone

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.1.2-2
org.jetbrains.kotlin : kotlin-reflect jar 1.1.2-2
org.apache.poi : poi jar 3.16
org.apache.poi : poi-ooxml jar 3.16
org.slf4j : slf4j-api jar 1.7.21

test (3)

Group / Artifact Type Version
org.jetbrains.spek : spek-api jar 1.1.1
org.jetbrains.spek : spek-junit-platform-engine jar 1.1.1
org.junit.platform : junit-platform-runner jar 1.0.0-M4

Project Modules

There are no modules declared in this project.

{{fishbone}}

Map objects to placeholder in Excel template

How to use

pom.xml

<dependencies>
    <dependency>
        <groupId>com.yo1000</groupId>
        <artifactId>fishbone</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

template.xslx

A B C D
1 {{subject}}
2 {{date}}
3
4 {{i.name}} [[i : items : 4,5,V]] {{i.price}} {{i.number}} {{i.subtotal}}
5
6
7
8
9 Total number {{totalNumber}}
10 Total fee {{totalFee}}

Implementation

public static void main(String[] args) {
    Sales sales = new Sales(
            "Flower shop",
            new Date(),
            new ArrayList<SalesStatement>() {
                {
                    add(new SalesStatement("Clover", 450, 1));
                    add(new SalesStatement("Moss phlox", 200, 2));
                    add(new SalesStatement("Rosemary", 270, 1));
                }
            });

    new XlsxSheetMapper().map(
            Application.class.getResourceAsStream("/template.xlsx"),
            sales,
            false,
            new Function1<Workbook, Unit>() {
                @Override
                public Unit invoke(Workbook workbook) {
                    try {
                        workbook.write(new FileOutputStream(new File(
                                "target/template1.wrote.xlsx")));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }
    );
}

public static class SalesStatement {
    private String name;
    private int price;
    private int number;

    public SalesStatement(String name, int price, int number) {
        this.name = name;
        this.price = price;
        this.number = number;
    }

    // Getter/Setter
    
    public int getSubtotal() {
        return getPrice() * getNumber();
    }
}

public static class Sales {
    private String subject;
    private Date date;
    private List<SalesStatement> items;

    public Sales(String subject, Date date, List<SalesStatement> items) {
        this.subject = subject;
        this.date = date;
        this.items = items;
    }

    // Getter/Setter

    public int getTotalNumber() {
        int total = 0;

        for (SalesStatement statement : getItems()) {
            total += statement.getNumber();
        }

        return total;
    }

    public int getTotalFee() {
        int total = 0;

        for (SalesStatement statement : getItems()) {
            total += statement.getSubtotal();
        }

        return total;
    }
}

Result

A B C D
1 Flower shop      
2 2017/05/08
3
4 Clover 450 1 450
5 Moss phlox 200 2 400
6 Rosemary 270 1 270
7
8
9 Total number 4
10 Total fee 1120

Versions

Version
1.0.2
1.0.1
1.0.0