java 用 jxl poi 进行excel 解析 *** 最爱那水货

  1     /**
  2      * 解析excel文件 ,并把数据放入数组中  格式 xlsx xls
  3      * @param path 从ftp上下载到本地的文件的路径
  4      * @return 数据数组集合
  5      */
  6     public List<String[]> readExcelPublic(String path){
  7         List<String[]> list = new ArrayList<String[]>();
  8             log.info("开始解析"+path.substring(path.lastIndexOf("\")+1)+"文件");
  9             
 10             try {
 11                 FileInputStream is = new FileInputStream(path); //文件流  
 12                 Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
 13                 Sheet sheet = workbook.getSheetAt(0);//得到excel第一页的内容
 14                 for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
 15                     //循环行数
 16                     Row row = sheet.getRow(i);
 17                     //获取当前行的列长度
 18                     int cols_length = row.getPhysicalNumberOfCells();
 19                     //设置当前数组长度 5
 20                     String[] str = new String[cols_length];
 21                     for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {  
 22                         Cell cell = row.getCell(j);  
 23                         str[j] = this.getValue(cell);
 24                     }  
 25                     list.add(str);
 26                 }
 27                 log.info("文件"+path.substring(path.lastIndexOf("\")+1)+"解析完毕!");
 28             } catch (FileNotFoundException e) {
 29                 log.error("!!!!!!! 未找到文件!文件为: 【"+path+"】");
 30                 e.printStackTrace();
 31             } catch (InvalidFormatException e) {
 32                 log.error("!!!!!!! 解析文件出错!文件为: 【"+path+"】");
 33                 e.printStackTrace();
 34             } catch (IOException e) {
 35                 log.error("!!!!!!! 解析文件出错!文件为: 【"+path+"】");
 36                 e.printStackTrace();
 37             }
 38         return list;
 39     }
 40     
 41     
 42     /**
 43      * 解析excel 文件<br>
 44      *   格式为:xls
 45      * @param path 文件的路径
 46      * @return List<String[]>
 47      */
 48     public List<String[]> xlsReadExcel(String path){
 49         List<String[]> list = new ArrayList<String[]>();
 50             log.info("开始解析"+path.substring(path.lastIndexOf("\")+1)+"文件");
 51             try {
 52                 FileInputStream is = new FileInputStream(path); //文件流  
 53                 HSSFWorkbook workbook = new HSSFWorkbook(is);   
 54                 HSSFSheet  sheet = workbook.getSheetAt(0);//得到excel第一页的内容
 55                 for (int i = 0; i < sheet.getLastRowNum(); i++) {
 56                     //循环行数
 57                     HSSFRow  row = sheet.getRow(i);
 58                     //获取当前行的列长度
 59                     int cols_length = row.getLastCellNum();
 60                     //设置当前数组长度 5
 61                     String[] str = new String[cols_length];
 62                     for (int j = 0; j < row.getLastCellNum(); j++) {  
 63                         HSSFCell  cell = row.getCell(j);  
 64                         str[j] = this.getValue(cell);
 65                     }  
 66                     list.add(str);
 67                     log.info("文件"+path.substring(path.lastIndexOf("\")+1)+"解析完毕!");
 68                 }
 69             } catch (FileNotFoundException e) {
 70                 log.error("!!!!!!! 未找到文件!文件为: 【"+path+"】");
 71                 e.printStackTrace();
 72             } catch (IOException e) {
 73                 log.error("!!!!!!! 解析文件出错!文件为: 【"+path+"】");
 74                 e.printStackTrace();
 75             }
 76         return list;
 77     }
 78     
 79     
 80     
 81     
 82     /**
 83      * 解析excel 文件<br>
 84      *   格式为:xlsx
 85      * @param path 文件的路径
 86      * @return List<String[]>
 87      */
 88     public List<String[]> xlsxAnalysisExcexl(String path){
 89         List<String[]> list = new ArrayList<String[]>();
 90             log.info("开始解析"+path.substring(path.lastIndexOf("\")+1)+"文件");
 91             try {
 92                 FileInputStream is = new FileInputStream(path); //文件流  
 93                 XSSFWorkbook workbook = new XSSFWorkbook( is);   
 94                 XSSFSheet  sheet = workbook.getSheetAt(0);//得到excel第一页的内容
 95                 for (int i = 0; i < sheet.getLastRowNum(); i++) {
 96                     //循环行数
 97                     XSSFRow  row = sheet.getRow(i);
 98                     //获取当前行的列长度
 99                     int cols_length = row.getLastCellNum();
100                     //设置当前数组长度 5
101                     String[] str = new String[cols_length];
102                     for (int j = 0; j < row.getLastCellNum(); j++) {  
103                         XSSFCell  cell = row.getCell(j);  
104                         str[j] = this.getValue(cell);
105                     }  
106                     list.add(str);
107                     log.info("文件"+path.substring(path.lastIndexOf("\")+1)+"解析完毕!");
108                 }
109             } catch (FileNotFoundException e) {
110                 log.error("!!!!!!! 未找到文件!文件为: 【"+path+"】");
111                 e.printStackTrace();
112             } catch (IOException e) {
113                 log.error("!!!!!!! 解析文件出错!文件为: 【"+path+"】");
114                 e.printStackTrace();
115             }
116         return list;
117     }
118     
119 
120     
121     /**
122      * JXL 解析excel 【只适用于 xls格式 的文件解析】 <br>
123      * JXL 不支持 xlsx 文件的解析
124      * @param path 文件的路径
125      * @return
126      */
127     public List<String[]> jxlReadExcel(String path){
128         List<String[]> list = new ArrayList<String[]>();
129         try {
130             InputStream is = new FileInputStream(path);
131             jxl.Workbook rwb = jxl.Workbook.getWorkbook(is);
132             jxl.Sheet sheet = rwb.getSheet(0);
133             for (int i = 0; i < sheet.getRows(); i++) {
134                 jxl.Cell[] cell = sheet.getRow(i);
135                 String[] strs = new String[cell.length];
136                 for (int j = 0; j < cell.length; j++) {
137                     strs[j] = cell[j].getContents();
138                 }
139                 list.add(strs);
140             }
141         } catch (FileNotFoundException e) {
142             e.printStackTrace();
143         } catch (BiffException e) {
144             e.printStackTrace();
145         } catch (IndexOutOfBoundsException e) {
146             e.printStackTrace();
147         } catch (IOException e) {
148             e.printStackTrace();
149         } 
150         return list;
151     }
152     
153     
154     
155     
156     /**
157      * 根据不同的cell格式数据 来转换为string的数据
158      * @param cell 单元格内容  
159      * @return  
160      */
161     private String getValue(Cell cell) {
162          int cellType = cell.getCellType();  
163          String cellValue = null;  
164          switch(cellType) {  
165              case Cell.CELL_TYPE_STRING: //文本  
166                  cellValue = cell.getStringCellValue();  
167                  break;  
168              case Cell.CELL_TYPE_NUMERIC: //数字、日期  
169                  if(DateUtil.isCellDateFormatted(cell)) {  
170                      cellValue = new SimpleDateFormat("yyyyMMdd").format(cell.getDateCellValue()); //日期型  
171                  }  
172                  else {  
173                      String num = String.valueOf(cell.getNumericCellValue()); //数字  
174                      if(num.contains("E")){
175                          cellValue = num.substring(0,num.indexOf("E")).replaceAll("\.", "");
176                      }else if(num.endsWith(".0")){
177                          cellValue = num.split("\.")[0];
178                      }else{
179                          cellValue = num;
180                      }
181                  }  
182                  break;  
183              case Cell.CELL_TYPE_BOOLEAN: //布尔型  
184                  cellValue = String.valueOf(cell.getBooleanCellValue());  
185                  break;  
186              case Cell.CELL_TYPE_BLANK: //空白  
187                  cellValue = cell.getStringCellValue();  
188                  break;  
189              case Cell.CELL_TYPE_ERROR: //错误  
190                  cellValue = "";  
191                  break;  
192              case Cell.CELL_TYPE_FORMULA: //公式  
193                  cellValue = "";  
194                  break;  
195              default:  
196                  cellValue = "";  
197         
198         }
199          return cellValue;
200     }
201     
202     
203     /**
204      * 转换类型 <br>
205      * 支持xls 格式的excel文件
206      * @param hssfCell 
207      * @return
208      */
209     @SuppressWarnings("static-access")
210     private String getValue(HSSFCell hssfCell){  
211         if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN){  
212           return String.valueOf( hssfCell.getBooleanCellValue());  
213         }else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC){
214             String num = String.valueOf( hssfCell.getNumericCellValue());
215             if(num.contains("E")){//科学计数法  去掉. 和E及E后面的
216                 return num.substring(0,num.indexOf("E")).replaceAll("\.", "");
217             }else if(num.endsWith(".0")){// 自动转成 .0的,去掉.0
218                     return num.split("\.")[0];
219             }else{
220                 return num;
221             }
222         }else{  
223           return String.valueOf( hssfCell.getStringCellValue());  
224         }  
225     }  
226     
227     
228     /**
229      * 转换类型 <br>
230      * 支持 xlsx 格式的excel文件
231      * @param xssfCell
232      * @return
233      */
234     @SuppressWarnings("static-access")  
235       private String getValue(XSSFCell xssfCell){  
236         if(xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN){  
237           return String.valueOf( xssfCell.getBooleanCellValue());  
238         }else if(xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC){  
239           String num = String.valueOf( xssfCell.getNumericCellValue());
240           if(num.contains("E")){//科学计数法  去掉. 和E及E后面的
241                 return num.substring(0,num.indexOf("E")).replaceAll("\.", "");
242           }else if(num.endsWith(".0")){// 自动转成 .0的,去掉.0
243                   return num.split("\.")[0];
244           }else{
245               return num;
246           }
247         }else{  
248           return String.valueOf( xssfCell.getStringCellValue());  
249         }  
250       }  
251     
原文地址:https://www.cnblogs.com/chinazhou-wang/p/5910056.html