Spring Boot本身并不直接提供解析Excel或CSV文件的功能,但你可以使用第三方库来实现这些功能。以下是一些建议和示例代码,帮助你在Spring Boot项目中解析Excel和CSV文件。
对于Excel文件,你可以使用Apache POI库。首先,添加依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
然后,你可以使用以下代码来读取Excel文件:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ExcelReader {
public List<List<String>> readExcel(String filePath) throws IOException {
List<List<String>> data = new ArrayList<>();
FileInputStream file = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
List<String> rowData = new ArrayList<>();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
rowData.add(cell.toString());
}
data.add(rowData);
}
workbook.close();
file.close();
return data;
}
}
对于CSV文件,你可以使用OpenCSV库。首先,添加依赖:
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.5.2</version>
</dependency>
然后,你可以使用以下代码来读取CSV文件:
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CsvReader {
public List<String[]> readCsv(String filePath) throws IOException, CsvValidationException {
List<String[]> data = new ArrayList<>();
try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
String[] line;
while ((line = reader.readNext()) != null) {
data.add(line);
}
}
return data;
}
}
你可以在Spring Boot的Controller或Service中使用这些类来解析文件。例如:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
@RestController
public class FileController {
@PostMapping("/upload-excel")
public String uploadExcel(@RequestParam("file") MultipartFile file) {
try {
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
ExcelReader reader = new ExcelReader();
List<List<String>> data = reader.readExcel(convFile.getAbsolutePath());
// 处理数据
return "Excel文件解析成功";
} catch (IOException e) {
e.printStackTrace();
return "Excel文件解析失败";
}
}
@PostMapping("/upload-csv")
public String uploadCsv(@RequestParam("file") MultipartFile file) {
try {
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
CsvReader reader = new CsvReader();
List<String[]> data = reader.readCsv(convFile.getAbsolutePath());
// 处理数据
return "CSV文件解析成功";
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
return "CSV文件解析失败";
}
}
}
请注意,这里的示例代码仅用于演示目的。在实际项目中,你可能需要处理更多的异常情况和边界条件。
领取专属 10元无门槛券
手把手带您无忧上云