zoukankan      html  css  js  c++  java
  • EXCEL(POI)导入导出工具类

     1 /**
     2  * 
     3  * TODO: POI工具类
     4  * 不支持07版本
     5  * @author zyl
     6  * @date 2018年11月5日
     7  */
     8 public class POIUtils {
     9     
    10     /**
    11      * 
    12      * TODO: 从输入流中读取excel的数据
    13      *
    14      * @param in 输入流
    15      * @param columnLength 需要读取excel的列的数量
    16      * @param columnStart excel从哪一行开始读 0开始
    17      * @param sheetPage excel sheet  0开始
    18      * @return excel中对应的列的数据集合
    19      */
    20     public static List<String[]> getExcelData(InputStream in, int columnLength, int columnStart,int sheetPage){
    21         if(in == null) return null;
    22         try{
    23             return getExcelData(new HSSFWorkbook(in), columnLength, columnStart,sheetPage);
    24         }catch(Exception ex){
    25             throw new RuntimeException("读取excel列的数据出现异常,错误信息:"+ ex);
    26         }
    27     }
    28     
    29     /**
    30      * 
    31      * TODO: 从输入流中读取excel的数据
    32      *
    33      * @param wb poi中excel对象
    34      * @param columnLength 需要读取excel的列的数量
    35      * @param columnHeader excel是否有列头
    36      * @return excel中对应的列的数据集合
    37      */
    38     private static List<String[]> getExcelData(Workbook wb, int columnLength, int columnStart,int sheetPage){
    39         if(wb== null || columnLength<= 0){
    40             return null;
    41         }
    42         List<String[]> data= new ArrayList<String[]>();
    43         Sheet sheet= wb.getSheetAt(sheetPage);
    44         
    45         Row row= sheet.getRow(columnStart);
    46         while(row != null){
    47             String[] rowStringArray= new String[columnLength];
    48             for(int i=0; i< columnLength; i++){
    49                 Cell cell= row.getCell(i);
    50                 if(cell != null){
    51                     if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
    52                         rowStringArray[i]= String.valueOf(Double.valueOf(cell.getNumericCellValue()).intValue());
    53                     else if(cell.getCellType() == Cell.CELL_TYPE_STRING)
    54                         rowStringArray[i]= String.valueOf(cell.getStringCellValue());
    55                     else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN)
    56                         rowStringArray[i]= String.valueOf(cell.getBooleanCellValue());
    57                     else if(cell.getCellType() == Cell.CELL_TYPE_ERROR)
    58                         rowStringArray[i]= String.valueOf(cell.getErrorCellValue());
    59                     else
    60                         rowStringArray[i]= String.valueOf("");
    61                 }else{
    62                     rowStringArray[i]= "";
    63                 }
    64             }
    65             data.add(rowStringArray);
    66             columnStart++;
    67             row= sheet.getRow(columnStart);
    68         }
    69         return data;
    70     }
    71 }
  • 相关阅读:
    泛型自限定
    java 泛型类转型
    迷茫
    spring boot 连接mysql 错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one
    mysql8.0.14 安装
    泛型通配符
    java 泛型数组
    java FTP上传文件
    spring location设置本地路径
    套接口定义
  • 原文地址:https://www.cnblogs.com/10fly/p/9908617.html
Copyright © 2011-2022 走看看