java poi读写excel

本文介绍了一款用于Excel数据导入导出的实用工具,支持实体对象转换及多种导出方式,包括全注解导出、多sheet操作等,并提供了详细的接口使用说明。

 最近开发公司的运营平台,几次需求都涉及到excel的导入导出,所以决定写个util,包含实体对象转换功能,并且打成了jar包,在文章下方有链接

具体使用方法看下面代码注释?:

一.excel导出接口.<br/>

导出接口代码如下:
  1 package transmit.excel;
  2 
  3 import java.io.OutputStream;
  4 import java.util.Collection;
  5 import java.util.List;
  6 
  7 /**
  8  * excel导出接口.<br/>
  9  * 数据量过万推荐使用SXSSFExporter,非线程安全.<br/>
 10  * 支持全注解方式导出,支持多sheet操作,但不支持并发操作(POI不支持),如果实体对象仅添加注解,没有写注解value和index则导出的标题行为属性名.<br/>
 11  * 属性类型为数组/list的泛型仅支持:基本数据类型对应的包装类和String(写出的格式为逗号分隔的字符串).<br/>
 12  * 支持导出方式: <br/>
 13  * <ol>
 14  * 一. auto ,执行完毕直接写入数据到输出流并且关闭workbook.<br/>
 15  * 1.注解+标题list方式导出:<br/>
 16  * writeTo(List<T> beans, List<String> headers, OutputStream outputStream) <br/>
 17  * 入参:
 18  * <ol>
 19  * <li>(1)需要导出的对象,需要加ExcelAlias注解,不需要配置index.</li>
 20  * <li>(2)注解的title值List(支持多标题,英文逗号分隔),根据元素顺序排序.</li>
 21  * <li>(3)目标流</li>
 22  * </ol>
 23  * 2.全注解导出:writeTo(List<T> beans, Class<T> clazz, OutputStream os) <br/>
 24  * 入参:
 25  * <ol>
 26  * <li>(1)需要导出的对象,需要加ExcelAlias注解,根据注解的index排序.</li>
 27  * <li>(2)实体类型.</li>
 28  * <li>(3)目标流</li>
 29  * </ol>
 30  * 二. 手动,最后需要自己执行export方法将workbook中的数据写入到输出流<br/>
 31  * 步骤:
 32  * <ol>
 33  * <li>(1)writeHeader(List<String> headers).</li>
 34  * <li>(2)writeContent(List<List<String>> dataSet).</li>
 35  * <li>(3)export(OutputStream outputStream).</li>
 36  * </ol>
 37  * </ol>
 38  *
 39  * @author v_hebo
 40  * @create 2017-12-07 14:30
 41  */
 42 public interface ExcelExporter<T extends Collection> extends Exporter {
 43 
 44     /**
 45      * 删除指定行的内容.<br></>
 46      *
 47      * @param row 行号
 48      */
 49     void removeRow(Integer row);
 50 
 51     /**
 52      * 删除最后一行数据.<br></>
 53      */
 54     void removeLastRow();
 55 
 56     /**
 57      * 替换指定行的内容.<br></>
 58      *
 59      * @param row  行号
 60      * @param line 数据
 61      */
 62     void replace(Integer row, T line);
 63 
 64     /**
 65      * 跳过skip行.<br></>
 66      *
 67      * @param skip 跳过的行数
 68      */
 69     void skip(Integer skip);
 70 
 71     /**
 72      * 写入一行到指定的sheet.<br></>
 73      *
 74      * @param line 数据Collection
 75      * @return this
 76      */
 77     ExcelExporter writeLine(T line);
 78 
 79     /**
 80      * 将标题行写入指定的sheet.<br/>
 81      *
 82      * @param headers 标题Collection
 83      * @return this
 84      */
 85     ExcelExporter writeHeader(T headers);
 86 
 87     /**
 88      * 将多行数据写入指定的sheet.<br/>
 89      *
 90      * @param dataSet 数据list
 91      * @return this
 92      */
 93     ExcelExporter writeContent(List<T> dataSet);
 94 
 95     /**
 96      * 将多行数据写入指定的sheet<br/>
 97      *
 98      * @param dataSet   数据list
 99      * @param sheetName sheetName
100      * @return this
101      */
102     ExcelExporter writeToAppointSheet(List<T> dataSet, String sheetName);
103 
104     /**
105      * 全注解导出,title为标题,index排序.<br/>
106      *
107      * @param beans 实体List
108      * @param clazz class
109      * @param <E>   实体类型
110      * @return 成功:true,失败:false
111      * @throws ReflectiveOperationException ReflectiveOperationException
112      */
113     <E> ExcelExporter writeTo(List<E> beans, Class<E> clazz) throws ReflectiveOperationException;
114 
115     /**
116      * 全注解导出,title为标题,index排序.<br/>
117      *
118      * @param beans     实体List
119      * @param clazz     class
120      * @param sheetName sheetName
121      * @param <E>       实体类型
122      * @return 成功:true,失败:false
123      * @throws ReflectiveOperationException ReflectiveOperationException
124      */
125     <E> ExcelExporter writeToAppointSheet(List<E> beans, Class<E> clazz, String sheetName) throws ReflectiveOperationException;
126 
127     /**
128      * 全注解导出,title为标题,index排序.<br/>
129      *
130      * @param beans        实体List
131      * @param clazz        class
132      * @param outputStream 输出流
133      * @param <E>          实体类型
134      * @return 成功:true,失败:false
135      * @throws ReflectiveOperationException ReflectiveOperationException
136      */
137     <E> Boolean writeTo(List<E> beans, Class<E> clazz, OutputStream outputStream) throws ReflectiveOperationException;
138 
139     /**
140      * 根据属性List<String> headers的顺序导出List<E> beans,<br/>
141      *
142      * @param beans   实体List
143      * @param headers 导出的标题
144      * @param <E>     实体类型
145      * @return 成功:true,失败:false
146      * @throws ReflectiveOperationException ReflectiveOperationException
147      */
148     <E> ExcelExporter writeTo(List<E> beans, List<String> headers) throws ReflectiveOperationException;
149 
150     /**
151      * 根据属性List<String> headers的顺序导出List<E> beans,<br/>
152      *
153      * @param beans     实体List
154      * @param headers   导出的标题
155      * @param sheetName sheetName
156      * @param <E>       实体类型
157      * @return 成功:true,失败:false
158      * @throws ReflectiveOperationException ReflectiveOperationException
159      */
160     <E> ExcelExporter writeToAppointSheet(List<E> beans, List<String> headers, String sheetName) throws ReflectiveOperationException;
161 
162     /**
163      * 根据属性List<String> headers的顺序导出List<E> beans,<br/>
164      *
165      * @param beans        实体List
166      * @param headers      导出的标题
167      * @param outputStream 输出流
168      * @param <E>          实体类型
169      * @return 成功:true,失败:false
170      * @throws ReflectiveOperationException ReflectiveOperationException
171      */
172     <E> Boolean writeTo(List<E> beans, List<String> headers, OutputStream outputStream) throws ReflectiveOperationException;
173 }

 




二.excel导入接口.<br/>
 1 package transmit.excel;
 2 
 3 
 4 import java.lang.reflect.InvocationTargetException;
 5 import java.util.Collection;
 6 import java.util.List;
 7 import java.util.Map;
 8 
 9 /**
10  * excel导入接口.<br/>
11  * 解析方式:目前支持三种方式
12  * <ol>
13  * 1. readContentToBeans(Class<E> clazz)<br/>
14  * excel文件第一行必须是属性名或者注解value(支持value写多个值,逗号分隔).<br/>
15  * 入参:
16  * <ol>
17  * <li>(1)写入实体的类型.</li>
18  * </ol>
19  * 2.readContentToBeans(Class<E> clazz, Map<Integer,String> mapperTo)<br/>
20  * 入参:
21  * <ol>
22  * <li>(1)写入实体的类型.</li>
23  * <li>(2)属性映射位置(K:cellIndex,V:可以属性名,也可以注解value).</li>
24  * </ol>
25  * 3. readContentToBeans(Class<E> clazz,boolean useAnnotationIndex)<br/>
26  * 入参:
27  * <ol>
28  * <li>(1)写入实体的类型.</li>
29  * <li>(2)是否使用注解索引(索引为excel文件的cellIndex).</li>
30  * </ol>
31  * </ol>
32  *
33  * @author v_hebo
34  * @create 2017-12-07 14:38
35  */
36 public interface ExcelImporter<T extends Collection> extends Importer {
37 
38     /**
39      * readHeader 读取sheet内容,有默认config,也可自己配置读取的相关参数.<br/>
40      * {@link ExcelImporterImpl#config(int, int, int, int)}
41      *
42      * @return 读取的数据
43      */
44     T readHeader();
45 
46     /**
47      * readContent 读取sheet内容,有默认config,也可自己配置读取的相关参数.<br/>
48      * {@link ExcelImporterImpl#config(int, int, int, int)}
49      *
50      * @return 读取的数据
51      */
52     List<T> readContent();
53 
54     /**
55      * readContent 通过注解读取sheet内容到对象,有默认config,也可自己配置读取的相关参数.<br/>
56      * {@link ExcelImporterImpl#config(int, int, int, int)}
57      *
58      * @return 读取的数据
59      */
60     <E> List<E> readContentToBeans(Class<E> clazz) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException;
61 
62     /**
63      * readContent 通过注解读取sheet内容到对象,有默认config,也可自己配置读取的相关参数.<br/>
64      * {@link ExcelImporterImpl#config(int, int, int, int)}
65      *
66      * @return 读取的数据
67      */
68     <E> List<E> readContentToBeans(Class<E> clazz, boolean useAnnotationIndex) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException;
69 
70     /**
71      * readContent 通过注解读取sheet内容到对象,有默认config,也可自己配置读取的相关参数.<br/>
72      * {@link ExcelImporterImpl#config(int, int, int, int)}
73      *
74      * @return 读取的数据
75      */
76     <E> List<E> readContentToBeans(Class<E> clazz, Map<Integer, String> headers) throws NoSuchMethodException;
77 
78 }

 


把项目打成了压缩包,直接可以导入到eclise中,有一个ExcelTest.java,修改IO的路径可以直接运行,导入的测试代码需要根据需求修改注解或者map
百度云链接: 点击下载
  

 

转载于:https://www.cnblogs.com/jia-bei/p/8391735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值