spring-boot - Spring Boot - 处理 CSV 以及 Excel 多部分文件
问题描述
我在 Spring Boot 应用程序中有一个 REST API,它接收一个 Multipart 文件类型的参数。
用户可能会导入需要处理的巨大尺寸的 CSV 文件或 Excel(.xlsx / .xsl) 文件。
我正在使用 Apache POI 读取 Excel 类型的文件,它工作正常。对于我现有的代码,我如何有效地处理 CSV 文件读取
下面是Excel文件读取代码:
@RequestMapping(value="/read", method = RequestMethod.POST)
@Transactional
public Map<String, String> read(@RequestParam("file") MultipartFile file) {
Map<String, String> response = new ArrayList();
if (!file.isEmpty()) {
ByteArrayInputStream stream;
Workbook wb;
StringBuilder contentSb = new StringBuilder();
try {
stream = new ByteArrayInputStream(file.getBytes());
wb = WorkbookFactory.create(stream);
org.apache.poi.ss.usermodel.Sheet sheet = wb.getSheetAt(wb.getActiveSheetIndex());
Iterator<Row> rowIterator = sheet.rowIterator();
System.out.println("Processing Excel file");
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(0);
if (cell != null) {
contentSb.append(cell.getStringCellValue()+",");
}
}
}
System.out.println("Processed Excel file");
return response;
} catch (Exception e) {
e.printStackTrace();
}
}
else {
return response;
}
}
先感谢您!
解决方案
推荐阅读
- python - 如何使用 scipy 的 savemat 在单元格数组中创建 matlab 向量
- r - 是否可以在 R 中将方法名称作为包装函数参数传递?
- arduino - Arduino灯泡控制
- sql - 动态透视查询超出 varchar 的最大长度 (Microsoft SQL Server)
- python - 在 Jupyter Notebook 中运行 python 语句后没有得到结果
- java - 控制改造调用的错误,如 (404)
- azure-machine-learning-service - Azure 机器学习计算配额?
- python-3.x - 有效地分组数据集,同时保留额外的列
- javascript - Javascript“选择”更改转到锚点(锚点ID是选择值)
- php - 我必须使用 Eloquent 在 Laravel 中加入 6 个不同的表格