java 读取Excel并解析获取数据

本文介绍了一种将Excel文件解析成Map的方法,其中key为sheet名称,value为对应sheet的数据内容。通过遍历读取每个sheet,处理每一行和每一列的数据,并存储到List中,最终形成一个Map结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下方法将Excel文件解析成map,key为Excel的sheet名称,value为Excel中对应sheet数据内容list

public Map<String, Object> readXlsToMap(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        InputStream input = null;
        HSSFWorkbook wb = null;
        try {
            input = file.getInputStream();
            // 创建文档
            wb = new HSSFWorkbook(input);
            // 读取sheet(页)
            for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {

                HSSFSheet hssfSheet = wb.getSheetAt(numSheet);
                if (hssfSheet != null) {
                    List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
                    ArrayList<String> rowList = null;
                    if (hssfSheet != null) {
                        totalRows = hssfSheet.getLastRowNum();
                        // 读取Row
                        for (int rowNum = 0; rowNum <= totalRows; rowNum++) {
                            HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                            if (hssfRow != null) {
                                rowList = new ArrayList<String>();
                                totalCells = hssfRow.getLastCellNum();
                                // 读取列,从第一列开始
                                for (short c = 0; c <= totalCells + 1; c++) {
                                    HSSFCell cell = hssfRow.getCell(c);
                                    if (cell == null) {
                                        rowList.add(ExcelUtil.EMPTY);
                                        continue;
                                    }
                                    rowList.add(ExcelUtil.getHValue(cell).trim());
                                }
                                list.add(rowList);
                            }
                        }
                    }
                    map.put(hssfSheet.getSheetName(), list);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

ExcelUtil 工具类


import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;

/**
 * Excel工具类
 * 
 * @author lp
 *
 */
public class ExcelUtil {
    public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";
    public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";
    public static final String EMPTY = "";
    public static final String POINT = ".";
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

    /**
     * 获得path的后缀名
     * 
     * @param path
     * @return
     */
    public static String getPostfix(String path) {
        if (path == null || EMPTY.equals(path.trim())) {
            return EMPTY;
        }
        if (path.contains(POINT)) {
            return path.substring(path.lastIndexOf(POINT) + 1, path.length());
        }
        return EMPTY;
    }

    /**
     * 单元格格式
     * 
     * @param hssfCell
     * @return
     */
    @SuppressWarnings({ "static-access", "deprecation" })
    public static String getHValue(HSSFCell hssfCell) {
        if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
            return String.valueOf(hssfCell.getBooleanCellValue());
        } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
            String cellValue = "";
            if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
                Date date = HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue());
                cellValue = sdf.format(date);
            } else {
                DecimalFormat df = new DecimalFormat("#.##");
                cellValue = df.format(hssfCell.getNumericCellValue());
                String strArr = cellValue.substring(cellValue.lastIndexOf(POINT) + 1, cellValue.length());
                if (strArr.equals("00")) {
                    cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
                }
            }
            return cellValue;
        } else {
            return String.valueOf(hssfCell.getStringCellValue());
        }
    }

    /**
     * 单元格格式
     * @param xssfCell
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getXValue(XSSFCell xssfCell){
         if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
             return String.valueOf(xssfCell.getBooleanCellValue());
         } else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
             String cellValue = "";
                 DecimalFormat df = new DecimalFormat("#.##");
                 cellValue = df.format(xssfCell.getNumericCellValue());
                 String strArr = cellValue.substring(cellValue.lastIndexOf(POINT)+1,cellValue.length());
                 if(strArr.equals("00")){
                     cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));
                 }    
             return cellValue;
         } else {
            return String.valueOf(xssfCell.getStringCellValue());
         }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值