java读取excle ,读取列名、读取第一行

1.读取整个excle列名字

try {
			//构建Workbook对象, 只读Workbook对象
			  //直接从本地文件创建Workbook
	        //从输入流创建Workbook
	        InputStream is = new FileInputStream(url);
	        Workbook rwb;
			rwb = Workbook.getWorkbook(is);
			 //获得工作薄(Workbook)中工作表(Sheet)的个数
	        int sheetNum = rwb.getNumberOfSheets();
	        //String sheetName = rwb.getSheet(0);.getName();
	                  
	        //获取第一张Sheet表
	        Sheet rs = rwb.getSheet(0);
	        //获取第一行,第一列的值
//	      Cell c00 = rs.getCell(0, 0); //getCell(列,行)
//	      String strc00 = c00.getContents();
	                     
	        System.out.println("工作薄中Sheet的个数: "+ sheetNum);
	                     
	        //输出sheet名
	        System.out.print("sheet :");
	        for(int i=0;i<sheetNum;i++){
	            System.out.print(rwb.getSheet(i).getName()+" ; ");
	        }System.out.println();

	        //列总数
	        int rsColumns = rs.getColumns();
	        //行总数
	        int rsRows =rs.getRows();
	                     
	        //输出表
	        for(int i =0;i<rsRows;i++){
	            for(int j=0 ;j<rsColumns;j++){
	                System.out.print(rs.getCell(j, i).getContents()+"  ");
	            }
	            System.out.println();
	        }
	 
	        //生成SQL
	        String colName="";
	        for(int i=0 ;i<rsColumns;i++){
	            if(i==rsColumns-1){
	                colName=colName+rs.getCell(i, 0).getContents().toUpperCase();//小写转大写
	            }else{
	                colName=colName+rs.getCell(i, 0).getContents().toUpperCase()+",";//小写转大写
	            }
	        }
	        System.out.println("colName :"+colName);
} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 2.读取excle内容第一行

try {
			//构建Workbook对象, 只读Workbook对象
			  //直接从本地文件创建Workbook
	        //从输入流创建Workbook
	        InputStream is = new FileInputStream(url);
	        Workbook rwb;
			rwb = Workbook.getWorkbook(is);
			 //获得工作薄(Workbook)中工作表(Sheet)的个数
	        int sheetNum = rwb.getNumberOfSheets();
	        //String sheetName = rwb.getSheet(0);.getName();
	                  
	        //获取第一张Sheet表
	        Sheet rs = rwb.getSheet(0);
	        //获取第一行,第一列的值
//	      Cell c00 = rs.getCell(0, 0); //getCell(列,行)
//	      String strc00 = c00.getContents();
	                     
	        System.out.println("工作薄中Sheet的个数: "+ sheetNum);
	                     
	        //输出sheet名
	        System.out.print("sheet :");
	        for(int i=0;i<sheetNum;i++){
	            System.out.print(rwb.getSheet(i).getName()+" ; ");
	        }System.out.println();

	        //列总数
	        int rsColumns = rs.getColumns();
	        //行总数
	        int rsRows =rs.getRows();
	                     
	        //输出表
	        for(int i =0;i<rsRows;i++){
	            for(int j=0 ;j<rsColumns;j++){
	                System.out.print(rs.getCell(j, i).getContents()+"  ");
	            }
	            System.out.println();
	        }	



//获取第2行的值,第1行列名
//            for(int i=0 ;i<rsRows-1 ;i++){
               int ii =0;
                for(int j=0 ;j<rsColumns;j++){
                    if(j==rsColumns-1){
                    	rsName=rsName+"\'"+rs.getCell(j,ii+1).getContents()+"\'";
                    }else{
                    	rsName=rsName+"\'"+rs.getCell(j,ii+1).getContents()+"\'"+",";
                    }
                }
//            }
} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

 

你可能感兴趣的:(java)