1. 程式人生 > >Excel JAVA 讀取代碼POI Excel Reader 修改補充

Excel JAVA 讀取代碼POI Excel Reader 修改補充

string tac del cto ror input 工具類 獲得 art

主要調整了一下代碼類的封裝。

以前的代碼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 = 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 修改補充