• Java连接excel实现:通过姓名查找id和通过id查找姓名


    注意每个方法结束都要关闭workbook;

    还有getIdbyname()方法中字符串flag与name的比较,一定要用equals()方法!!!;

    剩下的不多解释,注释都在代码中:

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellType;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    
    public class DnsTest {
        public static void main(String[] args) throws IOException{
            getIdbyname("株洲");
            getNamebyid(6) ;  
        }
        public static void getIdbyname(String name)throws IOException  {
            //通过name获得id    
            String filePath="D://dns.xls";
             InputStream input = new FileInputStream(filePath);
                Workbook wb = null;
                wb = new HSSFWorkbook(input);
                //得到一个工作表对象;
                Sheet sheet = wb.getSheetAt(0);
                int rsRows = sheet.getLastRowNum();// 获取sheet表中的总行数            
                 // 遍历行
            for (int i=0;i<=rsRows;i++) {
                System.out.println("遍历行数"+i);
                Row row = sheet.getRow(i);
                int id=0;
                String flag=null;
                //遍历行单元格,已知有两列;第一列int型id,第二列String型name
                    Cell cell1 = row.getCell(0);
                    Cell cell2 = row.getCell(1);
                    if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){
                        System.out.println("id为空");
                        break;
                    }else {
                        //数值型
                        id=(int) cell1.getNumericCellValue();
                        
                    };
                    if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){
                        System.out.println("name为空");
                         break;
                    }else {
                        //字符串型
                        flag= cell2.getStringCellValue();
                    };    
                    String a=new String(flag); 
                    String b=new String(name);
                    if(a.equals(b)){
                        System.out.println(id);
                    };
            }                    
            wb.close();//记得关闭
        }
    
        public static void getNamebyid(int id) throws IOException {
            //通过id获得name
    
                    String filePath="D://dns.xls";
                    InputStream input = new FileInputStream(filePath);
                    Workbook wb = null;
                    wb = new HSSFWorkbook(input);
                    //得到一个工作表对象;
                    Sheet sheet = wb.getSheetAt(0);
                    int rsRows = sheet.getLastRowNum();// 获取sheet表中的总行数            
                     // 遍历行
                    for (int i=0;i<=rsRows;i++) {
                        int flag=0;
                        String name=null;
                        Row row = sheet.getRow(i);
                        //遍历行单元格
                        Cell cell1= row.getCell(0);
                        Cell cell2 = row.getCell(1);
                        if(cell1==null||cell1.equals(null)||cell1.getCellType()==CellType.BLANK){
                            break;
                        }else {
                            //数值型
                            flag=(int) cell1.getNumericCellValue();
                            
                        }
                        if(cell2==null||cell2.equals(null)||cell2.getCellType()==CellType.BLANK){
                             break;
                        }else {
                            //字符串型
                            name= cell2.getStringCellValue();
                        }
                        if(flag==id){
                            System.out.println(name);
                        }
                    }                    
                    wb.close();//记得关闭    
        }
    }
  • 相关阅读:
    java中的几种对象(PO,VO,DAO,BO,POJO)
    【转】Spring boot 打成jar包问题总结
    mac 上安装lua
    Mac下更新Vim到最新版本
    刘以鬯和香港文学
    权重随机算法的java实现
    MySQL具体解释(7)-----------MySQL线程池总结(一)
    IIS PHP 配置 问题总结
    HDU 3622 Bomb Game(2-sat)
    poj 2388 Who&#39;s in the Middle
  • 原文地址:https://www.cnblogs.com/sengzhao666/p/11147976.html
Copyright © 2020-2023  润新知