java对Excle进行操作。A表有id,name,B表有id,age。A表里的id比B表的多,但是B表的idA表都有,想把A表的id与B表的id比对,一样的将A表的name写入到B表对应的id行

java对Excle进行操作。A表有id,name,B表有id,age。A表里的id比B表的多,但是B表的idA表都有,想把A表的id与B表的id比对,一样的将A表的name写入到B表对应的id行

对比excel:

public class RWExcel {

    private String filePath;
    private String anotherfilePath;


    /**
     * 构造方法
     * */

    public RWExcel(String filePath,String anotherfilePath){

        this.filePath = filePath;
        this.anotherfilePath = anotherfilePath;
    }

    /**
     * 
     * 读取excel 封装成集合
     * 该程序需要传入一份excel 和excel的列数 行数由程序自动检测
     * 注意:该方法统计的行数是默认第一行为title 不纳入统计的
     * 
     * @return
     * 
     */
    // @Test
    public ArrayList<List> ReadExcel(int sheetNum) {

        // int column = 5;//column表示excel的列数

        ArrayList<List> list = new ArrayList<List>();

        try {
            // 建需要读取的excel文件写入stream
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
            // 指向sheet下标为0的sheet 即第一个sheet 也可以按在sheet的名称来寻找
            HSSFSheet sheet = workbook.getSheetAt(sheetNum);
            // 获取sheet1中的总行数
            int rowTotalCount = sheet.getLastRowNum();
            //获取总列数
            int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();

            //System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);

            for (int i = 0; i <= rowTotalCount; i++) {
                // 获取第i列的row对象
                HSSFRow row = sheet.getRow(i);

                ArrayList<String> listRow = new ArrayList<String>();

                for (int j = 0; j < columnCount; j++) {
                    //下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错
                    String cell = null;
                    //如果未null则加上""组装成非null的字符串
                    if(row.getCell(j) == null){

                        cell = row.getCell(j)+"";

                        listRow.add(cell);
                    //如果读取到额cell不为null 则直接加入  listRow集合
                    }else{
                        cell = row.getCell(j).toString();
                        listRow.add(cell);
                    }
                    // 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出
                    //System.out.printf("%15s", cell);

                }

                list.add(listRow);

                //System.out.println();
            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return list;
    }

    /**
     * 读取另外一份Excel 保存成list集合
     * */

    public ArrayList<List> ReadAnotherExcel(int anotherSheetNum){


        ArrayList<List> list = new ArrayList<List>();

        try {
            // 建需要读取的excel文件写入stream
            HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(anotherfilePath));
            // 指向sheet下标为0的sheet 即第一个sheet 也可以按在sheet的名称来寻找
            HSSFSheet sheet = workbook.getSheetAt(anotherSheetNum);
            // 获取sheet1中的总行数
            int rowTotalCount = sheet.getLastRowNum();
            //获取总列数
            int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();

            //System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);

            for (int i = 0; i <= rowTotalCount; i++) {
                // 获取第i列的row对象
                HSSFRow row = sheet.getRow(i);

                ArrayList<String> listRow = new ArrayList<String>();

                for (int j = 0; j < columnCount; j++) {
                    //下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错
                    String cell = null;
                    //如果未null则加上""组装成非null的字符串
                    if(row.getCell(j) == null){

                        cell = row.getCell(j)+"";

                        listRow.add(cell);
                    //如果读取到额cell不为null 则直接加入  listRow集合
                    }else{
                        cell = row.getCell(j).toString();
                        listRow.add(cell);
                    }
                    // 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出
                    //System.out.printf("%15s", cell);

                }

                list.add(listRow);

                //System.out.println();
            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return list;
    }


    /**
     * 调试方法
     * */

//  public static void main(String[] args) {
//
//       RWExcel excel = new RWExcel("D:\\345.xls", "D:\\345.xls");
//       
//       ArrayList<List> list1 = excel.ReadExcel(0);
//
//       ArrayList<List> list2 = excel.ReadAnotherExcel(1);
//       
//       for (List list : list1) {
//          
//           System.out.println(list.toString());
//      }
//       
//       System.out.println("==========================");
//       
//       for (List list : list2) {
//              
//           System.out.println(list.toString());
//      }
//  }
}

https://blog.csdn.net/hujyhfwfh2/article/details/81082326

你完善一下

我第一个想到的是用poi挨个比对,不过不知道数据量的大小,所以你可以整两张表出来,最后用SQL来实现,在用poi导出想要的表格

不知道你的具体问题是什么,大概步骤如下:

1.将A表的数据读到map,map.put(id,name)
2.将B表的数据读到list, list.add(id)
3.对B表进行写入,通过list逐行写入id列,直接从A表中读取映射值,如果没有就原样,sheet.addCell(new Label(column, i, map.get(id) == null ? id : map.get(id), format))

循环读出2个表的id列,一个判断,一个写入

涉及的知识点:
1、读写excel
2、循环和循环嵌套
3、判断

应该都很基础吧