1 Star 5 Fork 1

彭欢/excel-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

excel-utils

介绍

  • 通过导入一个模板, 和与模板中匹配的数据源, 进行数据替换;
  • 现支持普通键值数据{{key}}, 和表格数据{{#key#}}两种;
  • 不支持直接实例化,通过builder静态方法获得一个构建模板和数据的对象;
  • 通过该对象build()方法构建TemplateExcelUtils对象,调用export方法即可导出。

软件架构

基于POI开发

使用

第一步:maven依赖
<properties>
        <poi.version>3.15</poi.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>ooxml-schemas</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml-schemas</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
第二步:导入代码
  • 主要代码包括两个Java类(TemplateExcelUtils.java 和 Table.java)
  • TemplateExcelUtils.java为只要逻辑代码,建议放在utils包下
  • Table.java为添加表数据的实体类,建议放在common包下面
第三步: 使用
  1. 在excel表格中使用{{}}和{{##}}两种占位符, 保存Excel(D:template.xlsx)
{{name}} {{title}}
{{#table1#}}
  1. 调用构建类,导入模板
//通过文件系统的方式, 最大扫描行,最大扫描列
TemplateExcelUtils.builder()
    				.setFileSystemTemplate("D:/template.xlsx", 2, 3);
//在web环境下的方式, 最大扫描行,最大扫描列
TemplateExcelUtils.builder().
    				.setClassTemplatePath("/template.xlsx", 2, 3);
  1. 添加普通键值数据
//添加键值数据
Map<String, String> data = new HashMap<>();
data.put("name", "xiaoming");
data.put("title", "The title");
TempalteExcelUtils.builder().
    				.setFileSystemTemplate("D:/template.xlsx", 2, 3)
    				.addValueData(data);
  1. 添加表格数据

Table.java

public class Table{
    //键序列, 通过这个序列依次再rowDataList中取值
    private List<String> keySequence;
    //表头数据
    private List<String> headers;
    //行数据
    private List<Map<String, String>> rowDataList;

    public Table(List<String> keySequence, List<String> headers, List<Map<String, String>> rowDataList) {
        this.keySequence = keySequence;
        this.headers = headers;
        this.rowDataList = rowDataList;
    }

    public Table(List<String> keySequence, List<Map<String, String>> rowDataList) {
        this.keySequence = keySequence;
        this.rowDataList = rowDataList;
    }
    
    /**
     *   getter/setter
     */
}

添加表格数据

//......

//实例表格数据
List<Map<String, String>> rows = new ArrayList<>();
Map<String, String> row = new HashMap<>();
row.put("field1", "data1");
row.put("field2", "data2");
rows.add(row);
Table table = new Table(Arrays.asList("field1", "field2"), rows);

TempalteExcelUtils.builder().
    				.setFileSystemTemplate("D:/template.xlsx", 2, 3)
					.addValueData(data)
					.addMergerTableData("table1", table);

/**
 * 一次调用addMergerTableData 添加表数据,则是将这些表格数据合并; 
 * 多次调用addMergerTableData 添加表数据,则是分别添加。
 * addMergerTableData(String tableName, Table... table);
 */
  1. 调用build() 构建excel
//...

//调用build构建excel
TempalteExcelUtils.builder().
    				.addValueData(data);
					.addMergerTableData("table1", table);
					.build()
  1. 调用export() 导出Excel
//...

OutputStream out = new FileOutputStream("D:/new.xlsx");
TempalteExcelUtils.builder().
    				.addValueData(data);
					.addMergerTableData("table1", table);
					.build()
                    .export(out);
  1. 最终效果
xiaoming The title
data1 data2

web环境

在web环境中将 TemplateExcelUtils.java中的注释打开,导入需要的包

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/pengdev/excel-utils.git
git@gitee.com:pengdev/excel-utils.git
pengdev
excel-utils
excel-utils
master

搜索帮助