EXCEL导出
<dependency>
<groupId>org.loulan.application</groupId>
<artifactId>loulan-excel-export</artifactId>
<version>2.0.0</version>
</dependency>
更新说明
2.0.0
- 该版本是初始版本,导出得excel得后缀为xlsx
jar包说明
一. 概述
excel得导出主要使用得工具类是ExcelExportUtil,里面包含多种导出得方法,
有快速导出和普通导出,快速导出可以不使用xml配置文件对导出进行配置,下面对这两种导出
做出说明。可以申请doc文档
1. 快速导出 fastWriteExcel
这种方式得导出不使用xml配置描述导出的excel,直接写入数据之后导出数据,
表头由数据对象的key作为表头,也可以指定替换表头名称。但是导出的方式限制为水平方向
的导出,而且不能做单元格合并等等。
2. 普通的导出 writeExcel
这种导出的方式需要xml配置描述文件的配合。需要在描述文件中描述清楚,
要到处excel的工作簿名称。以及要进行合并的单元格,以及指定的单元格作为表头的名称。
同时需要指定导出的模式为水平导出还是垂直都出,数据的开始行列位置。如下为xml的配置
文件:
<?xml version="1.0" encoding="UTF-8"?>
<excel-export-description>
<!--配置工作簿,可以不配置名称,采用默认方式-->
<sheet name="测试1" >
<!--这个是对应循环对象中的key关键字,保证指定的start行列(0是第一行或者第一列)写入指定的数据,
mode是指是水平方向还是垂直方向(vertical垂直,horizontal水平),默认是水平方向-->
<title-column mode="horizontal" start="2">
<column col="0" key="id"/>
<column col="1" key="username"/>
<column col="2" key="name"/>
<column col="3" key="age"/>
</title-column>
<!--这个是表头名称的配置,也可以配置合并单元格-->
<title-config>
<cell startRow="0" endRow="0" startCol="0" endCol="1" name="带索引"/>
<cell startRow="0" endRow="0" startCol="2" endCol="3" name="普通"/>
<cell startRow="1" endRow="1" startCol="0" endCol="0" name="主键"/>
<cell startRow="1" endRow="1" startCol="1" endCol="1" name="用户名"/>
<cell startRow="1" endRow="1" startCol="2" endCol="2" name="姓名"/>
<cell startRow="1" endRow="1" startCol="3" endCol="3" name="年龄"/>
</title-config>
</sheet>
<!--配置工作簿,可以不配置名称,采用默认方式-->
<sheet name="测试2" >
<!--这个是对应循环对象中的key关键字,保证指定的start行列(0是第一行或者第一列)写入指定的数据,
mode是指是水平方向还是垂直方向(vertical垂直,horizontal水平),默认是水平方向-->
<title-column mode="vertical" start="2">
<column col="0" key="id"/>
<column col="1" key="username"/>
<column col="2" key="name"/>
<column col="3" key="age"/>
</title-column>
<!--这个是表头名称的配置,也可以配置合并单元格-->
<title-config>
<cell startRow="0" endRow="1" startCol="0" endCol="0" name="带索引"/>
<cell startRow="2" endRow="3" startCol="0" endCol="0" name="普通"/>
<cell startRow="0" endRow="0" startCol="1" endCol="1" name="主键"/>
<cell startRow="1" endRow="1" startCol="1" endCol="1" name="用户名"/>
<cell startRow="2" endRow="2" startCol="1" endCol="1" name="姓名"/>
<cell startRow="3" endRow="3" startCol="1" endCol="1" name="年龄"/>
</title-config>
</sheet>
</excel-export-description>
测试展示
package org.loulan.application.excel.test;
import org.junit.Before;
import org.junit.Test;
import org.loulan.application.excel.exports.bean.ExcelExportException;
import org.loulan.application.excel.exports.excelInterface.ExcelExportUtil;
import org.loulan.application.excel.test.bean.User;
import org.springframework.util.ResourceUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
/*********************************************************
** Description: excel导入工具测试
** <br><br>
** Date: Created in 2020/3/2 10:58
** @author 楼兰
** @version 0.0.0
*********************************************************/
public class ExcelImportUtilTest {
List<User> list = new ArrayList<>();
@Before
public void before () {
for (int i = 0; i < 5; i++) {
User user = new User();
user.setId("1");
user.setName("杨晓强");
user.setUsername("admin");
user.setAge(12);
user.setDate(new Date());
list.add(user);
}
}
@Test
public void test () throws IOException, ExcelExportException {
FileInputStream xmlStream = new FileInputStream(ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX + "test.xml"));
FileOutputStream fileOutputStream = new FileOutputStream("F:/我的下载/导出测试1.xlsx");
ExcelExportUtil.writeExcel(xmlStream,fileOutputStream,list);
}
@Test
public void test1 () throws FileNotFoundException, ExcelExportException {
FileOutputStream fileOutputStream = new FileOutputStream("F:/我的下载/导出测试2.xlsx");
ExcelExportUtil.fastWriteExcel(fileOutputStream,list);
}
@Test
public void test2 () throws FileNotFoundException, ExcelExportException {
FileOutputStream fileOutputStream = new FileOutputStream("F:/我的下载/导出测试3.xlsx");
Map<String, String> map = new HashMap<>();
map.put("id", "主键");
map.put("username", "用户名");
map.put("age", "年龄");
map.put("name", "姓名");
map.put("date", "日期");
ExcelExportUtil.fastWriteExcel(fileOutputStream, list, map);
}
}