First introduced its dependencies
<-! Parsing office documents -> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <-! Parsing office documents ->
Tools
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
public class OfficeUtils {
protected static final Logger logger = LoggerFactory.getLogger(OfficeUtils.class);
the Map static public <Integer, the Map <Integer, Object >> readExcelContentz (a MultipartFile File) throws Exception {
the Map <Integer, the Map <Integer, Object Content >> new new = the HashMap <Integer, the Map <Integer, Object >> ();
/ / upload filename
the Workbook getWb WB = (file);
IF (WB == null) {
the throw new new BusinessException (ErrorType.WORK_BOOK_EMPTY);
}
Sheet Sheet wb.getSheetAt = (0);
// get the number of rows
int rowNum = sheet. getLastRowNum ();
row row = sheet.getRow (0);
int colNum row.getPhysicalNumberOfCells = ();
// body content should start from the second row, the first row header heading
for (int i = 1; i < rowNum =; I ++) {
Row = sheet.getRow (I);
int j = 0;
Map<Integer, Object> cellValue = new HashMap<Integer, Object>();
while (j < colNum) {
Object obj = getCellFormatValue(row.getCell(j));
cellValue.put(j, obj);
j++;
}
content.put(i, cellValue);
}
return content;
}
//根据Cell类型设置数据
private static Object getCellFormatValue(Cell cell) {
Object cellvalue = "";
if (cell != null) {
switch (cell.getCellTypeEnum()) {
case NUMERIC:
cellvalue = String.valueOf(cell.getNumericCellValue());
break;
case FORMULA: {
cellvalue = cell.getDateCellValue();
break;
}
case STRING:
cellvalue = cell.getRichStringCellValue().getString();
break;
default:
cellvalue = "";
}
} else {
cellvalue = "";
}
return cellvalue;
}
private static Workbook getWb(MultipartFile mf) {
String filepath = mf.getOriginalFilename();
String ext = filepath.substring(filepath.lastIndexOf("."));
Workbook wb = null;
try {
InputStream is = mf.getInputStream();
if (".xls".equals(ext)) {
wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(ext)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
} catch (FileNotFoundException e) {
logger.error("FileNotFoundException", e);
} catch (IOException e) {
logger.error("IOException", e);
}
return wb;
}
}
service layer
public Map<Integer, Map<Integer,Object>> addCustomerInfo(MultipartFile file) { Map<Integer, Map<Integer,Object>> map = new HashMap<>(); try { map = ReadExcelUtil.readExcelContentz(file); } catch (Exception e) { e.printStackTrace (); } // excel in the map data is present, map.get (0) .get (0 ) is the value of the first row of the first column excel, where the data can be processed }
controller layer
@PostMapping public String add(@RequestParam("file")MultipartFile file){ Map<Integer, Map<Integer,Object>> map = customerService.addCustomerInfo(file); return "success"; }
Thus, the basic completion of parsing of Excel.