编写一段springboot代码,纯后端导出Excel,excel中包含数据列表和echarts统计图
时间: 2024-03-02 18:51:17 浏览: 88
springboot导出excel
5星 · 资源好评率100%
好的,下面是一份使用poi和echarts实现的纯后端导出Excel的示例代码:
```java
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/export")
public class ExcelExportController {
@RequestMapping(value = "/excel", method = RequestMethod.GET)
@ResponseBody
public String exportExcel() {
// 构造数据列表
List<User> userList = new ArrayList<>();
userList.add(new User("张三", 18, "男"));
userList.add(new User("李四", 20, "女"));
userList.add(new User("王五", 22, "男"));
userList.add(new User("赵六", 24, "女"));
// 初始化 Excel 文档
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("用户信息");
// 创建表头
XSSFRow headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("姓名");
headerRow.createCell(1).setCellValue("年龄");
headerRow.createCell(2).setCellValue("性别");
// 填充数据
int rowNum = 1;
for (User user : userList) {
XSSFRow row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getName());
row.createCell(1).setCellValue(user.getAge());
row.createCell(2).setCellValue(user.getGender());
}
// 添加图表
XSSFDrawing drawing = sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 4, 1, 15, 20);
XSSFSheet chartSheet = workbook.createSheet("统计图");
XSSFChart chart = drawing.createChart(anchor);
XSSFChartLegend legend = chart.getOrCreateLegend();
legend.setPosition(LegendPosition.TOP_RIGHT);
XSSFChartAxis xAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
XSSFValueAxis yAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
yAxis.setCrosses(AxisCrosses.AUTO_ZERO);
List<String> xData = new ArrayList<>();
List<Double> yData = new ArrayList<>();
xData.add("张三");
xData.add("李四");
xData.add("王五");
xData.add("赵六");
yData.add(18.0);
yData.add(20.0);
yData.add(22.0);
yData.add(24.0);
XSSFChartSeries series = chart.createSeries();
series.setTitle("年龄统计");
XSSFDataSheet xDataSheet = chartSheet.createDataSheet();
XSSFChartData data = chart.getChartDataFactory().createBarChartData();
XSSFChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
XSSFValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
ChartDataSource<String> xDataSource = DataSources.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 0, 0));
ChartDataSource<Number> yDataSource = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 1, 1));
series = data.addSeries(xDataSource, yDataSource);
chart.plot(data, bottomAxis, leftAxis);
// 输出 Excel 文件
try (FileOutputStream outputStream = new FileOutputStream("users.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return "Excel 导出成功!";
}
private static class User {
private String name;
private int age;
private String gender;
public User(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
}
```
这份代码会在浏览器端提供一个 /export/excel 接口,访问该接口后会在后端生成一个 users.xlsx 文件,其中包含了用户信息列表和一个年龄统计图。
阅读全文