加入非poi的excel导出,导出数据类型灵活(得益于jdk8+的 BiFuntion使用)
This commit is contained in:
parent
aaa80c9678
commit
f7b0fb28b5
15
.gitignore
vendored
Normal file
15
.gitignore
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
/target/
|
||||
.project
|
||||
.classpath
|
||||
/.settings/
|
||||
/.externalToolBuilders/
|
||||
/bin/
|
||||
*.iml
|
||||
.idea/
|
||||
/.idea/
|
||||
/out/
|
||||
|
||||
*.zip
|
||||
/tmp/
|
||||
*.swp
|
||||
~$*
|
7
README.md
Normal file
7
README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# ExcelUtil
|
||||
|
||||
### 功能
|
||||
1. poi的导入导出 支持多sheet导出,详见代码 ExcelKit
|
||||
2. 非poi导出,导出数据量理论无上限;导出数据类型不限制List<T>(JDK8+),
|
||||
如果是jdk7及以下版本,未做支持(需要修改部分代码)
|
||||
|
49
pom.xml
Normal file
49
pom.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.lxyer</groupId>
|
||||
<artifactId>jdk-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<testSourceDirectory>test</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.poi</groupId>
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<version>3.17</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tomcat</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
<version>6.0.37</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
167
src/excel/ExcelExportKit.java
Normal file
167
src/excel/ExcelExportKit.java
Normal file
File diff suppressed because one or more lines are too long
@ -1,20 +1,25 @@
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
package excel.poi;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRow;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 使用poi报表导出工具类
|
||||
@ -23,7 +28,7 @@ import org.apache.poi.ss.usermodel.Workbook;
|
||||
* @param
|
||||
*
|
||||
*/
|
||||
public class ExcelUtil {
|
||||
public class ExcelKit {
|
||||
|
||||
/**
|
||||
* <b>导出list中map做载体的数据到excel</b><br>
|
||||
@ -35,9 +40,9 @@ public class ExcelUtil {
|
||||
* @author LiangXianYou
|
||||
* @date 2015-6-25 上午10:22:51
|
||||
*/
|
||||
public static <T> boolean outPutExcelByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
public static <T> boolean exportByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
|
||||
Workbook wb = new HSSFWorkbook(); //创建工作薄
|
||||
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
||||
Sheet sheet = wb.createSheet(); //创建工作表
|
||||
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
|
||||
//写入表头---Excel的第一行数据
|
||||
@ -63,9 +68,9 @@ public class ExcelUtil {
|
||||
* @date 2015-6-16 下午5:56:56
|
||||
*/
|
||||
//map:data,sheetName,hds,hdNames,xlsName;request,response
|
||||
public static <T> boolean outPutExcels(List<Map<String, Object>> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
public static <T> boolean exportExcels(List<Map<String, Object>> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
|
||||
Workbook wb = new HSSFWorkbook(); //创建工作薄
|
||||
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
||||
for(int i=0;i<list.size();i++){
|
||||
Map<String, Object> map = list.get(i);
|
||||
Sheet sheet = wb.createSheet(); //创建工作表
|
||||
@ -107,9 +112,9 @@ public class ExcelUtil {
|
||||
* @throws Exception
|
||||
* @date 2015-3-13 上午11:00:30
|
||||
*/
|
||||
public static <T> boolean outPutExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
public static <T> boolean exportExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
|
||||
|
||||
Workbook wb = new HSSFWorkbook(); //创建工作薄
|
||||
Workbook wb = new XSSFWorkbook(); //创建工作薄
|
||||
Sheet sheet = wb.createSheet(); //创建工作表
|
||||
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
|
||||
//写入表头---Excel的第一行数据
|
||||
@ -214,4 +219,162 @@ public class ExcelUtil {
|
||||
}
|
||||
}
|
||||
|
||||
//读取 excel
|
||||
//读取excel表头
|
||||
public static Map readExcelHead(File file, String[] fields) throws IOException {
|
||||
List<Map> list = null;
|
||||
|
||||
try {
|
||||
list = readExcel(new FileInputStream(file), fields, 0);//2007 -
|
||||
} catch (OfficeXmlFileException e) {
|
||||
list = readExcelPlus(new FileInputStream(file), fields, 0);//2007 +
|
||||
}
|
||||
return list.size() > 0 ? list.get(0) : new HashMap();
|
||||
}
|
||||
|
||||
//读取excel表头
|
||||
public static Map readExcelHead(String fPath, String[] fields) throws IOException {
|
||||
return readExcelHead(new File(fPath), fields);
|
||||
}
|
||||
|
||||
public static List<Map> readExcel(File file, String[] fields) throws IOException {
|
||||
List<Map> list = null;
|
||||
|
||||
try {
|
||||
list = readExcel(new FileInputStream(file), fields, -1);//2007 -
|
||||
} catch (OfficeXmlFileException e) {
|
||||
list = readExcelPlus(new FileInputStream(file), fields, -1);//2007 +
|
||||
}
|
||||
|
||||
//去除表头
|
||||
if (list.size() > 0){
|
||||
list.remove(0);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static List<Map> readExcel(String fPath, String[] fields) throws IOException {
|
||||
return readExcel(new File(fPath), fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* excel读取2007-
|
||||
* @author Lxyer 2016/8/1 10:32.
|
||||
*/
|
||||
private static List<Map> readExcel(InputStream is, String[] fields, int lastRowNum) throws IOException,OfficeXmlFileException {
|
||||
List<Map> list = new ArrayList<>();
|
||||
|
||||
HSSFWorkbook wk = new HSSFWorkbook(is);
|
||||
HSSFSheet sheet = wk.getSheetAt(0);
|
||||
|
||||
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
|
||||
lastRowNum = sheet.getLastRowNum();
|
||||
}
|
||||
|
||||
int t = 0;
|
||||
r:for (int i=0; i<=lastRowNum; i++){
|
||||
HSSFRow row = sheet.getRow(i);
|
||||
if (row == null) continue ;
|
||||
short cellNum = row.getLastCellNum();
|
||||
//空跳过/连续三行为空结束
|
||||
if (isEmptyRow(row, fields.length)) {
|
||||
if (t++ > 3) break;
|
||||
continue ;
|
||||
}
|
||||
|
||||
Map map = new HashMap();
|
||||
for (int j=0; j<cellNum && j<fields.length; j++){
|
||||
HSSFCell cell = row.getCell(j);
|
||||
if (cell == null){
|
||||
map.put(fields[j], "");
|
||||
continue;
|
||||
}
|
||||
|
||||
int cellType = cell.getCellType();
|
||||
if (cellType == 0){
|
||||
map.put(fields[j], (long)cell.getNumericCellValue()+"");
|
||||
}else {
|
||||
map.put(fields[j], cell.getStringCellValue());
|
||||
}
|
||||
}
|
||||
list.add(map);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* excel读取 2007+
|
||||
* @author Lxyer 2016/8/1 10:32.
|
||||
*/
|
||||
private static List<Map> readExcelPlus(InputStream is, String[] fields, int lastRowNum) throws IOException {
|
||||
List<Map> list = new ArrayList<>();
|
||||
|
||||
XSSFWorkbook wk = new XSSFWorkbook(is);
|
||||
XSSFSheet sheet = wk.getSheetAt(0);
|
||||
|
||||
if (lastRowNum < 0 || lastRowNum > sheet.getLastRowNum()){
|
||||
lastRowNum = sheet.getLastRowNum();
|
||||
}
|
||||
|
||||
int t=0;
|
||||
r:for (int i=0; i<=lastRowNum; i++){
|
||||
XSSFRow row = sheet.getRow(i);
|
||||
if (row == null) continue ;
|
||||
short cellNum = row.getLastCellNum();
|
||||
|
||||
if (isEmptyRow(row, fields.length)) {
|
||||
if (t++ > 3) break;
|
||||
continue ;
|
||||
}
|
||||
|
||||
Map map = new HashMap();
|
||||
//读取数据
|
||||
for (int j=0; j<cellNum && j<fields.length; j++){
|
||||
XSSFCell cell = row.getCell(j);
|
||||
if (cell == null){
|
||||
map.put(fields[j], "");
|
||||
continue;
|
||||
}
|
||||
|
||||
int cellType = cell.getCellType();
|
||||
if (cellType == 0){
|
||||
map.put(fields[j], (long)cell.getNumericCellValue()+"");
|
||||
}else {
|
||||
map.put(fields[j], cell.getStringCellValue());
|
||||
}
|
||||
}
|
||||
list.add(map);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
//空跳过/连续三行为空结束
|
||||
private static boolean isEmptyRow(XSSFRow row, int len){
|
||||
for (int i = 0; i< row.getLastCellNum() && i < len; i++) {
|
||||
XSSFCell cell = row.getCell(i);//列
|
||||
if (cell != null && cell.getCellType() != 0
|
||||
&& cell.getStringCellValue() != null
|
||||
&& !cell.getStringCellValue().isEmpty()){
|
||||
//System.out.println("---F");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//System.out.println("---T");
|
||||
return true;
|
||||
}
|
||||
private static boolean isEmptyRow(HSSFRow row, int len){
|
||||
for (int i = 0; i< row.getLastCellNum() && i < len; i++) {
|
||||
HSSFCell cell = row.getCell(i);//列
|
||||
if (cell != null && cell.getCellType() != 0
|
||||
&& cell.getStringCellValue() != null
|
||||
&& !cell.getStringCellValue().isEmpty()){
|
||||
//System.out.println("---F");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//System.out.println("---T");
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
76
test/excel/ExportTest.java
Normal file
76
test/excel/ExportTest.java
Normal file
@ -0,0 +1,76 @@
|
||||
package excel;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/6 10:29.
|
||||
*/
|
||||
public class ExportTest {
|
||||
|
||||
@Test
|
||||
public void exportBean(){
|
||||
Map<String, String> heads = new LinkedHashMap();
|
||||
heads.put("姓名", "name");
|
||||
heads.put("年龄", "age");
|
||||
|
||||
int total = 5_0000;
|
||||
List rows = new ArrayList<>(total);
|
||||
Random random = new Random();
|
||||
|
||||
for (int i = 0; i < total; i++) {
|
||||
String name = UUID.randomUUID().toString();
|
||||
int age = random.nextInt(100);
|
||||
rows.add(new Person(name, age));
|
||||
}
|
||||
File file = new File("target/user.zip");
|
||||
if (file.exists()) file.delete();
|
||||
|
||||
try {
|
||||
ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.BEAN);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exportMapToZip(){
|
||||
|
||||
Map<String, String> heads = new LinkedHashMap();
|
||||
heads.put("姓名", "name");
|
||||
heads.put("姓名1", "name1");
|
||||
heads.put("姓名2", "name2");
|
||||
heads.put("年龄", "age");
|
||||
|
||||
int total = 5_0000;
|
||||
List<Map> rows = new ArrayList<>(total);
|
||||
|
||||
Map r1 = new HashMap();
|
||||
r1.put("name", "张三");
|
||||
r1.put("age", 12);
|
||||
rows.add(r1);
|
||||
|
||||
Random random = new Random();
|
||||
|
||||
//使用uuid 模拟复杂数据导出
|
||||
for (int i = 0; i < total; i++) {
|
||||
Map r2 = new HashMap();
|
||||
r2.put("name", UUID.randomUUID());
|
||||
r2.put("name1", UUID.randomUUID());
|
||||
r2.put("name2", UUID.randomUUID());
|
||||
r2.put("age", random.nextInt(100));
|
||||
rows.add(r2);
|
||||
}
|
||||
File file = new File("target/hello.zip");
|
||||
if (file.exists()) file.delete();
|
||||
|
||||
try {
|
||||
ExcelExportKit.exportExcelZip(heads, rows, file, ExcelExportKit.TC.MAP);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
33
test/excel/Person.java
Normal file
33
test/excel/Person.java
Normal file
@ -0,0 +1,33 @@
|
||||
package excel;
|
||||
|
||||
/**
|
||||
* Created by liangxianyou at 2018/7/6 10:28.
|
||||
*/
|
||||
public class Person {
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user