java poi写一个Excel导出,导出对象类型是List<People> peopleList有5个属性, People对象中有两个属性为List<Son> sonList,List<dog> dogList。导出的Excel表头为peopleList中三个普通字段和List<Son>、List<dog>为一级表头,sonList、dogList中的属性为List<Son> sonList、List<dog> dogList下的二级表头
时间: 2023-07-15 09:11:50 浏览: 78
好的,首先你需要添加以下依赖:
```xml
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
然后,你可以按照以下步骤编写代码:
1. 创建一个 Workbook 对象,这里我们使用 XSSFWorkbook。
```java
Workbook workbook = new XSSFWorkbook();
```
2. 创建一个 Sheet 对象,并设置表头。
```java
Sheet sheet = workbook.createSheet("peopleList");
Row headerRow = sheet.createRow(0);
Cell cell = headerRow.createCell(0);
cell.setCellValue("姓名");
cell = headerRow.createCell(1);
cell.setCellValue("年龄");
cell = headerRow.createCell(2);
cell.setCellValue("性别");
int columnIndex = 3;
for (int i = 0; i < peopleList.size(); i++) {
People people = peopleList.get(i);
for (Son son : people.getSonList()) {
for (Dog dog : people.getDogList()) {
String sonName = son.getName();
String dogName = dog.getName();
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "名称");
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "颜色");
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "年龄");
}
}
}
```
3. 遍历 List<People>,并将数据写入 Excel 中。
```java
int rowIndex = 1;
for (People people : peopleList) {
Row row = sheet.createRow(rowIndex++);
cell = row.createCell(0);
cell.setCellValue(people.getName());
cell = row.createCell(1);
cell.setCellValue(people.getAge());
cell = row.createCell(2);
cell.setCellValue(people.getGender());
columnIndex = 3;
for (Son son : people.getSonList()) {
for (Dog dog : people.getDogList()) {
String sonName = son.getName();
String dogName = dog.getName();
String sonColor = son.getColor();
String dogColor = dog.getColor();
int sonAge = son.getAge();
int dogAge = dog.getAge();
cell = row.createCell(columnIndex++);
cell.setCellValue(sonName);
cell = row.createCell(columnIndex++);
cell.setCellValue(sonColor);
cell = row.createCell(columnIndex++);
cell.setCellValue(sonAge);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogName);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogColor);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogAge);
}
}
}
```
4. 最后,将 Excel 数据写入输出流中。
```java
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=peopleList.xlsx");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
```
完整代码如下:
```java
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
public class PeopleExcelExportUtil {
public static void export(HttpServletResponse response, List<People> peopleList) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("peopleList");
Row headerRow = sheet.createRow(0);
Cell cell = headerRow.createCell(0);
cell.setCellValue("姓名");
cell = headerRow.createCell(1);
cell.setCellValue("年龄");
cell = headerRow.createCell(2);
cell.setCellValue("性别");
int columnIndex = 3;
for (int i = 0; i < peopleList.size(); i++) {
People people = peopleList.get(i);
for (Son son : people.getSonList()) {
for (Dog dog : people.getDogList()) {
String sonName = son.getName();
String dogName = dog.getName();
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "名称");
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "颜色");
cell = headerRow.createCell(columnIndex++);
cell.setCellValue(sonName + "-" + dogName + "-" + "年龄");
}
}
}
int rowIndex = 1;
for (People people : peopleList) {
Row row = sheet.createRow(rowIndex++);
cell = row.createCell(0);
cell.setCellValue(people.getName());
cell = row.createCell(1);
cell.setCellValue(people.getAge());
cell = row.createCell(2);
cell.setCellValue(people.getGender());
columnIndex = 3;
for (Son son : people.getSonList()) {
for (Dog dog : people.getDogList()) {
String sonName = son.getName();
String dogName = dog.getName();
String sonColor = son.getColor();
String dogColor = dog.getColor();
int sonAge = son.getAge();
int dogAge = dog.getAge();
cell = row.createCell(columnIndex++);
cell.setCellValue(sonName);
cell = row.createCell(columnIndex++);
cell.setCellValue(sonColor);
cell = row.createCell(columnIndex++);
cell.setCellValue(sonAge);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogName);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogColor);
cell = row.createCell(columnIndex++);
cell.setCellValue(dogAge);
}
}
}
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=peopleList.xlsx");
OutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
}
```
阅读全文