使用springboot导出用以上的这个方式写一个controller,要求根据返回数据某个人数据分组之后在合并导出excel
时间: 2024-03-03 16:48:12 浏览: 64
springboot 实现后端接口操作Excel的导出、批量导入功能
好的,以下是根据返回数据某个人数据分组之后再合并并导出Excel的示例代码:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@RestController
public class ExportController {
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
// 假设这里是返回的数据
List<Map<String, Object>> dataList = new ArrayList<>();
Map<String, Object> data1 = new HashMap<>();
data1.put("name", "张三");
data1.put("age", 18);
data1.put("score", 90);
dataList.add(data1);
Map<String, Object> data2 = new HashMap<>();
data2.put("name", "李四");
data2.put("age", 20);
data2.put("score", 80);
dataList.add(data2);
Map<String, Object> data3 = new HashMap<>();
data3.put("name", "张三");
data3.put("age", 18);
data3.put("score", 95);
dataList.add(data3);
// 根据姓名分组
Map<String, List<Map<String, Object>>> groupMap = dataList.stream()
.collect(Collectors.groupingBy(map -> (String) map.get("name")));
// 合并分组后的数据
List<Map<String, Object>> resultList = new ArrayList<>();
for (Map.Entry<String, List<Map<String, Object>>> entry : groupMap.entrySet()) {
String name = entry.getKey();
List<Map<String, Object>> groupList = entry.getValue();
Map<String, Object> resultData = new HashMap<>();
resultData.put("name", name);
int totalScore = 0;
for (Map<String, Object> groupData : groupList) {
totalScore += (int) groupData.get("score");
}
resultData.put("totalScore", totalScore);
resultList.add(resultData);
}
// 导出Excel
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("导出数据");
// 创建标题行
Row titleRow = sheet.createRow(0);
Cell nameCell = titleRow.createCell(0);
nameCell.setCellValue("姓名");
Cell totalScoreCell = titleRow.createCell(1);
totalScoreCell.setCellValue("总分数");
// 创建数据行
int rowIndex = 1;
for (Map<String, Object> resultData : resultList) {
Row dataRow = sheet.createRow(rowIndex++);
Cell nameDataCell = dataRow.createCell(0);
nameDataCell.setCellValue((String) resultData.get("name"));
Cell totalScoreDataCell = dataRow.createCell(1);
totalScoreDataCell.setCellValue((int) resultData.get("totalScore"));
}
// 设置响应头信息
response.setContentType("application/octet-stream");
String fileName = URLEncoder.encode("导出数据.xlsx", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
// 写出响应
workbook.write(response.getOutputStream());
}
}
```
在这个示例中,我们首先假设这里是返回的数据,然后使用Java 8中的Stream API根据姓名分组。接着,我们遍历分组后的数据,计算每个人的总分数,并将结果放入一个新的列表中。最后,我们创建一个Excel文件,并将列表中的数据写入到Excel中。在写完Excel后,我们设置响应头信息以指示将Excel文件作为附件下载,并将Excel文件写入到响应体中。
阅读全文