Commit 958bce2fe7602699814b36ac5a1c243ec9047e50
1 parent
90f6a1cf
Exists in
master
新增过滤隐藏行
Showing
2 changed files
with
92 additions
and
17 deletions
Show diff stats
build.gradle
src/main/java/com/taover/util/UtilExcel.java
... | ... | @@ -238,6 +238,22 @@ public class UtilExcel { |
238 | 238 | } |
239 | 239 | |
240 | 240 | /** |
241 | + * 读取 | |
242 | + * @param filepath | |
243 | + * @return | |
244 | + * @throws Exception | |
245 | + */ | |
246 | + public static List<List<Object>> readExcelExcludeHideLine(String filepath) throws Exception{ | |
247 | + File file = new File(filepath); | |
248 | + List<List<Object>> result = new ArrayList<List<Object>>(); | |
249 | + if(!file.exists()){ | |
250 | + return result; | |
251 | + } | |
252 | + Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
253 | + return readExcelBySheetIndexExcludeHideLine(filepath, wb.getActiveSheetIndex()); | |
254 | + } | |
255 | + | |
256 | + /** | |
241 | 257 | * 读取excel表--所有工作表 |
242 | 258 | * @param path |
243 | 259 | */ |
... | ... | @@ -313,29 +329,88 @@ public class UtilExcel { |
313 | 329 | return result; |
314 | 330 | } |
315 | 331 | |
316 | - public static void main(String args[]){ | |
317 | - String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; | |
318 | - List<List<Object>> data = null; | |
319 | - try { | |
320 | - data = UtilExcel.readExcel(filepath); | |
321 | - } catch (Exception e) { | |
322 | - // TODO Auto-generated catch block | |
323 | - e.printStackTrace(); | |
332 | + public static List<List<Object>> readExcelBySheetIndexExcludeHideLine(String filepath, int sheetIndex) throws Exception{ | |
333 | + File file = new File(filepath); | |
334 | + List<List<Object>> result = new ArrayList<List<Object>>(); | |
335 | + if(!file.exists()){ | |
336 | + return result; | |
324 | 337 | } |
325 | - List<Short> styleList = new ArrayList<Short>(); | |
326 | - for(int i=0; i<data.size(); ++i){ | |
327 | - if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index)); | |
328 | - else styleList.add(null); | |
329 | - for(int j=0; j<data.get(i).size(); ++j){ | |
330 | - System.out.print(data.get(i).get(j).toString()+" : "); | |
338 | + Workbook wb = UtilExcel.getWorkbook(filepath, true); | |
339 | + //创建Excel工作簿对象 | |
340 | + DecimalFormat df = new DecimalFormat("0"); | |
341 | + Sheet sheet = wb.getSheetAt(sheetIndex); | |
342 | + int start = sheet.getFirstRowNum(); | |
343 | + int end = sheet.getLastRowNum(); | |
344 | + if(end > UtilExcel.maxExcelRowNum){ | |
345 | + end = UtilExcel.maxExcelRowNum; | |
346 | + } | |
347 | + for(int i=start; i<end+1; ++i){ | |
348 | + Row row = sheet.getRow(i); | |
349 | + if(row == null){ | |
350 | + continue; | |
331 | 351 | } |
332 | - System.out.println(""); | |
352 | + //去除隐藏行 | |
353 | + if(row.getZeroHeight()){ | |
354 | + continue; | |
355 | + } | |
356 | + List<Object> dataRow = new ArrayList<Object>(); | |
357 | + int lastCellNum = row.getLastCellNum(); | |
358 | + if(lastCellNum > UtilExcel.maxExcelColumnNum){ | |
359 | + lastCellNum = UtilExcel.maxExcelColumnNum; | |
360 | + } | |
361 | + for(int j=0; j<lastCellNum; ++j){ | |
362 | + Cell cell = row.getCell(j); | |
363 | + if(cell != null){ | |
364 | + CellType currCellType = cell.getCellTypeEnum(); | |
365 | + | |
366 | + if(currCellType.compareTo(CellType.STRING) == 0){ | |
367 | + dataRow.add(UtilString.trimCodePage(cell.getRichStringCellValue().getString())); | |
368 | + }else if(currCellType.compareTo(CellType.NUMERIC) == 0){ | |
369 | + dataRow.add(df.format(cell.getNumericCellValue())); | |
370 | + | |
371 | + }else if(currCellType.compareTo(CellType.BOOLEAN) == 0){ | |
372 | + dataRow.add(cell.getBooleanCellValue()); | |
373 | + }else if(currCellType.compareTo(CellType.FORMULA) == 0 | |
374 | + || currCellType.compareTo(CellType.BLANK) == 0 | |
375 | + || currCellType.compareTo(CellType.ERROR) == 0){ | |
376 | + dataRow.add(""); | |
377 | + } | |
378 | + | |
379 | + }else{ | |
380 | + dataRow.add(""); | |
381 | + } | |
382 | + } | |
383 | + result.add(dataRow); | |
333 | 384 | } |
385 | + return result; | |
386 | + } | |
387 | + | |
388 | + public static void main(String args[]){ | |
389 | + //String filepath = "C:\\Users\\root\\Desktop\\千丁-6.27.xlsx"; | |
390 | + String filepath = "C:\\Users\\EDZ\\Desktop\\gaoming测试甩单.xls"; | |
391 | + List<List<Object>> data = null; | |
334 | 392 | try { |
335 | - UtilExcel.saveExcel("测试", data, "D:\\12345.xlsx", styleList); | |
393 | + data = UtilExcel.readExcel(filepath); | |
394 | + System.out.println(data.size()); | |
395 | + System.out.println(UtilExcel.readExcelExcludeHideLine(filepath).size()); | |
336 | 396 | } catch (Exception e) { |
337 | 397 | // TODO Auto-generated catch block |
338 | 398 | e.printStackTrace(); |
339 | 399 | } |
400 | +// List<Short> styleList = new ArrayList<Short>(); | |
401 | +// for(int i=0; i<data.size(); ++i){ | |
402 | +// if(i == 1)styleList.add(Short.valueOf(HSSFColor.RED.index)); | |
403 | +// else styleList.add(null); | |
404 | +// for(int j=0; j<data.get(i).size(); ++j){ | |
405 | +// System.out.print(data.get(i).get(j).toString()+" : "); | |
406 | +// } | |
407 | +// System.out.println(""); | |
408 | +// } | |
409 | +// try { | |
410 | +// UtilExcel.saveExcel("测试", data, "D:\\12345.xlsx", styleList); | |
411 | +// } catch (Exception e) { | |
412 | +// // TODO Auto-generated catch block | |
413 | +// e.printStackTrace(); | |
414 | +// } | |
340 | 415 | } |
341 | 416 | } | ... | ... |