java读取excel,比较两个sheet数据

加入两个依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>

 

然后直接操作即可:

public class Exe {

    private  int totalRows = 0;
    //总条数
    private  int totalCells = 0;

    private  int totalRows1 = 0;
    //总条数
    private  int totalCells1 = 0;

    public static void main(String[] args) throws  Exception{
        Workbook wb = null;
        wb = new XSSFWorkbook(new FileInputStream("D:\\20190115-19.xlsx"));
        File f = new File("D:\\test.txt");
        Exe exe = new Exe();
        exe.readExcelValue(wb);
    }

    private  void readExcelValue(Workbook wb) {  //List<AgentEquBatch>
        // 得到第二个shell
        Sheet sheet1 = wb.getSheetAt(1);
        // 得到Excel的行数
        this.totalRows1 = sheet1.getPhysicalNumberOfRows();
        System.out.println("-totalRows111111----"+totalRows1);
        // 得到Excel的列数(前提是有行数)
        if (totalRows1 > 1 && sheet1.getRow(0) != null) {
            this.totalCells1 = sheet1.getRow(0).getPhysicalNumberOfCells();
        }
        List<String>  li = new ArrayList<>();
        // 循环Excel行数
        for (int r = 1; r < totalRows1; r++) {
            Row row = sheet1.getRow(r);
            if (row == null){
                continue;
            }
            String sh = String.valueOf(row.getCell(0));
            if(!sh.equals("")){
                System.out.println("shhhhhh"+sh+"==");
                li.add(sh);
            }
        }

        // 得到第一个shell
        Sheet sheet = wb.getSheetAt(0);
        // 得到Excel的行数
        this.totalRows = sheet.getPhysicalNumberOfRows();
        System.out.println("-totalRows00000000000----"+totalRows);
        // 得到Excel的列数(前提是有行数)
        if (totalRows > 1 && sheet.getRow(0) != null) {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
            System.out.println("-totalCells--->>0000000000"+totalCells);
        }
        List<String>  shdhs= new ArrayList<>();
        List<String>  cjsjs = new ArrayList<>();
        List<String>   jes = new ArrayList<>();
        List<String>   zts  = new ArrayList<>();
         // 循环Excel行数
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            String cjsj = String.valueOf(row.getCell(0));
            String shdh = String.valueOf(row.getCell(1)) ;
            String je = String.valueOf(row.getCell(5)) ;
            String zt = String.valueOf(row.getCell(6)) ;
            cjsjs.add(cjsj);
            shdhs.add(shdh);
            jes.add(je);
            zts.add(zt);
        }

        StringBuilder st = new StringBuilder();
        for(int i=0;i<shdhs.size();i++){
           if(li.contains(shdhs.get(i))){
             st.append("创建时间:"+cjsjs.get(i)+"  ,订单号 "+shdhs.get(i)+"   ,金额"+jes.get(i)+", 状态:"+zts.get(i));
             System.out.println("创建时间:"+cjsjs.get(i)+"  ,订单号 "+shdhs.get(i)+"   ,金额"+jes.get(i)+", 状态:"+zts.get(i));
       }
   }


    try{
        PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("D:\\test.txt")));
        pw.print(st);
        pw.close();
    }catch (Exception e){e.printStackTrace();}



    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

欲将心事付瑶琴知音少弦断有谁听

欲将心事付瑶琴,知音少

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值