EXCEL(POI)导入导出工具类

本文介绍了一个用于读取Excel数据的POI工具类,该类能够从输入流中读取指定列数和起始位置的数据,适用于处理Excel表格文件。提供了详细的代码实现,包括异常处理和数据类型转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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 }

 

转载于:https://www.cnblogs.com/10fly/p/9908617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值