EXCEL文件導入相關

總結一下excel文件上傳時對excel文件格式的判定以及根據不同版本的excel創建對應的workbook。直接上代碼

package com.mrx.controller;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.FileMagic;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.IOException;

/**
 * @author
 * @create 2020-03-17 11:48
 */
@Api(tags = "Excel測試")
@RequestMapping(value = "/api/excel")
@RestController
public class ExcelController {


    public void upload(@RequestParam("file") MultipartFile file){
        // 判斷是否是Excel方法一
        isExcel1(file);

        // 判斷是否是Excel方法二(推薦)
        isExcel2(file);

        // excel處理
        excelProcess(file);
    }


    /** 通過文件後綴名判斷 */
    private boolean isExcel1(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        if ((StringUtils.endsWithIgnoreCase(fileName,".xls")) || (StringUtils.endsWithIgnoreCase(fileName,".xlsx"))){
            return true;
        }else {
            return false;
        }
    }

    /** 使用魔法數字判斷 */
    private boolean isExcel2(MultipartFile file) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(file.getInputStream());
            FileMagic fileMagic = FileMagic.valueOf(bis);
            if ((fileMagic.equals(FileMagic.OLE2)) || (fileMagic.equals(FileMagic.OOXML))){
                return true;
            }
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /** 判斷excel版本創建不同的workbook */
    private void excelProcess(MultipartFile file) {
        Workbook workbook = null;
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(file.getInputStream());
            // 判斷是否是excel以及判斷excel版本
            FileMagic fileMagic = FileMagic.valueOf(bis);
            //.xls
            if (fileMagic.equals(FileMagic.OLE2)) {
                workbook = new HSSFWorkbook(bis);
            //.xlsx
            } else if (fileMagic.equals(FileMagic.OOXML)) {
                workbook = new XSSFWorkbook(bis);
            }
            if(workbook != null){
            	// 主要業務邏輯處理......
            	// 主要業務邏輯處理......
            	// 主要業務邏輯處理......
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (workbook != null){
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

小尾巴~~
只要有積累,就會有進步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章