首先,jar
maven 添加依赖
?
|
1
2
3
4
5
6
|
<!-- 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
要用到的类
- XSSFWorkbook , 代表一个excel文档
- XSSFSheet , 代表文档中的一个sheet
- XSSFRow , 代表sheet中的一行
- XSSFCell , 代表row中的每一项的值
最最基本的使用
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//创建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
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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 进行测试使用
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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();
}
}
|
测试结果截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://www.wenjingyi.top/passage/get/39?utm_source=tuicool&utm_medium=referral
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 104
-
CentOS下ssh如何登录限制ip?CentOS下ssh登录限制ip的方法
2025-05-27 86 -
2025-05-29 94
-
2025-06-04 73
-
2025-05-27 64
热门评论


