Commit 626d93d1c378d047c49bd9f2c795ce4509a8c32a

Authored by unknown
1 parent 45a966ad
Exists in master

ExcelUtil 增加行数限制与不限制接口

build.gradle
... ... @@ -59,7 +59,7 @@ uploadArchives {
59 59 authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD)
60 60 }
61 61 pom.project {
62   - version '1.1.32'
  62 + version '1.1.34'
63 63 artifactId ARTIFACT_Id
64 64 groupId GROUP_ID
65 65 packaging TYPE
... ...
src/main/java/com/taover/util/UtilExcel.java
... ... @@ -237,26 +237,24 @@ public class UtilExcel {
237 237 */
238 238 public static List<List<Object>> readExcel(String filepath) throws Exception{
239 239 File file = new File(filepath);
240   - List<List<Object>> result = new ArrayList<List<Object>>();
241 240 if(!file.exists()){
242   - return result;
  241 + throw new Exception("Excel["+filepath+"]文件不存在");
243 242 }
244 243 Workbook wb = UtilExcel.getWorkbook(filepath, true);
245   - return readExcelBySheetIndex(filepath, wb.getActiveSheetIndex());
  244 + return readExcelBySheetIndex(wb, wb.getActiveSheetIndex());
246 245 }
247 246  
248 247 /**
249 248 * 读取excel表--当前激活的工作表
250 249 * @param path
251 250 */
252   - public static List<List<Object>> readExcel(String filepath, int rowLimit) throws Exception{
  251 + public static List<List<Object>> readExcel(String filepath, boolean hasLimit) throws Exception{
253 252 File file = new File(filepath);
254   - List<List<Object>> result = new ArrayList<List<Object>>();
255 253 if(!file.exists()){
256   - return result;
  254 + throw new Exception("Excel["+filepath+"]文件不存在");
257 255 }
258 256 Workbook wb = UtilExcel.getWorkbook(filepath, true);
259   - return readExcelBySheetIndex(filepath, wb.getActiveSheetIndex());
  257 + return readExcelBySheetIndex(wb, wb.getActiveSheetIndex(), hasLimit);
260 258 }
261 259  
262 260 /**
... ... @@ -267,9 +265,8 @@ public class UtilExcel {
267 265 */
268 266 public static List<List<Object>> readExcelExcludeHideLine(String filepath) throws Exception{
269 267 File file = new File(filepath);
270   - List<List<Object>> result = new ArrayList<List<Object>>();
271 268 if(!file.exists()){
272   - return result;
  269 + throw new Exception("Excel["+filepath+"]文件不存在");
273 270 }
274 271 Workbook wb = UtilExcel.getWorkbook(filepath, true);
275 272 return readExcelBySheetIndexExcludeHideLine(filepath, wb.getActiveSheetIndex());
... ... @@ -283,12 +280,12 @@ public class UtilExcel {
283 280 File file = new File(filepath);
284 281 List<List<Object>> result = new ArrayList<List<Object>>();
285 282 if(!file.exists()){
286   - return result;
  283 + throw new Exception("Excel["+filepath+"]文件不存在");
287 284 }
288 285 Workbook wb = UtilExcel.getWorkbook(filepath, true);
289 286 int sheetNumber = wb.getNumberOfSheets();
290 287  
291   - for(int sheetIndex=0; sheetIndex<sheetNumber; ++sheetIndex){
  288 + for(int sheetIndex=0; sheetIndex<sheetNumber; ++sheetIndex){
292 289 result.addAll(readExcelBySheetIndex(filepath, sheetIndex));
293 290 }
294 291  
... ... @@ -301,47 +298,40 @@ public class UtilExcel {
301 298 */
302 299 public static List<List<Object>> readExcelBySheetIndex(String filepath, int sheetIndex) throws Exception{
303 300 File file = new File(filepath);
304   - List<List<Object>> result = new ArrayList<List<Object>>();
305 301 if(!file.exists()){
306   - return result;
  302 + throw new Exception("Excel["+filepath+"]文件不存在");
307 303 }
308   - Workbook wb = UtilExcel.getWorkbook(filepath, true);
309   - //创建Excel工作簿对象
310   - DecimalFormat df = new DecimalFormat("0");
  304 + Workbook wb = UtilExcel.getWorkbook(filepath, true);
311 305 Sheet sheet = wb.getSheetAt(sheetIndex);
312   - int start = sheet.getFirstRowNum();
313 306 int end = sheet.getLastRowNum();
314 307 if(end > UtilExcel.maxExcelRowNum){
315 308 throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
316 309 }
317   - for(int i=start; i<end+1; ++i){
318   - Row row = sheet.getRow(i);
319   - if(row == null){
320   - continue;
321   - }
322   - List<Object> dataRow = new ArrayList<Object>();
323   - int lastCellNum = row.getLastCellNum();
324   - if(lastCellNum > UtilExcel.maxExcelColumnNum){
325   - lastCellNum = UtilExcel.maxExcelColumnNum;
326   - }
327   - for(int j=0; j<lastCellNum; ++j){
328   - try {
329   - dataRow.add(getCellValue(row.getCell(j), df));
330   - }catch(Exception e) {
331   - dataRow.add("");
332   - e.printStackTrace();
333   - }
  310 + return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false);
  311 + }
  312 +
  313 + private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex) throws Exception{
  314 + return readExcelBySheetIndex(wb, sheetIndex, true);
  315 + }
  316 +
  317 + private static List<List<Object>> readExcelBySheetIndex(Workbook wb, int sheetIndex, boolean hasRowLimit) throws Exception{
  318 + Sheet sheet = wb.getSheetAt(sheetIndex);
  319 + int end = sheet.getLastRowNum();
  320 + if(hasRowLimit){
  321 + if(end > UtilExcel.maxExcelRowNum) {
  322 + throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
334 323 }
335   - result.add(dataRow);
  324 + return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, false);
  325 + }else {
  326 + return readExcelBySheet(sheet, Integer.MAX_VALUE, false);
336 327 }
337   - return result;
338 328 }
339 329  
340 330 /**
341 331 * 读取excel表--当前激活的工作表
342 332 * @param path
343 333 */
344   - public static List<List<Object>> readExcelBySheet(Sheet sheet, int rowLimit) throws Exception{
  334 + private static List<List<Object>> readExcelBySheet(Sheet sheet, int rowLimit, boolean excludeRowZeroHeight) throws Exception{
345 335 List<List<Object>> result = new ArrayList<List<Object>>();
346 336 int start = sheet.getFirstRowNum();
347 337 int end = sheet.getLastRowNum();
... ... @@ -354,6 +344,10 @@ public class UtilExcel {
354 344 if(row == null){
355 345 continue;
356 346 }
  347 + //去除隐藏行
  348 + if(excludeRowZeroHeight && row.getZeroHeight()){
  349 + continue;
  350 + }
357 351 List<Object> dataRow = new ArrayList<Object>();
358 352 int lastCellNum = row.getLastCellNum();
359 353 if(lastCellNum > UtilExcel.maxExcelColumnNum){
... ... @@ -381,13 +375,11 @@ public class UtilExcel {
381 375 if(currCellType.compareTo(CellType.STRING) == 0){
382 376 return UtilString.trimCodePage(cell.getRichStringCellValue().getString());
383 377 }else if(currCellType.compareTo(CellType.NUMERIC) == 0){
384   -
385   - //if (DateUtil.isCellDateFormatted(cell)) {
386 378 if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) {
387 379 // 如果是date类型则 ,获取该cell的date值
388 380 return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue()));
389   - } else { // 纯数字
390   - // return String.valueOf(cell.getNumericCellValue());
  381 + } else {
  382 + // 纯数字
391 383 return df.format(cell.getNumericCellValue());
392 384 }
393 385 }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){
... ... @@ -415,114 +407,17 @@ public class UtilExcel {
415 407  
416 408 public static List<List<Object>> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{
417 409 File file = new File(filepath);
418   - List<List<Object>> result = new ArrayList<List<Object>>();
419 410 if(!file.exists()){
420   - return result;
  411 + throw new Exception("Excel文件["+filepath+"]不存在");
421 412 }
422   - Workbook wb = UtilExcel.getWorkbook(filepath, true);
423   - //创建Excel工作簿对象
424   - DecimalFormat df = new DecimalFormat("0");
  413 + Workbook wb = UtilExcel.getWorkbook(filepath, true);
425 414 Sheet sheet = wb.getSheetAt(sheetIndex);
426   - int start = sheet.getFirstRowNum();
427 415 int end = sheet.getLastRowNum();
428 416 if(end > UtilExcel.maxExcelRowNum){
429 417 throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大");
430 418 }
431   - for(int i=start; i<end+1; ++i){
432   - Row row = sheet.getRow(i);
433   - if(row == null){
434   - continue;
435   - }
436   - //去除隐藏行
437   - if(row.getZeroHeight()){
438   - continue;
439   - }
440   - List<Object> dataRow = new ArrayList<Object>();
441   - int lastCellNum = row.getLastCellNum();
442   - if(lastCellNum > UtilExcel.maxExcelColumnNum){
443   - lastCellNum = UtilExcel.maxExcelColumnNum;
444   - }
445   - for(int j=0; j<lastCellNum; ++j){
446   - Cell cell = row.getCell(j);
447   - if(cell != null){
448   - CellType currCellType = cell.getCellTypeEnum();
449   -
450   - if(currCellType.compareTo(CellType.STRING) == 0){
451   - dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString()));
452   - }else if(currCellType.compareTo(CellType.NUMERIC) == 0){
453   - //if (DateUtil.isCellDateFormatted(cell)) {
454   - if (cell.getCellStyle().getDataFormat()==28||cell.getCellStyle().getDataFormat()==31 || cell.getCellStyle().getDataFormat() == 58) {
455   - // 如果是date类型则 ,获取该cell的date值
456   - dataRow.add( new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(cell.getNumericCellValue())));
457   - } else { // 纯数字
458   - // return String.valueOf(cell.getNumericCellValue());
459   - dataRow.add( df.format(cell.getNumericCellValue()));
460   - }
461   -
462   - }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){
463   - dataRow.add(cell.getBooleanCellValue());
464   - }else if(currCellType.compareTo(CellType.FORMULA) == 0
465   - || currCellType.compareTo(CellType.BLANK) == 0
466   - || currCellType.compareTo(CellType.ERROR) == 0){
467   - dataRow.add("");
468   - }
469   -
470   - }else{
471   - dataRow.add("");
472   - }
473   - }
474   - result.add(dataRow);
475   - }
476   - return result;
477   - }
478   -
479   - public static List<List<Object>> readExcelBySheetExcludeHideLine(Sheet sheet, int rowLimit) throws Exception{
480   - List<List<Object>> result = new ArrayList<List<Object>>();
481   - DecimalFormat df = new DecimalFormat("0");
482   - int start = sheet.getFirstRowNum();
483   - int end = sheet.getLastRowNum();
484   - if(end > rowLimit){
485   - end = rowLimit;
486   - }
487   - for(int i=start; i<end+1; ++i){
488   - Row row = sheet.getRow(i);
489   - if(row == null){
490   - continue;
491   - }
492   - //去除隐藏行
493   - if(row.getZeroHeight()){
494   - continue;
495   - }
496   - List<Object> dataRow = new ArrayList<Object>();
497   - int lastCellNum = row.getLastCellNum();
498   - if(lastCellNum > UtilExcel.maxExcelColumnNum){
499   - lastCellNum = UtilExcel.maxExcelColumnNum;
500   - }
501   - for(int j=0; j<lastCellNum; ++j){
502   - Cell cell = row.getCell(j);
503   - if(cell != null){
504   - CellType currCellType = cell.getCellTypeEnum();
505   -
506   - if(currCellType.compareTo(CellType.STRING) == 0){
507   - dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString()));
508   - }else if(currCellType.compareTo(CellType.NUMERIC) == 0){
509   - dataRow.add(df.format(cell.getNumericCellValue()));
510   - }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){
511   - dataRow.add(cell.getBooleanCellValue());
512   - }else if(currCellType.compareTo(CellType.FORMULA) == 0
513   - || currCellType.compareTo(CellType.BLANK) == 0
514   - || currCellType.compareTo(CellType.ERROR) == 0){
515   - dataRow.add("");
516   - }
517   -
518   - }else{
519   - dataRow.add("");
520   - }
521   - }
522   - result.add(dataRow);
523   - }
524   - return result;
525   - }
  419 + return readExcelBySheet(sheet, UtilExcel.maxExcelRowNum, true);
  420 + }
526 421  
527 422 public static void main(String args[]){
528 423 //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx";
... ...