Skip to content

minsx framework common excel

Joker edited this page Apr 4, 2018 · 6 revisions
项目描述:主要用于Excel数据到JAVA对象解析转化
主要功能
  • 支持.xls与.xlsx
  • 支持同名列
  • 支持合并单元格判断及取值
  • 支持JAVA对象转化
  • 支持去除其他空行/列
  • 支持所有格式的单元格数据
  • 支持高精度数据保存
Excel Reader使用示例
import com.alibaba.fastjson.JSON;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.util.List;

public class ExcelTest {

    private ExcelReader excelReader;

    @Before
    public void initial() throws FileNotFoundException {
        excelReader = new ExcelReader(ExcelTest.class.getClassLoader().getResourceAsStream("测试.xlsx"));
    }

    @Test
    public void getAllSheetData() {
        List<Sheet> dataList = excelReader.getAllSheet();
        System.out.println(JSON.toJSONString(dataList));
    }

    @Test
    public void getAllSheetDataWithDataMapper() {
        List<Sheet> newDataList = excelReader.getAllSheet(oldCell -> {
            Cell newCell = new Cell(oldCell);
            if ("姓名".equals(oldCell.getColumnName())) {
                newCell.setValue("新姓名是:" + oldCell.getValue());
            }
            return newCell;
        });
        System.out.println(JSON.toJSONString(newDataList));
    }

    @Test
    public void getSheetData() {
        Sheet sheet = excelReader.getSheet(0);
        System.out.println(JSON.toJSONString(sheet));
    }

    @Test
    public void getRowData() {
        Row row = excelReader.getRow(0, 0);
        System.out.println(JSON.toJSONString(row));
    }

    @Test
    public void getCellData() {
        Cell cell = excelReader.getCell(0, 0, 0);
        System.out.println(JSON.toJSONString(cell));
    }

    @Test
    public void getSheetDataWithDataMapper() {
        Sheet newSheet = excelReader.getSheet(0, oldCell -> {
            Cell newCell = new Cell(oldCell);
            if ("姓名".equals(oldCell.getColumnName())) {
                newCell.setValue("新姓名是:" + oldCell.getValue());
            }
            return newCell;
        });
        System.out.println(JSON.toJSONString(newSheet));
    }

    @Test
    public void getBeansOfSheet() {
        List<Student> students = excelReader.getBeansOfSheet(1, oldRowData -> {
            Student student = new Student();
            oldRowData.getCells().forEach(oldCellData -> {
                if ("姓名".equals(oldCellData.getColumnName())) {
                    student.setName(oldCellData.getValue());
                } else if ("年龄".equals(oldCellData.getColumnName())) {
                    student.setAge(oldCellData.getValue());
                } else if ("性别".equals(oldCellData.getColumnName())) {
                    student.setSex(oldCellData.getValue());
                }
            });
            return student;
        });
        System.out.println(JSON.toJSONString(students));
    }

}
输出结果
[{
	"rows": [{
		"cells": [{
			"columnName": "姓名",
			"type": "STRING",
			"value": "小张"
		}, {
			"columnName": "年龄",
			"type": "NUMERIC",
			"value": "21"
		}, {
			"columnName": "性别",
			"type": "STRING",
			"value": "男"
		}]
	}, {
		"cells": [{
			"columnName": "姓名",
			"type": "STRING",
			"value": "小红"
		}, {
			"columnName": "年龄",
			"type": "NUMERIC",
			"value": "14"
		}, {
			"columnName": "性别",
			"type": "STRING",
			"value": "女"
		}]
	}, {
		"cells": [{
			"columnName": "姓名",
			"type": "STRING",
			"value": "小明"
		}, {
			"columnName": "年龄",
			"type": "NUMERIC",
			"value": "25"
		}, {
			"columnName": "性别",
			"mRValue": "男",
			"type": "STRING"
		}]
	}, {
		"cells": [{
			"columnName": "姓名",
			"type": "STRING",
			"value": "小亮"
		}, {
			"columnName": "年龄",
			"type": "NUMERIC",
			"value": "31"
		}, {
			"columnName": "性别",
			"mRValue": "男",
			"type": "BLANK"
		}]
	}, .........]
}, {
	"rows": [{
		"cells": [{
			"columnName": "姓名",
			"type": "STRING",
			"value": "老张"
		}, {
			"columnName": "年龄",
			"type": "NUMERIC",
			"value": "54"
		}, {
			"columnName": "性别",
			"type": "STRING",
			"value": "男"
		}]
	}, ........]
}]
Maven依赖使用请参考
  • POM引入:
<dependency>
  <groupId>com.minsx</groupId>
  <artifactId>minsx-framework-common-excel</artifactId>
  <version>1.0.0</version>
</dependency>

Minsx-framework

项目文档
联系方式
Clone this wiki locally