Commit a4f5d61287d4ab9e55c1f03ca47b078b5de511cb
1 parent
299ab778
Exists in
master
part utilExcel
Showing
1 changed file
with
100 additions
and
4 deletions
Show diff stats
src/main/java/com/taover/util/UtilExcel.java
| ... | ... | @@ -11,7 +11,6 @@ import java.util.List; |
| 11 | 11 | import java.util.Map; |
| 12 | 12 | |
| 13 | 13 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| 14 | -import org.apache.poi.hssf.util.HSSFColor; | |
| 15 | 14 | import org.apache.poi.ss.usermodel.Cell; |
| 16 | 15 | import org.apache.poi.ss.usermodel.CellStyle; |
| 17 | 16 | import org.apache.poi.ss.usermodel.CellType; |
| ... | ... | @@ -25,7 +24,7 @@ public class UtilExcel { |
| 25 | 24 | private final static String excel2007U =".xlsx"; //2007+ 版本的excel |
| 26 | 25 | private final static String CSV =".csv"; //csv |
| 27 | 26 | |
| 28 | - private final static int maxExcelRowNum = 5000; | |
| 27 | + private final static int maxExcelRowNum = 10000; | |
| 29 | 28 | private final static int maxExcelColumnNum = 50; |
| 30 | 29 | |
| 31 | 30 | /** |
| ... | ... | @@ -245,6 +244,20 @@ public class UtilExcel { |
| 245 | 244 | } |
| 246 | 245 | |
| 247 | 246 | /** |
| 247 | + * 读取excel表--当前激活的工作表 | |
| 248 | + * @param path | |
| 249 | + */ | |
| 250 | + public static List<List<Object>> readExcel(String filepath, int rowLimit) throws Exception{ | |
| 251 | + File file = new File(filepath); | |
| 252 | + List<List<Object>> result = new ArrayList<List<Object>>(); | |
| 253 | + if(!file.exists()){ | |
| 254 | + return result; | |
| 255 | + } | |
| 256 | + Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
| 257 | + return readExcelBySheetIndex(filepath, wb.getActiveSheetIndex()); | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 248 | 261 | * 读取 |
| 249 | 262 | * @param filepath |
| 250 | 263 | * @return |
| ... | ... | @@ -297,8 +310,43 @@ public class UtilExcel { |
| 297 | 310 | int start = sheet.getFirstRowNum(); |
| 298 | 311 | int end = sheet.getLastRowNum(); |
| 299 | 312 | if(end > UtilExcel.maxExcelRowNum){ |
| 300 | - end = UtilExcel.maxExcelRowNum; | |
| 313 | + throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); | |
| 314 | + } | |
| 315 | + for(int i=start; i<end+1; ++i){ | |
| 316 | + Row row = sheet.getRow(i); | |
| 317 | + if(row == null){ | |
| 318 | + continue; | |
| 319 | + } | |
| 320 | + List<Object> dataRow = new ArrayList<Object>(); | |
| 321 | + int lastCellNum = row.getLastCellNum(); | |
| 322 | + if(lastCellNum > UtilExcel.maxExcelColumnNum){ | |
| 323 | + lastCellNum = UtilExcel.maxExcelColumnNum; | |
| 324 | + } | |
| 325 | + for(int j=0; j<lastCellNum; ++j){ | |
| 326 | + try { | |
| 327 | + dataRow.add(getCellValue(row.getCell(j), df)); | |
| 328 | + }catch(Exception e) { | |
| 329 | + dataRow.add(""); | |
| 330 | + e.printStackTrace(); | |
| 331 | + } | |
| 332 | + } | |
| 333 | + result.add(dataRow); | |
| 334 | + } | |
| 335 | + return result; | |
| 336 | + } | |
| 337 | + | |
| 338 | + /** | |
| 339 | + * 读取excel表--当前激活的工作表 | |
| 340 | + * @param path | |
| 341 | + */ | |
| 342 | + public static List<List<Object>> readExcelBySheet(Sheet sheet, int rowLimit) throws Exception{ | |
| 343 | + List<List<Object>> result = new ArrayList<List<Object>>(); | |
| 344 | + int start = sheet.getFirstRowNum(); | |
| 345 | + int end = sheet.getLastRowNum(); | |
| 346 | + if(end > rowLimit){ | |
| 347 | + end = rowLimit; | |
| 301 | 348 | } |
| 349 | + DecimalFormat df = new DecimalFormat("0"); | |
| 302 | 350 | for(int i=start; i<end+1; ++i){ |
| 303 | 351 | Row row = sheet.getRow(i); |
| 304 | 352 | if(row == null){ |
| ... | ... | @@ -368,7 +416,7 @@ public class UtilExcel { |
| 368 | 416 | int start = sheet.getFirstRowNum(); |
| 369 | 417 | int end = sheet.getLastRowNum(); |
| 370 | 418 | if(end > UtilExcel.maxExcelRowNum){ |
| 371 | - end = UtilExcel.maxExcelRowNum; | |
| 419 | + throw new Exception("目前系统只支持读取10000行以内记录,您当前Excel行数过大"); | |
| 372 | 420 | } |
| 373 | 421 | for(int i=start; i<end+1; ++i){ |
| 374 | 422 | Row row = sheet.getRow(i); |
| ... | ... | @@ -411,6 +459,54 @@ public class UtilExcel { |
| 411 | 459 | return result; |
| 412 | 460 | } |
| 413 | 461 | |
| 462 | + public static List<List<Object>> readExcelBySheetExcludeHideLine(Sheet sheet, int rowLimit) throws Exception{ | |
| 463 | + List<List<Object>> result = new ArrayList<List<Object>>(); | |
| 464 | + DecimalFormat df = new DecimalFormat("0"); | |
| 465 | + int start = sheet.getFirstRowNum(); | |
| 466 | + int end = sheet.getLastRowNum(); | |
| 467 | + if(end > rowLimit){ | |
| 468 | + end = rowLimit; | |
| 469 | + } | |
| 470 | + for(int i=start; i<end+1; ++i){ | |
| 471 | + Row row = sheet.getRow(i); | |
| 472 | + if(row == null){ | |
| 473 | + continue; | |
| 474 | + } | |
| 475 | + //去除隐藏行 | |
| 476 | + if(row.getZeroHeight()){ | |
| 477 | + continue; | |
| 478 | + } | |
| 479 | + List<Object> dataRow = new ArrayList<Object>(); | |
| 480 | + int lastCellNum = row.getLastCellNum(); | |
| 481 | + if(lastCellNum > UtilExcel.maxExcelColumnNum){ | |
| 482 | + lastCellNum = UtilExcel.maxExcelColumnNum; | |
| 483 | + } | |
| 484 | + for(int j=0; j<lastCellNum; ++j){ | |
| 485 | + Cell cell = row.getCell(j); | |
| 486 | + if(cell != null){ | |
| 487 | + CellType currCellType = cell.getCellTypeEnum(); | |
| 488 | + | |
| 489 | + if(currCellType.compareTo(CellType.STRING) == 0){ | |
| 490 | + dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString())); | |
| 491 | + }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ | |
| 492 | + dataRow.add(df.format(cell.getNumericCellValue())); | |
| 493 | + }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ | |
| 494 | + dataRow.add(cell.getBooleanCellValue()); | |
| 495 | + }else if(currCellType.compareTo(CellType.FORMULA) == 0 | |
| 496 | + || currCellType.compareTo(CellType.BLANK) == 0 | |
| 497 | + || currCellType.compareTo(CellType.ERROR) == 0){ | |
| 498 | + dataRow.add(""); | |
| 499 | + } | |
| 500 | + | |
| 501 | + }else{ | |
| 502 | + dataRow.add(""); | |
| 503 | + } | |
| 504 | + } | |
| 505 | + result.add(dataRow); | |
| 506 | + } | |
| 507 | + return result; | |
| 508 | + } | |
| 509 | + | |
| 414 | 510 | public static void main(String args[]){ |
| 415 | 511 | //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; |
| 416 | 512 | String filepath = "C:\\Users\\EDZ\\Desktop\\榴莲1 (5).xlsx"; | ... | ... |