1.pom.xml中添加依赖
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--poi start--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.15</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.15</version> </dependency> <!--poi end-->
2、新建类DataToExcel.java
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.poi.xssf.usermodel.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 将List<Map<String,Object>> 类型数据导出到Excel文件 */ public class DataToExcel { @Data @AllArgsConstructor @NoArgsConstructor public static class Student { String name; int age; String sex; } /** * src:定义下载的文件路径 * @param src */ public static void createExcel(String src) { System.out.println("数据加载..."); List<Student> list = new ArrayList<>(); Student s1 = new Student("张三", 22, "男"); Student s2 = new Student("李四", 22, "男"); Student s3 = new Student("王五", 22, "男"); Student s4 = new Student("赵敏", 22, "女"); Student s5 = new Student("张无忌", 22, "男"); list.add(s1); list.add(s2); list.add(s3); list.add(s4); list.add(s5); System.out.println("数据加载完成..."); List<Map<String, Object>> mapArrayList = new ArrayList<>(); list.forEach(o -> { Map<String, Object> map = new HashMap<>(); map.put("姓名", o.getName()); map.put("年龄", o.getAge()); map.put("性别", o.getSex()); mapArrayList.add(map); }); System.out.println("数据转成Excel..."); // 定义一个新的工作簿 XSSFWorkbook wb = new XSSFWorkbook(); // 创建一个Sheet页 XSSFSheet sheet = wb.createSheet("First sheet"); //设置行高 sheet.setDefaultRowHeight((short) (2 * 256)); //设置列宽 sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 4000); sheet.setColumnWidth(2, 4000); XSSFFont font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 16); //获得表格第一行 XSSFRow row = sheet.createRow(0); //根据需要给第一行每一列设置标题 XSSFCell cell = row.createCell(0); cell.setCellValue("姓名"); cell = row.createCell(1); cell.setCellValue("年龄"); cell = row.createCell(2); cell.setCellValue("性别"); XSSFRow rows; XSSFCell cells; //循环拿到的数据给所有行每一列设置对应的值 for (int i = 0; i < mapArrayList.size(); i++) { // 在这个sheet页里创建一行 rows = sheet.createRow(i + 1); // 该行创建一个单元格,在该单元格里设置值 String name = mapArrayList.get(i).get("姓名").toString(); int age = (int) mapArrayList.get(i).get("年龄"); String sex = mapArrayList.get(i).get("性别").toString(); cells = rows.createCell(0); cells.setCellValue(name); cells = rows.createCell(1); cells.setCellValue(age); cells = rows.createCell(2); cells.setCellValue(sex); } try { File file = new File(src); FileOutputStream fileOutputStream = new FileOutputStream(file); wb.write(fileOutputStream); wb.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { createExcel("d:/aaaa.xls"); } }
4.文件结果
文件aaaa.xls里的数据
原文参考:https://www.freesion.com/article/27671039781/
将LIST<MAP<STRING,OBJECT>> 类型数据导出到EXCEL文件
1.pom.xml中添加依赖
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--poi start-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<!--poi end-->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
2.新建类DataToExcel.java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.xssf.usermodel.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** * 将List<Map<String,Object>> 类型数据导出到Excel文件 */
public class DataToExcel {
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class Student {
String name;
int age;
String sex;
}
/** * src:定义下载的文件路径 * @param src */
public static void createExcel(String src) {
System.out.println("数据加载...");
List<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 22, "男");
Student s2 = new Student("李四", 22, "男");
Student s3 = new Student("王五", 22, "男");
Student s4 = new Student("赵敏", 22, "女");
Student s5 = new Student("张无忌", 22, "男");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
System.out.println("数据加载完成...");
List<Map<String, Object>> mapArrayList = new ArrayList<>();
list.forEach(o -> {
Map<String, Object> map = new HashMap<>();
map.put("姓名", o.getName());
map.put("年龄", o.getAge());
map.put("性别", o.getSex());
mapArrayList.add(map);
});
System.out.println("数据转成Excel...");
// 定义一个新的工作簿
XSSFWorkbook wb = new XSSFWorkbook();
// 创建一个Sheet页
XSSFSheet sheet = wb.createSheet("First sheet");
//设置行高
sheet.setDefaultRowHeight((short) (2 * 256));
//设置列宽
sheet.setColumnWidth(0, 4000);
sheet.setColumnWidth(1, 4000);
sheet.setColumnWidth(2, 4000);
XSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
//获得表格第一行
XSSFRow row = sheet.createRow(0);
//根据需要给第一行每一列设置标题
XSSFCell cell = row.createCell(0);
cell.setCellValue("姓名");
cell = row.createCell(1);
cell.setCellValue("年龄");
cell = row.createCell(2);
cell.setCellValue("性别");
XSSFRow rows;
XSSFCell cells;
//循环拿到的数据给所有行每一列设置对应的值
for (int i = 0; i < mapArrayList.size(); i++) {
// 在这个sheet页里创建一行
rows = sheet.createRow(i + 1);
// 该行创建一个单元格,在该单元格里设置值
String name = mapArrayList.get(i).get("姓名").toString();
int age = (int) mapArrayList.get(i).get("年龄");
String sex = mapArrayList.get(i).get("性别").toString();
cells = rows.createCell(0);
cells.setCellValue(name);
cells = rows.createCell(1);
cells.setCellValue(age);
cells = rows.createCell(2);
cells.setCellValue(sex);
}
try {
File file = new File(src);
FileOutputStream fileOutputStream = new FileOutputStream(file);
wb.write(fileOutputStream);
wb.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
createExcel("d:/aaaa.xls");
}
}
- 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
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
3.文件结果
文件aaaa.xls里的数据