zoukankan      html  css  js  c++  java
  • 使用POI 导入excel

            引言:最近一直在接触excel的问题,网页也有很多关于POI解析excel的资料,我也简单的整理了下,有不对地方的还望及时指正,渴望与大家交流并学习。

    public int importExcel(File file) throws Exception {
    		FileInputStream fis = null;
    		String sheetName = "sheetname";
    		try {
    			fis = new FileInputStream(file);
    			POIFSFileSystem fs = new POIFSFileSystem(fis);
    			HSSFWorkbook wb = new HSSFWorkbook(fs);
    			HSSFSheet searchWordSheet = wb.getSheet(sheetName);
    			// sheet 空校验
    			if (searchWordSheet == null) {
    				return -2;
    			}
    			// sheet 无数据
    			if (searchWordSheet.getLastRowNum() < 1) {
    				return -1;
    			}
    			List<String []> ispList = new LinkedList<String []>();
    			//循环得到excel的每一行,然后得到每一个cell的值。
    			for (int i = 1; i <= searchWordSheet.getLastRowNum(); i++) {
    				HSSFRow row = searchWordSheet.getRow(i);
    				if (row == null) {
    					continue;
    				}
    				String [] isp = new String [3];		
    					
    				cell = row.getCell((short) 0);			
    				Long value = getNumberValueFromCell(cell);					
    				isp[0] = value.toString();
    						
    				cell = row.getCell((short) 1);
    				isp[1] = getStringValueFromCell(cell);
    			
    				cell = row.getCell((short) 2);
    				isp[2] = getStringValueFromCell(cell);
    				
    				ispList.add(isp);	
    			}
    
    			// 数据保存入库
    
    			List<String []> ispSaveList = new LinkedList<String []>();
    		
    			for (String [] isp : ispList) {
    			
    					ispSaveList.add(isp);
    						
        }
               saveIspFullInfoList(ispSaveList);	}
    public Long getNumberValueFromCell(HSSFCell cell) {
    		try {
    			if (cell != null && cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
    			return NumberUtils.isNumber(cell.getStringCellValue()) ? Long
    						.parseLong(cell.getStringCellValue()) : null;
    			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
    				return new Double(cell.getNumericCellValue()).longValue();
    			} else {
    				return null;
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    			return null;
    		}
    	}
    }


     

    public String getStringValueFromCell(HSSFCell cell) {
    		if (isNullForCellValue(cell)) {
    			return null;
    		} else {
    			if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
    				return cell.getStringCellValue();
    			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
    				return new Double(cell.getNumericCellValue()).longValue()+"";
    			} else {
    				return null;
    			}
    		}
    	}
    
    public Object getSourceValueFromCell(HSSFCell cell) {
    		if (isNullForCellValue(cell)) {
    			return null;
    		} else {
    			if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
    				return cell.getStringCellValue();
    			} else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
    				return new Double(cell.getNumericCellValue()).longValue();
    			} else {
    				return null;
    			}
    		}
    	}

      以上三块代码是获得不同类型的cell的值

    部分代码解释:   saveIspFullInfoList(ispSaveList);将从Excel得到的数据保存到数据库中

                                HSSFSheet searchWordSheet = wb.getSheet(sheetName);通过sheet的名称得到对于的sheet页(一个excel文件可能有多个sheet页)。

                                POIFSFileSystem fs = new POIFSFileSystem(fis); 

                                HSSFWorkbook wb = new HSSFWorkbook(fs);

                                这块也就是通过文件得到HSSFWorkbook,在之前的文章中也说过,不同版本的excel需要不同的方式来解析。所以也可以采用之前文章中的方式来进行对于的解析。代码如下:

    01.public static Workbook getWorkbook(InputStream is) throws IOException{    
    02.      Workbook wb = null;    
    03.              if(!is.markSupported()){    
    04.                  is = new PushbackInputStream(is,8);             
    05.              }    
    06.              if (POIFSFileSystem.hasPOIFSHeader(is)) {       //Excel2003及以下版本    
    07.                  wb = (Workbook) new HSSFWorkbook(is);    
    08.              }else if (POIXMLDocument.hasOOXMLHeader(is)) {      //Excel2007及以上版本    
    09.                  wb = new XSSFWorkbook(is);      
    10.              }else{    
    11.                  throw new IllegalArgumentException("你的Excel版本目前poi无法解析!");                      
    12.              }    
    13.        //  }    
    14.      return wb;    
    15.  }   
    

    导入所需的包可以在这里下载http://download.csdn.net/detail/javaweiming/5849101.
     


     

  • 相关阅读:
    【总】IdentityServer4 32篇汇总
    ASP.NET Core的身份认证框架IdentityServer4(3)-术语的解释
    ASP.NET Core身份认证服务框架IdentityServer4(2)-整体介绍
    dentityServer4(1)- 特性一览
    Identity和IdentityServer的区别及联系
    if (user?.Identity?.IsAuthenticated ?? false)这几个问号分别都代表啥意思?
    ASP.NET Core 认证与授权[1]:初识认证
    如何优化Python占用的内存,面试必学
    Python3的这些新特性很方便,新手必学
    Python常见面试题,80%的人不会
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3231151.html
Copyright © 2011-2022 走看看