Giter Club home page Giter Club logo

simple-excel's Introduction

Simple-Excel - A simple excel builder

Dependencies

<!--excel报表下载的封装-->
<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

<!--单元测试-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.8.1</version>
    <scope>test</scope>
</dependency>

Usage

DataBean.java

public class DataBean {
    String username;
    Integer age;
    Date birthday;

    public DataBean(String username, Integer age, Date birthday) {
        this.username = username;
        this.age = age;
        this.birthday = birthday;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

TestExcelFile.java

public class TestExcelFile {

    List<DataBean> dataBeans;
    String[] titles = "基本信息,保密".split(",");
    int[] titleColSpans = {2, 1};
    String[] subTitles = "用户名,年龄,生日".split(",");

    @Before
    public void setUp() {
        dataBeans = new ArrayList<DataBean>();
        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            dataBeans.add(new DataBean("name " + i, random.nextInt(100), new Date()));
        }
    }

    @Test
    public void generate() throws Exception {
        File file =
                new ExcelFile("测试文件生成")
                        .writeHead(titles, titleColSpans)
                        .writeHead(subTitles)
                        .writeRows(dataBeans, new RowReader<DataBean>() {
                            @Override
                            public List<Object> read(DataBean dataBean) {
                                List<Object> row = new ArrayList<Object>();
                                row.add(dataBean.getUsername());
                                row.add(dataBean.getAge());
                                row.add(dataBean.getBirthday());
                                return row;
                            }
                        }).end();

        ExcelFile.write(file,new FileOutputStream("./"+file.getName()));

        //do something with the generated file here
    }

}

Demo

Image of Yaktocat

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.