zoukankan      html  css  js  c++  java
  • 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();//记得关闭    
        }
    }
  • 相关阅读:
    php实现多张图片打包下载
    bootstrap的简单学习
    接口的简单调用模式
    oracle 日期常用函數 (SYSDATE、日期格式)
    php通过ajax请求数据后台返回数据要求和前台解析
    jquery+ajax实现分页
    Fatal error: Call to undefined function curl_init()问题
    利用php比较精确的统计在线人数的办法
    sql服务器启动不了问题
    字符串字母大小写转换(10)
  • 原文地址:https://www.cnblogs.com/sengzhao666/p/11147976.html
Copyright © 2011-2022 走看看