加入非poi的excel导出,导出数据类型灵活(得益于jdk8+的 BiFuntion使用)

This commit is contained in:
lxyer 2018-07-06 10:53:42 +08:00
parent aaa80c9678
commit f7b0fb28b5
7 changed files with 727 additions and 217 deletions

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
/target/
.project
.classpath
/.settings/
/.externalToolBuilders/
/bin/
*.iml
.idea/
/.idea/
/out/
*.zip
/tmp/
*.swp
~$*

7
README.md Normal file
View File

@ -0,0 +1,7 @@
# ExcelUtil
### 功能
1. poi的导入导出 支持多sheet导出,详见代码 ExcelKit
2. 非poi导出导出数据量理论无上限导出数据类型不限制List<T>(JDK8+),
如果是jdk7及以下版本未做支持需要修改部分代码

49
pom.xml Normal file
View 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>

File diff suppressed because one or more lines are too long

View File

@ -1,217 +1,380 @@
import java.io.IOException; package excel.poi;
import java.io.OutputStream;
import java.lang.reflect.Field; import org.apache.poi.hssf.usermodel.HSSFCell;
import java.text.SimpleDateFormat; import org.apache.poi.hssf.usermodel.HSSFRow;
import java.util.Calendar; import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.List; import org.apache.poi.poifs.filesystem.OfficeXmlFileException;
import java.util.Map; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import javax.servlet.http.HttpServletRequest; import org.apache.poi.ss.usermodel.Sheet;
import javax.servlet.http.HttpServletResponse; import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** import java.io.*;
* 使用poi报表导出工具类 import java.lang.reflect.Field;
* 把poi的一个调用接口抽出来便于导出功能的管理 import java.text.SimpleDateFormat;
* @author LiangXianYou lxy208@126.com import java.util.*;
* @param
* /**
*/ * 使用poi报表导出工具类
public class ExcelUtil { * 把poi的一个调用接口抽出来便于导出功能的管理
* @author LiangXianYou lxy208@126.com
/** * @param
* <b>导出list中map做载体的数据到excel</b><br> *
* 参数说明<br> */
* list:存放了Map数据的集合<br> public class ExcelKit {
* hdNames:表头列名<br>
* hds:对应表头的数据KEY<br> /**
* xlsName:导出文件名<br> * <b>导出list中map做载体的数据到excel</b><br>
* @author LiangXianYou * 参数说明<br>
* @date 2015-6-25 上午10:22:51 * list:存放了Map数据的集合<br>
*/ * hdNames:表头列名<br>
public static <T> boolean outPutExcelByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{ * hds:对应表头的数据KEY<br>
* xlsName:导出文件名<br>
Workbook wb = new HSSFWorkbook(); //创建工作薄 * @author LiangXianYou
Sheet sheet = wb.createSheet(); //创建工作表 * @date 2015-6-25 上午10:22:51
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度 */
//写入表头---Excel的第一行数据 public static <T> boolean exportByMap(List<Map<String, Object>> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
Row nRow = sheet.createRow(0); //创建行
for(int i=0;i<hdNames.length;i++){ Workbook wb = new XSSFWorkbook(); //创建工作薄
Cell nCell = nRow.createCell(i); //创建单元格 Sheet sheet = wb.createSheet(); //创建工作表
nCell.setCellValue(hdNames[i]); sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
} //写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
//写入每一行数据---一条记录就是一行数据 for(int i=0;i<hdNames.length;i++){
for(int i=0;i<list.size();i++){ Cell nCell = nRow.createCell(i); //创建单元格
for(int j=0;j<hds.length;j++){ nCell.setCellValue(hdNames[i]);
Object o = list.get(i).get(hds[j]); //得到列的值 }
data2Excel(sheet, o, i+1, j); //将值写入Excel
} //写入每一行数据---一条记录就是一行数据
} for(int i=0;i<list.size();i++){
return downloadExcel(wb, xlsName, request, response); for(int j=0;j<hds.length;j++){
} Object o = list.get(i).get(hds[j]); //得到列的值
data2Excel(sheet, o, i+1, j); //将值写入Excel
/** }
* Excels导出多个sheet }
* @author LiangXianYou return downloadExcel(wb, xlsName, request, response);
* @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{ * Excels导出多个sheet
* @author LiangXianYou
Workbook wb = new HSSFWorkbook(); //创建工作薄 * @date 2015-6-16 下午5:56:56
for(int i=0;i<list.size();i++){ */
Map<String, Object> map = list.get(i); //map:data,sheetName,hds,hdNames,xlsName;request,response
Sheet sheet = wb.createSheet(); //创建工作表 public static <T> boolean exportExcels(List<Map<String, Object>> list,String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
String sheetName = (String) map.get("sheetName");
wb.setSheetName(i, sheetName); Workbook wb = new XSSFWorkbook(); //创建工作薄
//写入表头---Excel的第一行数据 for(int i=0;i<list.size();i++){
Row nRow = sheet.createRow(0); //创建行 Map<String, Object> map = list.get(i);
String[] hdNames = (String[]) map.get("hdNames"); Sheet sheet = wb.createSheet(); //创建工作表
for(int j=0;j<hdNames.length;j++){ String sheetName = (String) map.get("sheetName");
Cell nCell = nRow.createCell(j); //创建单元格 wb.setSheetName(i, sheetName);
nCell.setCellValue(hdNames[j]); //写入表头---Excel的第一行数据
} Row nRow = sheet.createRow(0); //创建行
String[] hdNames = (String[]) map.get("hdNames");
//写入每一行数据---一条记录就是一行数据 for(int j=0;j<hdNames.length;j++){
@SuppressWarnings("unchecked") Cell nCell = nRow.createCell(j); //创建单元格
List<T> data = (List<T>) map.get("data"); nCell.setCellValue(hdNames[j]);
String[] hds = (String[]) map.get("hds"); }
for(int j=0;j<data.size();j++){
for(int k=0;k<hds.length;k++){ //写入每一行数据---一条记录就是一行数据
Object o = getFieldValue(data.get(j), hds[k]); //得到列的值 @SuppressWarnings("unchecked")
data2Excel(sheet, o, j+1, k); //将值写入Excel List<T> data = (List<T>) map.get("data");
} String[] hds = (String[]) map.get("hds");
} for(int j=0;j<data.size();j++){
} for(int k=0;k<hds.length;k++){
Object o = getFieldValue(data.get(j), hds[k]); //得到列的值
return downloadExcel(wb, xlsName, request, response); data2Excel(sheet, o, j+1, k); //将值写入Excel
} }
}
/** }
* 参数说明
* listlist数据集合 return downloadExcel(wb, xlsName, request, response);
* hdNames表头需要显示的名称 }
* hds表头对应的对象属性名称 hdNames一一对应
* xlsName导出Excel的预定义名称 /**
* request: HttpServletRequest * 参数说明
* responseHttpServletResponse * listlist数据集合
* 通过数据构建Excel * hdNames表头需要显示的名称
* @author LiangXianYou * hds表头对应的对象属性名称 hdNames一一对应
* @throws Exception * xlsName导出Excel的预定义名称
* @date 2015-3-13 上午11:00:30 * request: HttpServletRequest
*/ * responseHttpServletResponse
public static <T> boolean outPutExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{ * 通过数据构建Excel
* @author LiangXianYou
Workbook wb = new HSSFWorkbook(); //创建工作薄 * @throws Exception
Sheet sheet = wb.createSheet(); //创建工作表 * @date 2015-3-13 上午11:00:30
sheet.autoSizeColumn(( short ) 0 ); //自适应宽度 */
//写入表头---Excel的第一行数据 public static <T> boolean exportExcel(List<T> list, String[] hdNames, String[] hds, String xlsName, HttpServletRequest request, HttpServletResponse response) throws Exception{
Row nRow = sheet.createRow(0); //创建行
for(int i=0;i<hdNames.length;i++){ Workbook wb = new XSSFWorkbook(); //创建工作薄
Cell nCell = nRow.createCell(i); //创建单元格 Sheet sheet = wb.createSheet(); //创建工作表
nCell.setCellValue(hdNames[i]); sheet.autoSizeColumn(( short ) 0 ); //自适应宽度
} //写入表头---Excel的第一行数据
Row nRow = sheet.createRow(0); //创建行
//写入每一行数据---一条记录就是一行数据 for(int i=0;i<hdNames.length;i++){
for(int i=0;i<list.size();i++){ Cell nCell = nRow.createCell(i); //创建单元格
for(int j=0;j<hds.length;j++){ nCell.setCellValue(hdNames[i]);
Object o = getFieldValue(list.get(i), hds[j]); //得到列的值 }
data2Excel(sheet, o, i+1, j); //将值写入Excel
} //写入每一行数据---一条记录就是一行数据
} for(int i=0;i<list.size();i++){
return downloadExcel(wb, xlsName, request, response); for(int j=0;j<hds.length;j++){
} Object o = getFieldValue(list.get(i), hds[j]); //得到列的值
data2Excel(sheet, o, i+1, j); //将值写入Excel
/** }
* 传递一个Wookbook给定文件名以及request和response下载Excel文档 }
* @author LiangXianYou return downloadExcel(wb, xlsName, request, response);
* @throws IOException }
* @date 2015-3-13 上午9:00:36
*/ /**
@SuppressWarnings("all") * 传递一个Wookbook给定文件名以及request和response下载Excel文档
private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{ * @author LiangXianYou
if(request.getHeader("user-agent").indexOf("MSIE") != -1) { * @throws IOException
xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls"; * @date 2015-3-13 上午9:00:36
} else { */
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls"; @SuppressWarnings("all")
} private static boolean downloadExcel(Workbook wb, String xlsName, HttpServletRequest request, HttpServletResponse response) throws IOException{
OutputStream os = response.getOutputStream(); if(request.getHeader("user-agent").indexOf("MSIE") != -1) {
response.setContentType("application/vnd.ms-excel"); xlsName = java.net.URLEncoder.encode(xlsName,"utf-8") + ".xls";
response.setHeader("Content-disposition", "attachment;filename="+xlsName); } else {
xlsName = new String(xlsName.getBytes("utf-8"),"iso-8859-1")+ ".xls";
wb.write(os); }
return true; OutputStream os = response.getOutputStream();
} response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename="+xlsName);
/**
* 通过泛型实例对象得到某一字段值 wb.write(os);
* @author LiangXianYou return true;
* @date 2015-3-13 上午10:53:32 }
*/
private static <T> Object getFieldValue(T t, String fieldName) throws Exception{ /**
if(t == null){ * 通过泛型实例对象得到某一字段值
return null; * @author LiangXianYou
} * @date 2015-3-13 上午10:53:32
Field field = t.getClass().getDeclaredField(fieldName); */
field.setAccessible(true);//暴力反射 private static <T> Object getFieldValue(T t, String fieldName) throws Exception{
Object o = field.get(t);//得到字段数据的值 if(t == null){
return o; return null;
} }
Field field = t.getClass().getDeclaredField(fieldName);
/** field.setAccessible(true);//暴力反射
* 将数据写到Excel中 Object o = field.get(t);//得到字段数据的值
* @author LiangXianYou return o;
* @date 2015-3-13 上午10:37:55 }
*/
private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c){ /**
//通过获得sheet中的某一列有得到没有创建 * 将数据写到Excel中
Row nRow = sheet.getRow(r); * @author LiangXianYou
if(nRow == null){ * @date 2015-3-13 上午10:37:55
nRow = sheet.createRow(r); */
} private static void data2Excel(Sheet sheet, Object o, Integer r, Integer c){
// nRow.setColumnWidth(r, arg1); //通过获得sheet中的某一列有得到没有创建
Row nRow = sheet.getRow(r);
Cell nCell = nRow.createCell(c); if(nRow == null){
nRow = sheet.createRow(r);
}
//根据不同类型进行转化如有其它类型没有考虑周全的使用发现的时候添加 // nRow.setColumnWidth(r, arg1);
char type = 'x';
if(o instanceof Integer){ Cell nCell = nRow.createCell(c);
type = 1;
}else if(o instanceof Double){
type = 2; //根据不同类型进行转化如有其它类型没有考虑周全的使用发现的时候添加
}else if(o instanceof Float){ char type = 'x';
type = 3; if(o instanceof Integer){
}else if(o instanceof String){ type = 1;
type = 4; }else if(o instanceof Double){
}else if(o instanceof Date){ type = 2;
type = 5; }else if(o instanceof Float){
}else if(o instanceof Calendar){ type = 3;
type = 6; }else if(o instanceof String){
}else if(o instanceof Boolean){ type = 4;
type = 7; }else if(o instanceof Date){
}else if(o == null) { type = 5;
type = 8; }else if(o instanceof Calendar){
} type = 6;
}else if(o instanceof Boolean){
switch (type) { type = 7;
case 1:nCell.setCellValue((Integer)o);break; }else if(o == null) {
case 2:nCell.setCellValue((Double)o);break; type = 8;
case 3:nCell.setCellValue((Float)o);break; }
case 4:nCell.setCellValue((String)o);break;
case 5:nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));break; switch (type) {
case 6:nCell.setCellValue((Calendar)o);break; case 1:nCell.setCellValue((Integer)o);break;
case 7:nCell.setCellValue((Boolean)o);break; case 2:nCell.setCellValue((Double)o);break;
case 8:nCell.setCellValue("");break; case 3:nCell.setCellValue((Float)o);break;
default:nCell.setCellValue(o + "");break; case 4:nCell.setCellValue((String)o);break;
} case 5:nCell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(o));break;
} case 6:nCell.setCellValue((Calendar)o);break;
case 7:nCell.setCellValue((Boolean)o);break;
} case 8:nCell.setCellValue("");break;
default:nCell.setCellValue(o + "");break;
}
}
//读取 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;
}
}

View 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
View 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;
}
}