Excel imports Java tool poi

Function: Import the data in Excel into the data table.
The required jar packages are as follows:
Write a picture description here

Download address:
http://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1

To ensure the same version of the jar package, 3.16 is used here, commons-collections uses 4-4.1, 4.0 is not verified.

poi read excel key code

	private Map<Object,Object> readExcelValue0(Workbook wb)  {

		//总行数
		int totalRows = 0;
		int sheetRows = 0;
		//总条数
		int celllength = 0;
		List<String> sheetNamList = new ArrayList<>();
		List<List<String>> headerList = new ArrayList<List<String>>();
		int sheetNum = wb.getNumberOfSheets();

		//获取sheet页名称
		for(int i=0;i<sheetNum;i++) {
			sheetNamList.add(wb.getSheetName(i));
			Sheet sheet = wb.getSheetAt(i);
			sheetRows = sheet.getPhysicalNumberOfRows();//包含表头
			totalRows = totalRows + sheetRows - 1;
			if (sheetRows >= 1 && sheet.getRow(0) != null) {
				celllength = sheet.getRow(0).getPhysicalNumberOfCells();
			}
			Row row = sheet.getRow(0);
			List<String> oneList = new ArrayList<String>();
			for (int c = 0; c < celllength; c++) {
				Cell cell = row.getCell(c);
				if (cell != null) {
					oneList.add(cell.getStringCellValue());
				}
			}
			headerList.add(oneList);
		}
		Map<Object,Object> map = new HashMap<>();
		map.put("sheetNames", sheetNamList);
		map.put("headers", headerList);
		map.put("totalRows", totalRows);
		return map;
	}
      /*
       * 判断excel是03还是07
       */
     public boolean getExcelInfo(String fileName) { 
      //String fileName = mFile.getOriginalFilename();
        boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本 
        /*if (!validateExcel(fileName)) {// 验证文件名是否合格  ,放到前端
            return false; 
        }  */
        if (isExcel2007(fileName)) { 
            isExcel2003 = false; 
        } 
        return isExcel2003;
    }
public static boolean isExcel2007(String filePath)  {   
         return filePath.matches("^.+\\.(?i)(xlsx)$");   
     }
Published 137 original articles · Like 123 · Visit 250,000+

Guess you like

Origin blog.csdn.net/lz20120808/article/details/80774085