首先,jar
maven 添加依赖
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupid>org.apache.poi</groupid> <artifactid>poi-ooxml</artifactid> <version>3.15</version> </dependency>
开始以为是poi,然后就直接加poi的依赖,谁知道并没有所需要的类。查了查才发现是poi-ooxml
要用到的类
最最基本的使用
//创建excel文档 xssfworkbook workbook = new xssfworkbook(); //创建sheet xssfsheet sheet = workbook.createsheet("sheetname"); int rownum=0; //创建首行 xssfrow firstrow = sheet.createrow(rownum++); int cellnum = 0; //把保存在titles中的各个列名,分别在row中创建cell for(string key : titles){ xssfcell cell = firstrow.createcell(cellnum++); cell.setcellvalue(key); } //下面可以继续创建行 //把excel写到要写的outputstream中 workbook.write(output); //最后关闭 workbook.close();
小例子一枚
利用反射,把bean类中各属性(用getxxx取出),写入到excel中
excelutil.java
package me.paul.exceldemo; import java.io.ioexception; import java.io.outputstream; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import java.util.linkedhashmap; import java.util.list; import java.util.map; import java.util.regex.matcher; import java.util.regex.pattern; 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; public class excelutil { public <t> void getexcel(list<t> list,class<t> c,outputstream output) throws ioexception, illegalaccessexception, illegalargumentexception, invocationtargetexception{ map<string,method> methodmap = new linkedhashmap<>(); method[] methods = c.getdeclaredmethods(); for(int i=0;i<methods.length;i++){ method method = methods[i]; string name = method.getname(); pattern pattern = pattern.compile("get(.*)"); matcher matcher = null; if((matcher = pattern.matcher(name)).matches()){ name = matcher.group(1); char ch = name.charat(0); char newch = (char) (ch + 32); name = name.replace(ch,newch); methodmap.put(name, method); } } xssfworkbook workbook = new xssfworkbook(); xssfsheet sheet = workbook.createsheet(c.getcanonicalname()); int rownum=0; xssfrow firstrow = sheet.createrow(rownum++); int cellnum = 0; for(string key : methodmap.keyset()){ xssfcell cell = firstrow.createcell(cellnum++); cell.setcellvalue(key); } for(t t : list){ xssfrow row = sheet.createrow(rownum++); cellnum = 0; for(string key:methodmap.keyset()){ method method = methodmap.get(key); //设置可访问,之前不知道这方法,所以关于反射那篇文章有错误,见谅见谅 method.setaccessible(true); object obj = method.invoke(t); xssfcell cell = row.createcell(cellnum++); cell.setcellvalue(obj== null ? "":obj.tostring()); } } workbook.write(output); workbook.close(); } }
app.java 进行测试使用
package me.paul.exceldemo; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.lang.reflect.invocationtargetexception; import java.util.arraylist; import java.util.list; public class app { public static void main(string[] args) throws illegalaccessexception, illegalargumentexception, invocationtargetexception, ioexception { list<user> list = new arraylist<>(); user u = new user(); u.setid(1); u.setname("paul"); u.setage(18); list.add(u); u = new user(); u.setid(2); u.setname("johnson"); u.setage(20); list.add(u); u = new user(); u.setid(3); u.setname("david"); u.setage(22); list.add(u); outputstream output = new fileoutputstream("/home/paul/user.xlsx"); new excelutil().getexcel(list, user.class,output); output.close(); } }
测试结果截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
SpringBoot引用阿里easyexcel,Excel导出返回浏览器下载
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论