1.
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.
@PostMapping(value = "/readCell",produces = {"application/json;charset=utf-8"})
@ResponseBody
public HashMap<String,Object> readCell(@RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename();
System.out.println(fileName);
String filePath = "D:\\WorkPlace";
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdir();
}
filePath = filePath+File.separator+fileName;
System.out.println(filePath);
try {
List readCells = ReadCellUtil.readCell(filePath);
return new HashMap<String, Object>(){
{put("readCell", readCells);}};
} catch (Exception e) {
return new HashMap<String,Object>(){
{put("readCell","失败");}};
}
}
3.
@Getter
public enum ReadCellEnum {
DAI_MA("代码","代码","setCode"),
JB_LIST("疾病列表","疾病列表","setDiseaseList"),
JB_NAME("疾病名称","疾病名称","setDiseaseName");
private String code;
private String desc;
private String getWay;
ReadCellEnum(String code ,String desc,String getWay){
this.code = code;
this.desc = desc;
this.getWay = getWay;
}
public static ReadCellEnum getSetMethodByCode(String code) {
for (ReadCellEnum readCellEnum : ReadCellEnum.values()) {
if (readCellEnum.getCode().equals(code)) {
return readCellEnum;
}
}
return null;
}
}
4.
@Slf4j
public class ReadCellUtil {
/**
*
* @param filePath
* @return readCellTest
*/
public static List readCellTest(String filePath) {
FileInputStream inputStream = null;
ArrayList<ReadCell> readCells = new ArrayList<>();
try {
inputStream = new FileInputStream(new File(filePath));
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
log.info("workbook:{}", workbook);
XSSFSheet sheet = workbook.getSheetAt(0);
log.info("sheet:{}", sheet);
int lastRowNum = sheet.getLastRowNum();
log.info("lastRowNum:{}", lastRowNum);
int lastCellNum = sheet.getRow(0).getLastCellNum() - 1;
log.info("lastCellNum:{}", lastCellNum);
for (int i = 1; i <= lastRowNum; i++) {
ReadCell readCell = new ReadCell();
for (int j = 0; j <= lastCellNum; j++) {
XSSFCell title = sheet.getRow(0).getCell(j);
String titleValue = title.getStringCellValue();
log.info("titleValue:{}", titleValue);
XSSFCell cell = sheet.getRow(i).getCell(j);
CellType cellType = cell.getCellType();
log.info("cellType:{}", cellType);
log.info("cellTypeClass:{}", cel
POI解析excel文件读取文件(含合并单元格)
于 2022-08-14 04:10:56 首次发布