poi读取Excel数据,并保存到Java集合

这里我用的是gradle:导入了必备的jar包

dependencies {
    compile('org.apache.commons:commons-lang3:3.7')
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    implementation('net.sourceforge.jexcelapi:jxl:2.6')
    implementation('org.apache.poi:poi:3.14')
    implementation('org.apache.poi:poi-ooxml:3.14')
    implementation('org.springframework:spring-web:5.1.2.RELEASE')
    implementation('org.apache.httpcomponents:httpcore:4.4.5')
    implementation('org.springframework:spring-test:5.1.2.RELEASE')
}
import cn.waner.resolverule.demol.ResolveRule;
import org.apache.http.entity.ContentType;
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.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class poiReadExcelInfo {

    private static final String EXCEL_XLS = "xls";
    private static final String EXCEL_XLSX = "xlsx";

    public static void checkExcelVaild(MultipartFile file) throws Exception {
        if (file == null){
            throw new Exception("文件不存在");
        }
        if (!((file.getName().endsWith(EXCEL_XLS) || file.getName().endsWith(EXCEL_XLSX)))){
            throw new Exception("文件不是Excel");
        }

    }

    public static String getCellValue(Cell cell){
        String cellValue = "";
        if(cell == null){
            return cellValue;
        }
        //把数字当成String来读,避免出现1读成1.0的情况
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }
        //判断数据的类型
        switch (cell.getCellType()){
            case Cell.CELL_TYPE_NUMERIC: //数字
                cellValue = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_STRING: //字符串
                cellValue = String.valueOf(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN: //Boolean
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA: //公式
                cellValue = String.valueOf(cell.getCellFormula());
                break;
            case Cell.CELL_TYPE_BLANK: //空值
                cellValue = "";
                break;
            case Cell.CELL_TYPE_ERROR: //故障
                cellValue = "非法字符";
                break;
            default:
                cellValue = "未知类型";
                break;
        }
        return cellValue;
    }

    public static List readExcel(MultipartFile file) throws Exception {
        List resolveRuleList = new ArrayList();
        try {
            checkExcelVaild(file);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        //文件流
        InputStream is = file.getInputStream();
        Workbook workbook = null;
        if(file.getName().endsWith(EXCEL_XLS)){  //Excel 2003
            workbook = new HSSFWorkbook(is);
        }else if(file.getName().endsWith(EXCEL_XLSX)){  // Excel 2007/2010
            workbook = new XSSFWorkbook(is);
        }
        //获取Excel文件第一个sheet
        Sheet sheet = workbook.getSheetAt(0);

        int count = 0;
        //遍历每一行数据
        for (Row row : sheet){
            //过滤第一行数据
            if (count<1){
                count++;
                continue;
            }
            ResolveRule resolveRule = new ResolveRule();
            //遍历每一列数据
            int c = 0;
            for (Cell cell : row){
                String value = getCellValue(cell);
                if (c<5){
                    resolveRule.setRuleRegex(null);
                }
                switch (c){
                    case 0:
                        resolveRule.setAssetCompany(value);
                        break;
                    case 1:
                        resolveRule.setAssetType(value);
                        break;
                    case 2:
                        resolveRule.setAssetModel(value);
                        break;
                    case 3:
                        resolveRule.setEnable(Boolean.parseBoolean(value.toLowerCase()));
                        break;
                    case 4:
                        resolveRule.setNote(value);
                        break;
                    case 5:
                        String[] strings = value.split("###");
                        List patternList = new ArrayList();
                        Pattern pattern = null;
                        for (int x = 0;x Res(){
        List resolveRuleList = null;
        try {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入文件绝对路径(如:E:/解析规则.xlsx):");
            File file = new File(scanner.nextLine());

            FileInputStream inputStream = new FileInputStream(file);
            //File 转 MultipartFile
            MultipartFile mMultipartFile = new MockMultipartFile(file.getName(),file.getName(),
                    ContentType.APPLICATION_OCTET_STREAM.toString(),inputStream);

            resolveRuleList = readExcel(mMultipartFile);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return resolveRuleList;
    }
    
    public static void main(String[] args){
        
        List resolveRuleList = Res();
    }

}

 

你可能感兴趣的:(Java)