ssm数据导出excel
时间: 2023-07-25 08:39:38 浏览: 51
要实现SSM数据导出Excel,你可以按照以下步骤进行:
1.在Spring配置文件中配置POI相关依赖,例如:
```
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
```
2.创建一个Controller处理导出Excel的请求,例如:
```
@RequestMapping("/export")
public void export(HttpServletResponse response) throws Exception {
// 设置response头信息
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=data.xls");
response.flushBuffer();
// 获取数据
List<Data> dataList = dataService.getDataList();
// 创建Workbook
Workbook workbook = new HSSFWorkbook();
// 创建Sheet
Sheet sheet = workbook.createSheet("数据");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("序号");
headerRow.createCell(1).setCellValue("名称");
headerRow.createCell(2).setCellValue("数值");
// 填充数据
int rowNum = 1;
for (Data data : dataList) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(data.getId());
row.createCell(1).setCellValue(data.getName());
row.createCell(2).setCellValue(data.getValue());
}
// 输出数据
workbook.write(response.getOutputStream());
workbook.close();
}
```
3.在前端页面中添加导出Excel的链接,例如:
```
<a href="/export">导出Excel</a>
```
这样就可以实现SSM数据导出Excel了。