加入两个依赖
<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();} } }