使用zip压缩包上传excel文件的优点
1、体积更小,节约带宽2、比excel直接读取更方便携带参数及修改3、可以一次性批量导入
Java代码
Controller
@PostMapping("/importData")@ApiOperationSupport(order = 3)@ApiOperation(value = "上传")public R importData(@RequestParam MultipartFile file,@RequestParam("versionId") String versionId) {try {dataTableFjService.importDataSheets(file,versionId);} catch (Exception e) {log.error("上传文件接口", e);return R.fail("上传文件接口异常");}return R.success("上传成功");}
Service
void importDataSheets(MultipartFile file, String dataManagementId, String versionId);
Impl
@Transactional(rollbackFor = Exception.class)
public void importDataSheets(MultipartFile file, String versionId) throws IOException {if (file.isEmpty()) {throw new ServiceException("上传文件为空");}DataVersionFj dataVersion = dataVersionFjService.getById(versionId);if (Func.isEmpty(dataVersion)) {throw new ServiceException("未查询到版本信息");}@Cleanup ZipInputStream zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));ZipEntry entry = zipInputStream.getNextEntry();while (entry != null) {if (!entry.isDirectory()) {String entryName = entry.getName();if (entryName.contains("/")) {entryName = entryName.split("/")[1];}if (!isExcelFile(entryName)) {throw new ServiceException("文件类型有误");}byte[] fileBytes = IOUtils.toByteArray(zipInputStream);@Cleanup InputStream is = new ByteArrayInputStream(fileBytes);applicationContext.getBean(XXXImpl.class).processExcelFile(dataVersion, entryName, is);}zipInputStream.closeEntry();entry = zipInputStream.getNextEntry();}……
}
@Transactional(rollbackFor = Exception.class)
public void processExcelFile(DataVersionFj dataVersion, String fileName, InputStream zipInputStream) {Workbook workbook = null;try {workbook = fileName.endsWith("xlsx") ? new XSSFWorkbook(zipInputStream) :new HSSFWorkbook(zipInputStream);} catch (Exception e) {e.printStackTrace();throw new ServiceException("[" + fileName + "],文件打开失败,请核对文件内容");}String[] tableNames = fileName.split("[.]");TableNameEnum tableNameEnum = TableNameEnum.getByName(tableNames[0]);List<String> fieldMeaningNames = null; List<String> fieldMeaningNames2 = null; List<String> fieldMeaningNames3 = null; Sheet dataSheet = workbook.getSheetAt(0);int dataRow = 0; Iterator<Row> rowIterator = dataSheet.iterator();int dataIndex = 0;while (rowIterator.hasNext() && dataIndex < 3) {Row headRow = rowIterator.next();Iterator<Cell> cellIterator = headRow.cellIterator();List<String> fieldMeaningNamesThis = getFieldMeaningName(cellIterator);if (Func.isNotEmpty(fieldMeaningNamesThis)) {if (isEmptyRow(fieldMeaningNamesThis, fileName)) {continue; }switch (dataIndex) {case 0:fieldMeaningNames = fieldMeaningNamesThis;break;case 1:fieldMeaningNames2 = fieldMeaningNamesThis;break;case 2:fieldMeaningNames3 = fieldMeaningNamesThis;break;}dataRow = dataIndex;}dataIndex++;}if (fieldMeaningNames == null || fieldMeaningNames.size() == 0) {throw new ServiceException("上传文件内容为空");}try {if (Func.isNotEmpty(fieldMeaningNames3)) {for (int i = 1; i < fieldMeaningNames3.size(); i++) {if (Func.isNotBlank(fieldMeaningNames3.get(i)) && Func.isBlank(fieldMeaningNames2.get(i))) {for (int j = i - 1; j >= 0; j--) {if (Func.isNotBlank(fieldMeaningNames2.get(j))) {fieldMeaningNames2.set(i, fieldMeaningNames2.get(j));break;}}}}}if (Func.isNotEmpty(fieldMeaningNames2)) {for (int i = 1; i < fieldMeaningNames2.size(); i++) {if (Func.isNotBlank(fieldMeaningNames2.get(i)) && Func.isBlank(fieldMeaningNames.get(i))) {for (int j = i - 1; j >= 0; j--) {if (Func.isNotBlank(fieldMeaningNames.get(j))) {fieldMeaningNames.set(i, fieldMeaningNames.get(j));break;}}}}}List<Map<String, Object>> fieldMeaningModify = new ArrayList<>();List<Map<String, Object>> fieldMeaningTemp = new ArrayList<>();for (int i = 0; i < fieldMeaningNames.size(); i++) {Map<String, Object> item = new HashMap<>();item.put("name", fieldMeaningNames.get(i));item.put("index", i);fieldMeaningTemp.add(item);}if (fieldMeaningModify.size() == 0) {fieldMeaningModify = fieldMeaningTemp;}……} catch (Exception e) {log.error("插入数据异常:{}", e.getMessage());e.printStackTrace();throw new ServiceException("导入异常");}
}
public List<String> getFieldMeaningName(Iterator<Cell> cellIterator) {List<String> fieldMeaningNames = new ArrayList<>();DataFormatter dataFormatter = new DataFormatter();while (cellIterator.hasNext()) {Cell cell = cellIterator.next();String value = dataFormatter.formatCellValue(cell);fieldMeaningNames.add(value);}return fieldMeaningNames;
}