springboot excel导入导出代码
时间: 2023-04-01 12:03:09 浏览: 153
spring boot excel导出
你好,关于springboot excel导入导出代码,我可以为您提供一些参考。您可以使用Apache POI库来实现Excel文件的读写操作,具体可以参考以下代码:
1. 导入Excel文件
```
@PostMapping("/import")
public String importExcel(@RequestParam("file") MultipartFile file) throws Exception {
Workbook workbook = WorkbookFactory.create(file.getInputStream());
Sheet sheet = workbook.getSheetAt();
for (Row row : sheet) {
for (Cell cell : row) {
// 处理单元格数据
}
}
return "success";
}
```
2. 导出Excel文件
```
@GetMapping("/export")
public void exportExcel(HttpServletResponse response) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow();
Cell cell = row.createCell();
cell.setCellValue("Hello World");
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
workbook.write(response.getOutputStream());
}
```
希望这些代码可以帮助到您。
阅读全文