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里的數據