zoukankan      html  css  js  c++  java
  • Java导入excel

    1.excel工具类ExcelUtil.java

    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;
        }
    
    }

    2.web层调用,或者写入到serviceimpl

    @RequestMapping("importData")
        public BaseResult importData(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
            BaseResult result = tWorkUcsaInfoService.importExcel(file,request);
          try {
    if (file != null) {
    FileInputStream inputStream = (FileInputStream) file.getInputStream();
    ExcelUtil excelUtil = new ExcelUtil();
    //获取到excel里面的数据
    List<Map<String,String>> list = excelUtil.importExcel(inputStream);

    TWorkUcsaInfoExample workUcsaInfoExample = new TWorkUcsaInfoExample();
    List<TWorkUcsaInfo> example = tWorkUcsaInfoMapper.selectByExample(workUcsaInfoExample);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    for (int i = 0; i < list.size(); i++) {
    Map<String, String> map = list.get(i);
    for (int j = 0; j <example.size() ; j++) {

    }
    TWorkUcsaInfo ucsaInfo = new TWorkUcsaInfo();
    ucsaInfo.setXm(map.get("姓名"));
    ucsaInfo.setSfzh(map.get("身份证号"));
    ucsaInfo.setGsmc(map.get("公司名称"));
    tWorkUcsaInfoService.insert(ucsaInfo);
    }


    }
    } catch (Exception e) {
    e.printStackTrace();
    result.setMessage("失败");
    }
        return result; 
    }
  • 相关阅读:
    树的基本概念
    bean的生命周期
    bean的创建过程--doCreateBean
    bean的创建过程--doGetBean
    SpringBoot自动装配解析
    [论文理解] Good Semi-supervised Learning That Requires a Bad GAN
    Ubuntu 环境安装 opencv 3.2 步骤和问题记录
    Linux 环境使用 lsof 命令查询端口占用
    Ubuntu 安装不同版本的 gcc/g++ 编译器
    [持续更新] 安全能力成长计划
  • 原文地址:https://www.cnblogs.com/Jack-zhao/p/13259828.html
Copyright © 2011-2022 走看看