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行
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- threenewbee 2019-10-21 09:57关注
对比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()); // } // } }
你完善一下
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 对于相关问题的求解与代码
- ¥15 ubuntu子系统密码忘记
- ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作
- ¥15 求NPF226060磁芯的详细资料
- ¥15 使用R语言marginaleffects包进行边际效应图绘制
- ¥20 usb设备兼容性问题
- ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
- ¥15 安装svn网络有问题怎么办