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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)
- ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。