解析导入的Excel

 

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {
     /*    public static List<List<String>> readXlsx(String path) throws IOException {
            InputStream input = new FileInputStream(path);
            return readXlsx(input);
        }*/
     
        public static List<List<String>> readXls(String path) throws IOException {
            InputStream input = new FileInputStream(path);
            return readXls(input);
        }
     
        /*public static List<List<String>> readXlsx(InputStream input) throws IOException {
            List<List<String>> result = new ArrayList<List<String>>();
            XSSFWorkbook workbook = new XSSFWorkbook(input);
            
            for (XSSFSheet xssfSheet : xssfSheets) {
                if (xssfSheet == null) {
                    continue;
                }
                for (int rowNum = 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                    XSSFRow row = xssfSheet.getRow(rowNum);
                    int minCellNum = row.getFirstCellNum();
                    int maxCellNum = row.getLastCellNum();
                    List<String> rowList = new ArrayList<String>();
                    for (int i = minCellNum; i < maxCellNum; i++) {
                        XSSFCell cell = row.getCell(i);
                        if (cell == null) {
                            continue;
                        }
                        rowList.add(cell.toString());
                    }
                    result.add(rowList);
                }
            }
            return result;
        }*/
     
        public static List<List<String>> readXls(InputStream input) throws IOException {
            List<List<String>> result = new ArrayList<List<String>>();
            HSSFWorkbook workbook = new HSSFWorkbook(input);
            for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
                HSSFSheet sheet = workbook.getSheetAt(numSheet);
                if (sheet == null) {
                    continue;
                }
                int lastRowNum = sheet.getLastRowNum();
                for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                    HSSFRow row = sheet.getRow(rowNum);
                    if(row==null){
                         continue;
                    }
                    int minCellNum = row.getFirstCellNum();
                    int maxCellNum = row.getLastCellNum();
                    List<String> rowList = new ArrayList<String>();
                    for (int i = 0; i < 5; i++) {
                        HSSFCell cell = row.getCell(i);
                        if (cell == null) {
                             rowList.add(getStringVal(row.createCell(i)));
                            continue;
                        }
                        
                        rowList.add(getStringVal(cell));
                    }
                    result.add(rowList);
                }
            }
            return result;
        }
     
        private static String getStringVal(HSSFCell cell) {
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    return cell.getBooleanCellValue() ? "TRUE" : "FALSE";
                case Cell.CELL_TYPE_FORMULA:
                    return cell.getCellFormula();
                case Cell.CELL_TYPE_NUMERIC:
                    cell.setCellType(Cell.CELL_TYPE_STRING);
                    return cell.getStringCellValue();
                case Cell.CELL_TYPE_STRING:
                    return cell.getStringCellValue();
                default:
                    return null;
            }
        }

}
 

 

 

controller 里得到file文件

 InputStream inputStream = file.getInputStream();
 List<List<String>> datas = ExcelUtil.readXls(inputStream);//datas就是Excel的数据了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值