首先列出 工具类解析excel,及MultipartFile转换为File
package com.ott.server.btt.webapi.utils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class ReadExcelUtil {
public static List getColumn(InputStream iStream, int columnIndex) throws IOException, InvalidFormatException {
List columnList=new ArrayList<>();
Workbook book = WorkbookFactory.create(iStream);
Sheet sheet = book.getSheetAt(0);
for (int runNum =1; runNum <=sheet.getLastRowNum();runNum++) {
Row row = sheet.getRow(runNum);
if (row != null) {
int minColIx = row.getFirstCellNum();
int maxColIx = row.getLastCellNum();
//遍历该行,获取每个cell元素
for (int colIx = minColIx; colIx < maxColIx; colIx++) {
Cell cell = row.getCell(colIx);
//获取指定的一列
if (cell.getColumnIndex() == columnIndex) {
columnList.add(cell.getStringCellValue());
} else {
continue;
}
}
}
}
return columnList;
}
public static File MultipartFileToFile(MultipartFile multipartFile) {
File file = null;
//判断是否为null
if (multipartFile.equals("") || multipartFile.getSize() <= 0) {
return file;
}
//MultipartFile转换为File
InputStream ins = null;
OutputStream os = null;
try {
ins = multipartFile.getInputStream();
file = new File(multipartFile.getOriginalFilename());
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ins != null){
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
}
业务处理
public void downInvoiceImageZip(MultipartFile file, HttpServletResponse response) throws IOException, InvalidFormatException {
File fileNew = ReadExcelUtil.MultipartFileToFile(file);
List invoiceCode = ReadExcelUtil.getColumn(new FileInputStream(fileNew),3);
List invoiceNo = ReadExcelUtil.getColumn(new FileInputStream(fileNew),2);
List



