Commit 947bb1d865349083f383439dccbdf6bfcdb562e2
1 parent
fc987df9
Exists in
master
1.ExcelUtil优化
Showing
2 changed files
with
38 additions
and
20 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilExcel.java
... | ... | @@ -310,26 +310,11 @@ public class UtilExcel { |
310 | 310 | lastCellNum = UtilExcel.maxExcelColumnNum; |
311 | 311 | } |
312 | 312 | for(int j=0; j<lastCellNum; ++j){ |
313 | - Cell cell = row.getCell(j); | |
314 | - if(cell != null){ | |
315 | - CellType currCellType = cell.getCellTypeEnum(); | |
316 | - | |
317 | - if(currCellType.compareTo(CellType.STRING) == 0){ | |
318 | - dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString())); | |
319 | - }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ | |
320 | - dataRow.add(df.format(cell.getNumericCellValue())); | |
321 | - | |
322 | - }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ | |
323 | - dataRow.add(cell.getBooleanCellValue()); | |
324 | - }else if(currCellType.compareTo(CellType.FORMULA) == 0){ | |
325 | - dataRow.add(cell.getStringCellValue()); | |
326 | - }else if(currCellType.compareTo(CellType.BLANK) == 0 | |
327 | - || currCellType.compareTo(CellType.ERROR) == 0){ | |
328 | - dataRow.add(""); | |
329 | - } | |
330 | - | |
331 | - }else{ | |
313 | + try { | |
314 | + dataRow.add(getCellValue(row.getCell(j), df)); | |
315 | + }catch(Exception e) { | |
332 | 316 | dataRow.add(""); |
317 | + e.printStackTrace(); | |
333 | 318 | } |
334 | 319 | } |
335 | 320 | result.add(dataRow); |
... | ... | @@ -337,6 +322,39 @@ public class UtilExcel { |
337 | 322 | return result; |
338 | 323 | } |
339 | 324 | |
325 | + private static Object getCellValue(Cell cell, DecimalFormat df) { | |
326 | + if(cell == null) { | |
327 | + return ""; | |
328 | + } | |
329 | + CellType currCellType = cell.getCellTypeEnum(); | |
330 | + | |
331 | + if(currCellType.compareTo(CellType.STRING) == 0){ | |
332 | + return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
333 | + }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ | |
334 | + return df.format(cell.getNumericCellValue()); | |
335 | + }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ | |
336 | + return cell.getBooleanCellValue(); | |
337 | + }else if(currCellType.compareTo(CellType.FORMULA) == 0){ | |
338 | + return getFormulatValue(cell, df); | |
339 | + } | |
340 | + return ""; | |
341 | + } | |
342 | + | |
343 | + private static Object getFormulatValue(Cell cell, DecimalFormat df) { | |
344 | + CellType formulatResultType = cell.getCachedFormulaResultTypeEnum(); | |
345 | + | |
346 | + if(formulatResultType.compareTo(CellType.STRING) == 0){ | |
347 | + return UtilString.trimCodePage(cell.getRichStringCellValue().getString()); | |
348 | + }else if(formulatResultType.compareTo(CellType.NUMERIC) == 0){ | |
349 | + return df.format(cell.getNumericCellValue()); | |
350 | + }else if(formulatResultType.compareTo(CellType.BOOLEAN) == 0){ | |
351 | + return cell.getBooleanCellValue(); | |
352 | + }else if(formulatResultType.compareTo(CellType.FORMULA) == 0){ | |
353 | + return ""; | |
354 | + } | |
355 | + return ""; | |
356 | + } | |
357 | + | |
340 | 358 | public static List<List<Object>> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{ |
341 | 359 | File file = new File(filepath); |
342 | 360 | List<List<Object>> result = new ArrayList<List<Object>>(); | ... | ... |