Java导入excel

2021-04-25 04:27

阅读:735

标签:set   public   cell   循环   ace   info   类型   返回   tst   

package cn.deacy.work.util;

import org.apache.poi.hssf.usermodel.HSSFCell;
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 java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Excel相关处理
 */
public class ExcelUtil {
    /** 总行数 */
    private int totalRows = 0;
    /** 总列数 */
    private int totalCells = 0;
    /** 错误信息 */
    private String errorInfo;

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public void setTotalCells(int totalCells) {
        this.totalCells = totalCells;
    }

    public String getErrorInfo() {
        return errorInfo;
    }

    public void setErrorInfo(String errorInfo) {
        this.errorInfo = errorInfo;
    }

    /**
     * @描述:读取数据
     * @时间:2014-08-29 下午16:50:15
     * @参数:@param Workbook
     * @参数:@return
     * @返回值:List>
     */
    private List> read(Workbook wb) {
        List> dataLst = new ArrayList>();
        /** 得到第一个shell */
        Sheet sheet = wb.getSheetAt(0);
        /** 得到Excel的行数 */
        this.totalRows = sheet.getPhysicalNumberOfRows();
        /** 得到Excel的列数 */
        if (this.totalRows >= 1 && sheet.getRow(0) != null) {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
        }
        /** 循环Excel的行 */
        for (int r = 0; r this.totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }
            List rowLst = new ArrayList();
            /** 循环Excel的列 */
            for (int c = 0; c this.getTotalCells(); c++) {
                Cell cell = row.getCell(c);
                String cellValue = "";
                if (null != cell) {
                    // 以下是判断数据的类型
                    switch (cell.getCellType()) {
                        case HSSFCell.CELL_TYPE_NUMERIC: // 数字
                            DecimalFormat df = new DecimalFormat("0");
                            cellValue = df.format(cell.getNumericCellValue());
                            break;
                        case HSSFCell.CELL_TYPE_STRING: // 字符串
                            cellValue = cell.getStringCellValue();
                            break;
                        case HSSFCell.CELL_TYPE_BOOLEAN: // Boolean
                            cellValue = cell.getBooleanCellValue() + "";
                            break;
                        case HSSFCell.CELL_TYPE_FORMULA: // 公式
                            cellValue = cell.getCellFormula() + "";
                            break;
                        case HSSFCell.CELL_TYPE_BLANK: // 空值
                            cellValue = "";
                            break;
                        case HSSFCell.CELL_TYPE_ERROR: // 故障
                            cellValue = "非法字符";
                            break;
                        default:
                            cellValue = "未知类型";
                            break;
                    }
                }
                rowLst.add(cellValue);
            }
            /** 保存第r行的第c列 */
            dataLst.add(rowLst);
        }
        return dataLst;
    }

    /**
     * @描述:得到总列数
     * @时间:2014-08-29 下午16:27:15
     * @参数:@return
     * @返回值:int
     */
    public int getTotalCells() {
        return totalCells;
    }

    public List> importExcel(InputStream is) throws Exception {
        List> dataList = new ArrayList();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(is);
            List> read = this.read(workbook);
            if (read != null) {
                for (int i = 1; i ) {
                    Map map = new HashMap();
                    for (int j = 0; j ) {
                        map.put(read.get(0).get(j),read.get(i).get(j));
                    }
                    dataList.add(map);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataList;
    }

}

 

Java导入excel

标签:set   public   cell   循环   ace   info   类型   返回   tst   

原文地址:https://www.cnblogs.com/Jack-zhao/p/13259828.html


评论


亲,登录后才可以留言!