1 Star 0 Fork 85

宝儿 / easy-excel

forked from lis1314 / easy-excel 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 3.31 KB
一键复制 编辑 原始数据 按行查看 历史
lis1314 提交于 2017-03-23 16:22 . 更新 README.md

#easy-excel

使用介绍 http://my.oschina.net/lis1314/blog/693989

** 关于与Spring集成只需要加入下面配置**

<!-- 初始化SpringUtil -->
<bean id="springUtil" class="org.easy.util.SpringUtil"/>

一、jar包依赖

输入图片说明

二、如何使用?,参考

org.easy.excel.test.ExportTest

org.easy.excel.test.ImportTest

支持,复杂对象导航,支持自定义(单元格宽度) 标题样式(背景色,对齐方式,字体颜色)

导出测试使用时,运行org.easy.excel.test.ExportTest类的测试方法,观察具体生成的excel文件

导入测试使用时,运行org.easy.excel.test.ImportTest,观察org.easy.excel.vo.ExcelImportResult

下面展示配置文件

<!-- 测试使用 ,学生类-->
<excel id="student" class="org.easy.excel.test.model.StudentModel" enableStyle="true">
	<field name="id" align="center" titleBgColor="dark_blue"
	columnWidth="3000" titleFountColor="white" title="ID" uniformStyle="true"/>
	<field name="name" title="学生姓名" align="right"/>
	<field name="age" title="年龄" align="center" titleFountColor="red" titleBgColor="dark_blue" isNull="false" regex="^[1-9]\d*$" regexErrMsg="必须是数字"/>
	<field name="studentNo" title="学号" titleFountColor="blue" isNull="false" />
	<field name="createTime" columnWidth="4000" title="创建时间" pattern="yyyy-MM-dd"/>
	<field name="status" title="状态" titleBgColor="green" format="1:正常,0:禁用,-1:无效" />
	<!-- 创建人,可能需要查询数据库校验,这里使用自定义解析器 -->
	<field name="createUser" title="创建人" 
	cellValueConverter="org.easy.excel.test.converter.CreateUserCellValueConverter"/>
	
	<!-- 复杂对象 -->
	<field name="book.bookName" title="图书名称" columnWidth="6000"/>
	<field name="book.author.authorName" title="作者名称"/>
</excel>

上述配置,目前以包含所有支持的可配置属性。

关于excel配置属性说明:参看org.easy.excel.vo.ExcelDefinition

关于field配置属性说明参看:org.easy.excel.vo.FieldValue

关于使用:这里附上部分代码

(开发者只需要关注一个类即可org.easy.excel.ExcelContext),这个类暴露了对外的导入导出所有功能,通常在实际项目里,把它配置到spring容器中初始化一个就可以了

1、导入

public void testImport()throws Exception{
	InputStream fis = new FileInputStream(path);
	ExcelImportResult result = context.readExcel(excelId, fis);
	System.out.println(result.getHeader());
	List<StudentModel> stus = result.getListBean();
	for(StudentModel stu:stus){
		System.out.println(stu);
		BookModel book = stu.getBook();
		System.out.println(book);
		if(book!=null){
			System.out.println(book.getAuthor());
		}
	}
}

2、导出

public void testExportSimple()throws Exception{
	OutputStream ops = new FileOutputStream(path);
	Workbook workbook = context.createExcel(excelId,getStudents());
	workbook.write(ops);
	ops.close();
	workbook.close();
}

技术交流群

489137933

Java
1
https://gitee.com/xiaobao.com/easy-excel.git
git@gitee.com:xiaobao.com/easy-excel.git
xiaobao.com
easy-excel
easy-excel
master

搜索帮助