首页 > 代码库 > Excel JAVA 读取代码POI Excel Reader 修改补充

Excel JAVA 读取代码POI Excel Reader 修改补充

 

 

主要调整了一下代码类的封装。

以前的代码sheetIndex散落在多个方法上,而方法是要求按顺序执行的,没有被封装起来,会导致直接用部分方法会报错。

 

  1 import java.io.FileInputStream;
  2 import java.io.InputStream;
  3 import java.util.ArrayList;
  4 import java.util.List;
  5 
  6 import org.apache.logging.log4j.LogManager;
  7 import org.apache.logging.log4j.Logger;
  8 import org.apache.poi.ss.usermodel.Cell;
  9 import org.apache.poi.ss.usermodel.DateUtil;
 10 import org.apache.poi.ss.usermodel.Row;
 11 import org.apache.poi.ss.usermodel.Sheet;
 12 import org.apache.poi.ss.usermodel.Workbook;
 13 import org.apache.poi.ss.usermodel.WorkbookFactory;
 14 
 15 /**
 16  * ClassName:ExcelReader.java Author: wenbin.ji CreateTime: Jan 28, 2011
 17  * 11:16:29 AM Description:Excel数据读取工具类,POI实现,兼容Excel2003,及Excel2007
 18  **/
 19 
 20 public class ExcelReader {
 21     
 22     private static Logger logger = LogManager.getLogger(ExcelReader.class);
 23     
 24     private Sheet sheet=null;
 25     private List<String[]> dataList = null;
 26     
 27     
 28     public ExcelReader(String path,int sheetIndex) {
 29         try {
 30             InputStream inp = new FileInputStream(path);
 31             Workbook wb = WorkbookFactory.create(inp);
 32             sheet = wb.getSheetAt(sheetIndex);
 33             dataList=this.getAllData();
 34         } catch (Exception e) {
 35             e.printStackTrace();
 36         }
 37     }
 38 
 39     /**
 40      * 得到正式数据,头部除外
 41      * @param sheetIndex
 42      * @param startRow
 43      * @return
 44      */
 45     public List<String[]> getBodyData(int startRow) {
 46         List<String[]> allData = http://www.mamicode.com/this.getAllData();
 47         return allData.subList(startRow,allData.size());
 48     }
 49 
 50     
 51     /**
 52      * 获得最大数量的表格行。不考虑无数据行
 53      * @param sheetIndex
 54      * @return
 55      */
 56     private List<String[]> getAllData() {
 57         return this.getAllData(Integer.MAX_VALUE);
 58     }
 59     
 60     /**
 61      * 取Excel所有数据,包含header
 62      * @return List<String[]>
 63      */
 64     private List<String[]> getAllData(int endRow) {
 65         int columnNum = 0;
 66         dataList = new ArrayList<String[]>(100);
 67         int lastRow=this.getRowNum();
 68         //如果总行数大于指定结束行。那么以指定行为准
 69         lastRow=lastRow>endRow?endRow:lastRow;
 70         logger.error("lastRow=========>:"+lastRow);
 71         if (sheet.getRow(0) != null) {
 72             columnNum = sheet.getRow(0).getLastCellNum()
 73                     - sheet.getRow(0).getFirstCellNum();
 74         }
 75         if (columnNum > 0) {
 76             for (Row row : sheet) {
 77                 //到最后一行结束循环
 78                 if(row.getRowNum()>lastRow){
 79                     break;
 80                 }
 81                 
 82                 String[] singleRow = new String[columnNum];
 83                 int n = 0;
 84                 for (int i = 0; i < columnNum; i++) {
 85                     Cell cell = row.getCell(i, Row.CREATE_NULL_AS_BLANK);
 86                     switch (cell.getCellType()) {
 87                     case Cell.CELL_TYPE_BLANK:
 88                         singleRow[n] = "";
 89                         break;
 90                     case Cell.CELL_TYPE_BOOLEAN:
 91                         singleRow[n] = Boolean.toString(cell
 92                                 .getBooleanCellValue());
 93                         break;
 94                     // 数值
 95                     case Cell.CELL_TYPE_NUMERIC:
 96                         if (DateUtil.isCellDateFormatted(cell)) {
 97                             singleRow[n] = String.valueOf(cell
 98                                     .getDateCellValue());
 99                         } else {
100                             cell.setCellType(Cell.CELL_TYPE_STRING);
101                             String temp = cell.getStringCellValue();
102                             // 判断是否包含小数点,如果不含小数点,则以字符串读取,如果含小数点,则转换为Double类型的字符串
103                             if (temp.indexOf(".") > -1) {
104                                 singleRow[n] = String.valueOf(new Double(temp))
105                                         .trim();
106                             } else {
107                                 singleRow[n] = temp.trim();
108                             }
109                         }
110                         break;
111                     case Cell.CELL_TYPE_STRING:
112                         singleRow[n] = cell.getStringCellValue().trim();
113                         break;
114                     case Cell.CELL_TYPE_ERROR:
115                         singleRow[n] = "";
116                         break;
117                     case Cell.CELL_TYPE_FORMULA:
118                         cell.setCellType(Cell.CELL_TYPE_STRING);
119                         singleRow[n] = cell.getStringCellValue();
120                         if (singleRow[n] != null) {
121                             singleRow[n] = singleRow[n].replaceAll("#N/A", "")
122                                     .trim();
123                         }
124                         break;
125                     default:
126                         singleRow[n] = "";
127                         break;
128                     }
129                     n++;
130                 }
131                 if ("".equals(singleRow[0])) {
132                     continue;
133                 }// 如果第一行为空,跳过
134                 dataList.add(singleRow);
135             }
136         }
137         return dataList;
138     }
139 
140     /**
141      * 返回Excel最大行index值,实际行数要加1
142      * 
143      * @return
144      */
145     public int getRowNum() {
146         return sheet.getLastRowNum();
147     }
148 
149     /**
150      * 返回数据的列数
151      * 
152      * @return
153      */
154     public int getColumnNum() {
155         Row row = sheet.getRow(0);
156         if (row != null && row.getLastCellNum() > 0) {
157             return row.getLastCellNum();
158         }
159         return 0;
160     }
161 
162     /**
163      * 获取某一行数据
164      * @param rowIndex
165      *            计数从0开始,rowIndex为0代表header行
166      * @return
167      */
168     public String[] getRowData(int rowIndex) {
169         String[] dataArray = null;
170         if (rowIndex > this.getRowNum()) {
171             return dataArray;
172         } else {
173             dataArray = new String[this.getColumnNum()];
174             return this.dataList.get(rowIndex);
175         }
176 
177     }
178 
179     /**
180      * 获取某一列数据
181      * 
182      * @param colIndex
183      * @return
184      */
185     public String[] getColumnData(int colIndex) {
186         String[] dataArray = null;
187         if (colIndex > this.getColumnNum()) {
188             return dataArray;
189         } else {
190             if (this.dataList != null && this.dataList.size() > 0) {
191                 dataArray = new String[this.getRowNum() + 1];
192                 int index = 0;
193                 for (String[] rowData : dataList) {
194                     if (rowData != null) {
195                         dataArray[index] = rowData[colIndex];
196                         index++;
197                     }
198                 }
199             }
200         }
201         return dataArray;
202 
203     }
204 }

 

Excel JAVA 读取代码POI Excel Reader 修改补充