POI解析excel文件读取文件(含合并单元格)

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
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值