答案:基于Spring Boot和Apache POI实现商品库存的Excel导入导出。1. 添加Web、JPA、MySQL和POI依赖;2. 创建Product实体类映射数据库表;3. 使用XSSFWorkbook导出数据至Excel并设置响应头;4. 读取上传文件解析Excel内容并批量保存;5. 提供REST接口处理导出和导入请求,前端可触发下载或上传文件完成操作。

商品库存的导入导出功能在电商、仓储系统中非常常见。使用Java可以结合Spring Boot、Apache POI和数据库操作来高效实现Excel格式的导入导出。以下是具体实现思路和代码示例。
1. 准备工作:添加依赖
使用Maven管理项目依赖,在pom.xml中引入Spring Boot Web和Apache POI:
<font face="Courier New">
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</font>
2. 定义商品实体类
创建一个Product实体,用于映射数据库表和Excel数据:
<font face="Courier New">
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Integer stock;
// 构造函数、getter 和 setter 省略
}
</font>
3. 实现Excel导出功能
使用XSSFWorkbook生成Excel文件,将数据库中的商品数据写入工作表:
立即学习“Java免费学习笔记(深入)”;
<font face="Courier New">
@Service
public class ExportService {
@Autowired
private ProductRepository productRepository;
public void exportProductsToExcel(HttpServletResponse response) throws IOException {
List<Product> products = productRepository.findAll();
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("商品库存");
// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("ID");
headerRow.createCell(1).setCellValue("商品名称");
headerRow.createCell(2).setCellValue("库存数量");
// 填充数据
int rowNum = 1;
for (Product product : products) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(product.getId());
row.createCell(1).setCellValue(product.getName());
row.createCell(2).setCellValue(product.getStock());
}
// 设置响应头
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=products.xlsx");
workbook.write(response.getOutputStream());
workbook.close();
}
}
</font>
4. 实现Excel导入功能
读取上传的Excel文件,解析内容并批量保存到数据库:
<font face="Courier New">
@Service
public class ImportService {
@Autowired
private ProductRepository productRepository;
public void importProductsFromExcel(MultipartFile file) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
XSSFSheet sheet = workbook.getSheetAt(0);
List<Product> products = new ArrayList<>();
for (Row row : sheet) {
if (row.getRowNum() == 0) continue; // 跳过表头
Product product = new Product();
product.setName(row.getCell(1).getStringCellValue());
product.setStock((int) row.getCell(2).getNumericCellValue());
products.add(product);
}
productRepository.saveAll(products);
workbook.close();
}
}
</font>
5. 控制器层接口暴露
提供HTTP接口供前端调用:
<font face="Courier New">
@RestController
@RequestMapping("/api/products")
public class ProductController {
@Autowired
private ExportService exportService;
@Autowired
private ImportService importService;
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
exportService.exportProductsToExcel(response);
}
@PostMapping("/import")
public ResponseEntity<String> importFile(@RequestParam("file") MultipartFile file) {
try {
importService.importProductsFromExcel(file);
return ResponseEntity.ok("导入成功");
} catch (IOException e) {
return ResponseEntity.status(500).body("导入失败:" + e.getMessage());
}
}
}
</font>
前端可以通过表单上传Excel文件,或使用JavaScript触发下载。后端通过/api/products/export导出,通过/api/products/import导入。
基本上就这些。只要结构清晰,使用Spring Boot和POI处理Excel并不复杂,关键注意空值校验、异常处理和性能优化(如大数据量分批插入)。










