码迷,mamicode.com
首页 > 编程语言 > 详细

Java导入excel

时间:2020-07-07 13:23:37      阅读:63      评论:0      收藏:0      [点我收藏+]

标签: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<T> {
    /** 总行数 */
    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<List<String>>
     */
    private List<List<String>> read(Workbook wb) {
        List<List<String>> dataLst = new ArrayList<List<String>>();
        /** 得到第一个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<String> rowLst = new ArrayList<String>();
            /** 循环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<Map<String, String>> importExcel(InputStream is) throws Exception {
        List<Map<String, String>> dataList = new ArrayList<>();
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(is);
            List<List<String>> read = this.read(workbook);
            if (read != null) {
                for (int i = 1; i < read.size(); i++) {
                    Map<String, String> map = new HashMap<>();
                    for (int j = 0; j < read.get(i).size(); 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!