├── .idea
├── compiler.xml
└── misc.xml
├── README.md
├── excelutils.iml
├── pom.xml
└── src
└── main
└── java
└── com
└── jenkin
└── excel
├── ExcelUtils.java
├── anno
├── EnableExport.java
├── EnableExportField.java
├── EnableSelectList.java
└── ImportIndex.java
└── enums
└── ColorEnum.java
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # excelutils
2 | # 一个通用的Excel导入导出工具
3 | 使用ExcelUtils里面的parseExcelToList方法进行导入
4 | 使用ExcelUtils里面的exportExcel方法进行导出
5 |
6 | 以下四个注解是导入导出的实体类需要依赖的注解
7 | EnableExport
8 | 该注解使用在类上,设置允许导入导出
9 | EnableExportField
10 | 该注解设置在字段上,设置允许导出该字段,并且可以设置宽度,标题名,背景颜色
11 | EnableSelectList
12 | 该注解设置在字段上,设置是否使用下拉列表
13 | ImportIndex
14 | 该注解设置在字段上,设置允许导入,并且设置导入时对应的Excel列索引
15 |
16 | ColorEnum
17 | 该枚举是一个颜色枚举,里面有一些常用颜色
18 |
19 |
20 | 使用示例:
21 | 实体类:
22 | @EnableExport(fileName = "")
23 | public class SlrEmpSalary implements Serializable {
24 | @ImportIndex(index = 0)
25 | @EnableExportField(colName = "序号", colWidth = 80)
26 | private int seqNumber;
27 | @ImportIndex(index = 1)
28 | @EnableExportField(colName = "员工编号", colWidth = 160,cellColor = ColorEnum.RED)
29 | private String employeeCode;
30 | @ImportIndex(index = 2)
31 | @EnableExportField(colName = "员工姓名", colWidth = 160)
32 | private String employeeName;}
33 |
34 | 导入使用方法:
35 | //文件输入流
36 | inputStream =uploadedFile.getInputStream();
37 | //获取对象集合
38 | List< SlrEmpSalary> empSalaryList =
39 | (List< SlrEmpSalary>)ExcelUtils.parseExcelToList(inputStream, SlrEmpSalary.class);
40 |
41 | 导出使用方法:
42 | //导出数据
43 | List< SlrEmpSalary> dataList = new ArrayList();
44 | //导出
45 | FileUtils.exportExcel(outputStream, dataList, SlrEmpSalary.class, Const.ALL_SELECT_LIST_MAP, exportTitle);
46 |
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/excelutils.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.jenkin.excel
8 | excel-utils
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 |
14 | com.alibaba
15 | fastjson
16 | 1.1.41
17 |
18 |
19 |
20 | org.apache.poi
21 | poi
22 | 3.8
23 |
24 |
25 | org.apache.poi
26 | poi-ooxml
27 | 3.8
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/main/java/com/jenkin/excel/ExcelUtils.java:
--------------------------------------------------------------------------------
1 | package com.jenkin.excel;
2 |
3 | import com.alibaba.fastjson.util.TypeUtils;
4 |
5 |
6 | import com.jenkin.excel.anno.EnableExport;
7 | import com.jenkin.excel.anno.EnableExportField;
8 | import com.jenkin.excel.anno.EnableSelectList;
9 | import com.jenkin.excel.anno.ImportIndex;
10 | import com.jenkin.excel.enums.ColorEnum;
11 | import org.apache.poi.hssf.usermodel.*;
12 | import org.apache.poi.ss.usermodel.*;
13 | import org.apache.poi.ss.util.CellRangeAddress;
14 | import org.apache.poi.ss.util.CellRangeAddressList;
15 | import org.apache.poi.ss.util.RegionUtil;
16 | import org.apache.poi.xssf.usermodel.XSSFDataValidation;
17 | import java.io.*;
18 | import java.lang.reflect.Field;
19 | import java.lang.reflect.InvocationTargetException;
20 | import java.lang.reflect.Method;
21 |
22 | import java.util.ArrayList;
23 | import java.util.HashMap;
24 | import java.util.List;
25 | import java.util.Map;
26 | import java.util.regex.Matcher;
27 | import java.util.regex.Pattern;
28 | import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
29 | public class ExcelUtils {
30 | /** 所有的下拉列表数据存在这个map中,key是对应的Excel列的序号,从0开始,value为下拉列表键对值 **/
31 | public static final Map> ALL_SELECT_LIST_MAP = new HashMap> ();
32 | /**
33 | * 将Excel转换为对象集合
34 | * @param excel Excel 文件
35 | * @param clazz pojo类型
36 | * @return
37 | */
38 | public static List> parseExcelToList(File excel,Class clazz){
39 | List