Java POI 导入Excel以及解析Excel

用的SSM框架,所需要的jar包如图所示:,链接地址:jar包下载  ,下面直接上代码。1、ExcelUtil工具类[java] view plain copy print?"color:#993399;">"font-size:18px;color:#993399;">import java.text.DecimalFormat
摘要由CSDN通过智能技术生成

用的SSM框架,所需要的jar包如图所示:,链接地址:jar包下载  ,下面直接上代码。

1、ExcelUtil工具类

[java]  view plain   copy
  print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="color:#993399;"><span style="font-size:18px;color:#993399;">import java.text.DecimalFormat;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.Date;  
  5. import java.util.regex.Matcher;  
  6. import java.util.regex.Pattern;  
  7.   
  8. import org.apache.poi.hssf.usermodel.HSSFCell;  
  9. import org.apache.poi.hssf.usermodel.HSSFDateUtil;  
  10. import org.apache.poi.ss.usermodel.Cell;  
  11. import org.apache.poi.ss.usermodel.DateUtil;  
  12. import org.apache.poi.xssf.usermodel.XSSFCell;  
  13.   
  14. /** 
  15.  * Excel工具类 
  16.  * @author lp 
  17.  * 
  18.  */  
  19. public class ExcelUtil {  
  20.     public static final String OFFICE_EXCEL_2003_POSTFIX = "xls";  
  21.     public static final String OFFICE_EXCEL_2010_POSTFIX = "xlsx";  
  22.     public static final String EMPTY = "";  
  23.     public static final String POINT = ".";  
  24.     public static SimpleDateFormat sdf =   new SimpleDateFormat("yyyy/MM/dd");  
  25.     /** 
  26.      * 获得path的后缀名 
  27.      * @param path 
  28.      * @return 
  29.      */  
  30.     public static String getPostfix(String path){  
  31.         if(path==null || EMPTY.equals(path.trim())){  
  32.             return EMPTY;  
  33.         }  
  34.         if(path.contains(POINT)){  
  35.             return path.substring(path.lastIndexOf(POINT)+1,path.length());  
  36.         }  
  37.         return EMPTY;  
  38.     }  
  39.     /** 
  40.      * 单元格格式 
  41.      * @param hssfCell 
  42.      * @return 
  43.      */  
  44.     @SuppressWarnings({ "static-access""deprecation" })  
  45.     public static String getHValue(HSSFCell hssfCell){  
  46.          if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {  
  47.              return String.valueOf(hssfCell.getBooleanCellValue());  
  48.          } else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {  
  49.              String cellValue = "";  
  50.              if(HSSFDateUtil.isCellDateFormatted(hssfCell)){                  
  51.                  Date date = HSSFDateUtil.getJavaDate(hssfCell.getNumericCellValue());  
  52.                  cellValue = sdf.format(date);  
  53.              }else{  
  54.                  DecimalFormat df = new DecimalFormat("#.##");  
  55.                  cellValue = df.format(hssfCell.getNumericCellValue());  
  56.                  String strArr = cellValue.substring(cellValue.lastIndexOf(POINT)+1,cellValue.length());  
  57.                  if(strArr.equals("00")){  
  58.                      cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));  
  59.                  }    
  60.              }  
  61.              return cellValue;  
  62.          } else {  
  63.             return String.valueOf(hssfCell.getStringCellValue());  
  64.          }  
  65.     }  
  66.     /** 
  67.      * 单元格格式 
  68.      * @param xssfCell 
  69.      * @return 
  70.      */  
  71.     public static String getXValue(XSSFCell xssfCell){  
  72.          if (xssfCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {  
  73.              return String.valueOf(xssfCell.getBooleanCellValue());  
  74.          } else if (xssfCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {  
  75.              String cellValue = "";  
  76.              if(XSSFDateUtil.isCellDateFormatted(xssfCell)){  
  77.                  Date date = XSSFDateUtil.getJavaDate(xssfCell.getNumericCellValue());  
  78.                  cellValue = sdf.format(date);  
  79.              }else{  
  80.                  DecimalFormat df = new DecimalFormat("#.##");  
  81.                  cellValue = df.format(xssfCell.getNumericCellValue());  
  82.                  String strArr = cellValue.substring(cellValue.lastIndexOf(POINT)+1,cellValue.length());  
  83.                  if(strArr.equals("00")){  
  84.                      cellValue = cellValue.substring(0, cellValue.lastIndexOf(POINT));  
  85.                  }    
  86.              }  
  87.              return cellValue;  
  88.          } else {  
  89.             return String.valueOf(xssfCell.getStringCellValue());  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值