Excel 上传解析

pom.xml
    
        org.apache.poi
        poi
        3.13
    
    
        org.apache.poi
        poi-ooxml
        3.13
    
package com.sceo.erp.modules.activiti_model.service.impl;
import com.sceo.erp.modules.common.util.Result;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
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.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class excelTool {
    public Result getBankListByExcel(MultipartFile file){

        try {
            InputStream inputStream = file.getInputStream();
            long size = file.getSize();
            if(size >1024 *1024) {
                return Result.ERROR_DATA("当前文件大小为" + Math.ceil(size / 1024 / 1024 / 10 / 100) + "M,超过1M",
                        "当前文件大小为" + Math.ceil(size * 100 / 1024 / 1024 / 10 / 100) + "M,超过1M");
            }
            //获得Excel文件名
            String originalFilename = file.getOriginalFilename();
            // 创建excel工作簿
            Workbook work =getWorkbook(inputStream, originalFilename);
            //存放Eecel
            List list =new ArrayList();
            Sheet sheet = null;
            Row row = null;
            Cell cell = null;
            //遍历每张Sheet
            for (int i = 0; i < work.getNumberOfSheets(); i++) {
                sheet = work.getSheetAt(i);
                if (sheet == null) {
                    continue;
                }
                //遍历sheet里面的行
                for (int j = sheet.getFirstRowNum(); j <= sheet.getLastRowNum(); j++) {
                    row = sheet.getRow(j);
                    if (row == null || row.getFirstCellNum() == j) {
                        continue;
                    }
                    List li = new ArrayList();
                    //遍历每列
                    for (int y=row.getFirstCellNum();y 

                            
                        
                    
                    
                    

你可能感兴趣的:(Excel 上传解析)