官網直接下載POI http://poi.apache.org/
1 package com.CommonUtil; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.text.SimpleDateFormat; 8 import java.util.ArrayList; 9 10 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator; 11 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 12 import org.apache.poi.ss.usermodel.Cell; 13 import org.apache.poi.ss.usermodel.DateUtil; 14 import org.apache.poi.ss.usermodel.FormulaEvaluator; 15 import org.apache.poi.ss.usermodel.Row; 16 import org.apache.poi.ss.usermodel.Sheet; 17 import org.apache.poi.ss.usermodel.Workbook; 18 import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator; 19 import org.apache.poi.xssf.usermodel.XSSFWorkbook; 20 21 /** 22 * excel文件讀取工具類,支持xls,xlsx兩種格式 23 * @author Andrew 24 * 25 */ 26 public class ExcelUtil { 27 28 /** 29 * excel文件讀取指定列的數據 30 * @author Andrew 31 * @param excelPath 文件名 32 * @param args 需要查詢的列號 33 * @return ArrayList<ArrayList<String>> 二維字符串數組 34 * @throws IOException 35 */ 36 @SuppressWarnings({ "unused" }) 37 public ArrayList<ArrayList<String>> excelReader(String excelPath,int ... args) throws IOException { 38 // 創建excel工作簿對象 39 Workbook workbook = null; 40 FormulaEvaluator formulaEvaluator = null; 41 // 讀取目標文件 42 File excelFile = new File(excelPath); 43 InputStream is = new FileInputStream(excelFile); 44 // 判斷文件是xlsx還是xls 45 if (excelFile.getName().endsWith("xlsx")) { 46 workbook = new XSSFWorkbook(is); 47 formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook); 48 }else { 49 workbook = new HSSFWorkbook(is); 50 formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook); 51 } 52 53 //判斷excel文件打開是否正確 54 if(workbook == null){ 55 System.err.println("未讀取到內容,請檢查路徑!"); 56 return null; 57 } 58 //創建二維數組,儲存excel行列數據 59 ArrayList<ArrayList<String>> als = new ArrayList<ArrayList<String>>(); 60 //遍歷工作簿中的sheet 61 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { 62 Sheet sheet = workbook.getSheetAt(numSheet); 63 //當前sheet頁面為空,繼續遍歷 64 if (sheet == null) { 65 continue; 66 } 67 // 對於每個sheet,讀取其中的每一行 68 for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) { 69 Row row = sheet.getRow(rowNum); 70 if (row == null) { 71 continue; 72 } 73 ArrayList<String> al = new ArrayList<String>(); 74 // 遍歷每一行的每一列 75 for(int columnNum = 0 ; columnNum < args.length ; columnNum++){ 76 Cell cell = row.getCell(args[columnNum]); 77 al.add(getValue(cell, formulaEvaluator)); 78 } 79 als.add(al); 80 } 81 } 82 is.close(); 83 return als; 84 } 85 86 /** 87 * excel文件讀取全部信息 88 * @author Andrew 89 * @param excelPath 文件名 90 * @return ArrayList<ArrayList<String>> 二維字符串數組 91 * @throws IOException 92 */ 93 @SuppressWarnings({ "unused" }) 94 public ArrayList<ArrayList<String>> excelReader(String excelPath) throws IOException { 95 // 創建excel工作簿對象 96 Workbook workbook = null; 97 FormulaEvaluator formulaEvaluator = null; 98 // 讀取目標文件 99 File excelFile = new File(excelPath); 100 InputStream is = new FileInputStream(excelFile); 101 // 判斷文件是xlsx還是xls 102 if (excelFile.getName().endsWith("xlsx")) { 103 workbook = new XSSFWorkbook(is); 104 formulaEvaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook); 105 }else { 106 workbook = new HSSFWorkbook(is); 107 formulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook); 108 } 109 110 //判斷excel文件打開是否正確 111 if(workbook == null){ 112 System.err.println("未讀取到內容,請檢查路徑!"); 113 return null; 114 } 115 //創建二維數組,儲存excel行列數據 116 ArrayList<ArrayList<String>> als = new ArrayList<ArrayList<String>>(); 117 //遍歷工作簿中的sheet 118 for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) { 119 Sheet sheet = workbook.getSheetAt(numSheet); 120 //當前sheet頁面為空,繼續遍歷 121 if (sheet == null) { 122 continue; 123 } 124 // 對於每個sheet,讀取其中的每一行 125 for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) { 126 Row row = sheet.getRow(rowNum); 127 if (row == null) { 128 continue; 129 } 130 // 遍歷每一行的每一列 131 ArrayList<String> al = new ArrayList<String>(); 132 for(int columnNum = 0 ; columnNum < row.getLastCellNum(); columnNum++){ 133 Cell cell = row.getCell(columnNum); 134 al.add(getValue(cell, formulaEvaluator)); 135 } 136 als.add(al); 137 } 138 } 139 is.close(); 140 return als; 141 } 142 143 /** 144 * excel文件的數據讀取,包括后綴為xls,xlsx 145 * @param xssfRow 146 * @return 147 */ 148 @SuppressWarnings("deprecation") 149 private static String getValue(Cell cell, FormulaEvaluator formulaEvaluator) { 150 if(cell==null){ 151 return null; 152 } 153 switch (cell.getCellType()) { 154 case Cell.CELL_TYPE_STRING: 155 return cell.getRichStringCellValue().getString(); 156 case Cell.CELL_TYPE_NUMERIC: 157 // 判斷是日期時間類型還是數值類型 158 if (DateUtil.isCellDateFormatted(cell)) { 159 short format = cell.getCellStyle().getDataFormat(); 160 SimpleDateFormat sdf = null; 161 /* 所有日期格式都可以通過getDataFormat()值來判斷 162 * yyyy-MM-dd----- 14 163 * yyyy年m月d日----- 31 164 * yyyy年m月--------57 165 * m月d日 --------- 58 166 * HH:mm---------- 20 167 * h時mm分 --------- 32 168 */ 169 if(format == 14 || format == 31 || format == 57 || format == 58){ 170 //日期 171 sdf = new SimpleDateFormat("yyyy-MM-dd"); 172 }else if (format == 20 || format == 32) { 173 //時間 174 sdf = new SimpleDateFormat("HH:mm"); 175 } 176 return sdf.format(cell.getDateCellValue()); 177 } else { 178 // 對整數進行判斷處理 179 double cur = cell.getNumericCellValue(); 180 long longVal = Math.round(cur); 181 Object inputValue = null; 182 if(Double.parseDouble(longVal + ".0") == cur) { 183 inputValue = longVal; 184 } 185 else { 186 inputValue = cur; 187 } 188 return String.valueOf(inputValue); 189 } 190 case Cell.CELL_TYPE_BOOLEAN: 191 return String.valueOf(cell.getBooleanCellValue()); 192 case Cell.CELL_TYPE_FORMULA: 193 //對公式進行處理,返回公式計算后的值,使用cell.getCellFormula()只會返回公式 194 return String.valueOf(formulaEvaluator.evaluate(cell).getNumberValue()); 195 //Cell.CELL_TYPE_BLANK || Cell.CELL_TYPE_ERROR 196 default: 197 return null; 198 } 199 } 200 }