1 Star 0 Fork 0

云剑 / unit-office

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

UnitOffice

本项目是POI的一个Wrapper,目的是更方便的使用POI操作Excel文档, 本项目提供了非常流畅的API,能够自如的应对各种Excel的生成操作, 目前项目还在积极的开发中,不建议直接使用。

本项目采用MIT协议提供。

Quick start

通常,如果你需要使用POI生成Excel,那么这将会是一个很繁琐的过程, 你需要创建WorkBook,然后分别处理对应的行列以及Cell的样式,这是 一个十分费时费力的过程,本项目的目的就是将这一过程变得简单而流畅。

下面,我将演示如何通过本项目进行基本的Excel处理:

首先,这里有一个非常简单的POJO类型——Person

public class Person {

    private String name;

    private String age;

    private String gender;

    private String birthDay;

    // 省略Getter Setter
    
}

那么对于这样的对象,通过本项目进行导出,是非常简单的:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.swdc.offices.xlsx.ExcelRow;
import org.swdc.offices.xlsx.ExcelSheet;

import java.io.FileOutputStream;

public class Demo {

    public static void main(String[] args) {

        // 第一步,创建一个Workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        // 第二步,创建ExcelSheet对象,如果你在使用HSSF类型,这里需要的
        // 就是ExcelXLSSheet,但是没有关系,它们的API是一致的。
        ExcelSheet sheet = new ExcelSheet(workbook, "Sheet A");
        // 第三步,创建表头,
        ExcelRow row = sheet.rowAt(0) // 取第零行,第零个cell,开始填写内容
                .cell(0).text("姓名")
                .nextCell().text("年龄")
                .nextCell().text("生日")
                .nextCell().text("性别")
                .backToRow(); // 返回Row,接下来可以继续对行进行操作。


        // 这里有一组数据,它们每一个都可以生成一行内容
        List<Person> personList = Arrays.asList(
                new Person("张三", "20", "Male", "2021/3/1"),
                new Person("张三3", "20", "Male", "2021/3/3"),
                new Person("张三1", "20", "Male", "2021/4/1"),
                new Person("张三4", "20", "Male", "2021/6/1"),
                new Person("张三6", "20", "Male", "2021/3/8"),
                new Person("张三5", "20", "Male", "2021/7/6")
        );
        row = row.nextRow() // 跳转到表头的下一行,使用ForOf循环生成内容
                .forOf(personList, (person, cell) -> {
                    // 回调会提供当前生成的对象以及本行第0个Cell
                    // 这时要做的很简单,在对应Cell填写数据即可。
                    cell.text(person.getName()).nextCell()
                            .text(person.getAge()).nextCell()
                            .text(person.getBirthDay()).nextCell()
                            .text(person.getGender());
                });
        // 到此就生成完毕了,接下来只需要写到必要的位置,Excel的生成流程也就结束了。
        workbook.write(new FileOutputStream("testPerson.xlsx"));
    }

}

那么如果遇到很复杂的Excel,又该怎么办呢?本项目也提供了对应的设计,虽然生成Excel的 逻辑比较繁琐,但是如果以合适的形式进行组织,那么它的可维护性是非常有保证的:

import org.swdc.offices.generator.GeneratorStage;

public class DemoStrategies {

    @GeneratorStage
    public ExcelRow generateHeader(PipedGenerationContext ctx, ExcelSheet sheet) {
        sheet.autoColumnWidth(0)
                .autoColumnWidth(1)
                .autoColumnWidth(2)
                .autoColumnWidth(3);

        CellPresetFunction<ExcelCell> preset = cell -> cell
                .font()
                .bold(true)
                .back()
                .alignCenter();

        return sheet.rowAt(0).presetCell(preset)
                .cell(0).text("姓名")
                .nextCell().text("年龄")
                .nextCell().text("生日")
                .nextCell().text("性别")
                .backToRow();
    }

    @GeneratorStage
    public void generatePerson(PipedGenerationContext ctx, ExcelSheet sheet) {
        sheet.rowAt(1).forOf(ctx.getGrouped(Person.class), (cell, person) -> {
            cell.text(person.getName()).nextCell()
                    .text(person.getAge()).nextCell()
                    .text(person.getBirthDay()).nextCell()
                    .text(person.getGender());
        });
    }

}

如上所示,不仅可以生成Excel,而且API还可以对Excel的格式进行调整,当然这部分功能目前还相对有限, 通过GeneratorStage注解,一个复杂Excel的生成可以以一定的标准分为不同的部分,每一个方法承担 生成其中一部分的功能,接下来只需要通过PipedExcelGenerator就能使用它创建Excel了:

import org.swdc.offices.generator.PipedExcelGenerator;

import java.io.FileOutputStream;

public class Demo {

    public static void main(String[] args) {
        // 这里有一组数据,它们每一个都可以生成一行内容
        List<Person> personList = Arrays.asList(
                new Person("张三", "20", "Male", "2021/3/1"),
                new Person("张三3", "20", "Male", "2021/3/3"),
                new Person("张三1", "20", "Male", "2021/4/1"),
                new Person("张三4", "20", "Male", "2021/6/1"),
                new Person("张三6", "20", "Male", "2021/3/8"),
                new Person("张三5", "20", "Male", "2021/7/6")
        );

        PipedExcelGenerator generator = new PipedExcelGenerator();
        // 直接使用之前DemoStrategies对象
        generator.generateStages(new DemoStrategies());
        // 通过本方法创建Excel。
        generator.createExcel("Sheet A", personList, new FileOutputStream("demoExcel2.xlsx"));
    }

}
MIT License Copyright (c) 2023 SW-Fantastic 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. 特此免费授予获得本软件副本和相关文档文件(“软件”)的任何人在不受限制的情况下处理本软件的权限, 包括但不限于使用、复制、修改、合并、发布、分发、分许可和/或出售本软件副本的权利,并允许向其提 供本软件的人员这样做,符合以下条件: 上述版权声明和本许可声明应包含在软件的副本或实质部分。 软件是“按原样”提供的,没有任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和不侵权的保证。 在任何情况下,作者或版权持有人均不对因软件或软件的使用或其他交易而产生、产生或与之相关的任何索赔 、损害赔偿或其他责任承担责任,无论是在合同诉讼、侵权诉讼还是其他诉讼中。

简介

Excel API Based Apache POI 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/SWDCloud/unit-office.git
git@gitee.com:SWDCloud/unit-office.git
SWDCloud
unit-office
unit-office
master

搜索帮助