如需转载分享,请标明出处,且不用于盈利为目的,谢谢合作!
将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-->
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");
}
}
3.文件结果
文件aaaa.xls里的数据