简易的读取excel,临时用于读个文件啥的。

需要引得jar
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
//这个是我的dao层,ComplaintAnalysisMapper,因为需要存储数据库,所以传入了一个dao
package com.company.project.common.utils;

import com.company.project.entity.ComplaintAnalysisEntity;
import com.company.project.mapper.ComplaintAnalysisMapper;
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;

public class ExcelData {

    private XSSFSheet sheet;

    /**
     * 构造函数,初始化excel数据
     *
     * @param filePath  excel路径
     * @param sheetName sheet表名
     */
    public ExcelData(String filePath, String sheetName) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
            //获取sheet
//            sheet = sheets.getSheet(sheetName); //根据名称获取sheet
            sheet = sheets.getSheetAt(0);//根据坐标获取sheet
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据行和列的索引获取单元格的数据
     *
     * @param row
     * @param column
     * @return
     */
    public String getExcelDateByIndex(int row, int column) {
        XSSFRow row1 = sheet.getRow(row);
        String cell = row1.getCell(column).toString();
        return cell;
    }

    /**
     * 根据某一列值为“******”的这一行,来获取该行第x列的值
     *
     * @param caseName
     * @param currentColumn 当前单元格列的索引
     * @param targetColumn  目标单元格列的索引
     * @return
     */
    public String getCellByCaseName(String caseName, int currentColumn, int targetColumn) {
        String operateSteps = "";
        //获取行数
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 0; i < rows; i++) {
            XSSFRow row = sheet.getRow(i);
            String cell = row.getCell(currentColumn).toString();
            if (cell.equals(caseName)) {
                operateSteps = row.getCell(targetColumn).toString();
                break;
            }
        }
        return operateSteps;
    }

    //打印excel数据
    public void readExcelData(ComplaintAnalysisMapper complaintAnalysisMapper) {
        //获取行数
        int rows = sheet.getPhysicalNumberOfRows();
        for (int i = 1; i < rows; i++) {
            //获取列数
            XSSFRow row = sheet.getRow(i);
            int columns = row.getPhysicalNumberOfCells();
            ComplaintAnalysisEntity complaintAnalysisEntity = new ComplaintAnalysisEntity();
            String date = row.getCell(0).toString();
            String[] dates = date.split("-");
            String dateNew1 = dates[2];
            if (dates[1].equals("一月")) {
                dateNew1 = dateNew1 + "-01-";
            } else if (dates[1].equals("二月")) {
                dateNew1 = dateNew1 + "-02-";
            } else if (dates[1].equals("三月")) {
                dateNew1 = dateNew1 + "-03-";
            } else if (dates[1].equals("四月")) {
                dateNew1 = dateNew1 + "-04-";
            } else if (dates[1].equals("五月")) {
                dateNew1 = dateNew1 + "-05-";
            } else if (dates[1].equals("六月")) {
                dateNew1 = dateNew1 + "-06-";
            } else if (dates[1].equals("七月")) {
                dateNew1 = dateNew1 + "-07-";
            } else if (dates[1].equals("八月")) {
                dateNew1 = dateNew1 + "-08-";
            } else if (dates[1].equals("九月")) {
                dateNew1 = dateNew1 + "-09-";
            } else if (dates[1].equals("十月")) {
                dateNew1 = dateNew1 + "-10-";
            } else if (dates[1].equals("十一月")) {
                dateNew1 = dateNew1 + "-11-";
            } else if (dates[1].equals("十二月")) {
                dateNew1 = dateNew1 + "-12-";
            }
            dateNew1 = dateNew1 + dates[0];
            complaintAnalysisEntity.setCreateTime(dateNew1);
            complaintAnalysisEntity.setPhone(row.getCell(1).toString());
            complaintAnalysisEntity.setCity(row.getCell(2).toString());
            complaintAnalysisEntity.setAddress(row.getCell(3).toString());
            complaintAnalysisEntity.setScene(row.getCell(4).toString());
            complaintAnalysisEntity.setProblemType(row.getCell(5).toString());
            complaintAnalysisEntity.setResolved(row.getCell(6).toString());
            complaintAnalysisEntity.setReason(row.getCell(7).toString());
            complaintAnalysisEntity.setProgramme(row.getCell(8).toString());
            complaintAnalysisMapper.insert(complaintAnalysisEntity);
        }
    }

    //测试方法
    public static void main(String[] args) {
        //读取sheet
        ExcelData sheet1 = new ExcelData("D:\\tousu.xlsx", "username");
//        //获取第二行第4列
//        String cell2 = sheet1.getExcelDateByIndex(1, 3);
//        //根据第3列值为“customer23”的这一行,来获取该行第2列的值
//        String cell3 = sheet1.getCellByCaseName("customer23", 2, 1);
//        System.out.println(cell2);
//        System.out.println(cell3);
//数据已经都存到sheet种,调用这个方法即可看到所有的row
        sheet1.readExcelData();
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值