解析excel工具类

自定义封装:

public class ExcelUtil {
	 

	/**
	 * @author: 
	 * @date: 2018-5-26 下午4:40:04
	 * @Description: 解析
	 * @param @param excelFile
	 * @throws ExcelException 
	 * @throws IOException 
	 */
	public static List<Map<Integer, String>> getExcelData(
			MultipartFile excelFile,String datePattren,String numberPattern) throws ExcelException, IOException {
		//解析结果集,key表示第i列
		List<Map<Integer, String>> list = new ArrayList<>();
		// 文件类型
		String fileType = "";
		try {
			String fileName = excelFile.getOriginalFilename();
			fileType = fileName.substring(fileName.lastIndexOf(".") + 1,
					fileName.length());
		} catch (Exception e) {
			fileType = "";
		}
		if (!fileType.toLowerCase().equals("xls")
				&& !fileType.toLowerCase().equals("xlsx")) {
			throw new ExcelException("文件格式不支持,请上传xls或xlsx文件");
		}

		if ("xlsx".equals(fileType) || "xls".equals(fileType)) {
			Workbook wb = null;
			InputStream inp = excelFile.getInputStream();
			try {
				if (!inp.markSupported()) {
					inp = new PushbackInputStream(inp, 8);
				}

				if (POIFSFileSystem.hasPOIFSHeader(inp)) {
					wb = new HSSFWorkbook(inp);
				} else if (POIXMLDocument.hasOOXMLHeader(inp)) {
					wb = new XSSFWorkbook(OPCPackage.open(inp));
				}

				if (null == wb) {
					throw new ExcelException("导入失败");
				}
				Sheet data = wb.getSheetAt(0);// 第一个页脚
				// 解析

				for (int i = data.getFirstRowNum() + 1; i <= data
						.getLastRowNum(); i++) {
					// 第i行
					Row row = data.getRow(i);
					Iterator cells = row.cellIterator();
					HashMap<Integer, String> map = new HashMap<>();
					// 循环列
					
					while (cells.hasNext()) {
						// 设置单元格内容为字符串
						Cell cell = (Cell) cells.next();
						//cell.setCellType(Cell.CELL_TYPE_STRING);
						String val ="";
						
						 if(cell != null){  
					            switch (cell.getCellType()) {  
					            case Cell.CELL_TYPE_STRING:  
					                val = cell.getStringCellValue();  
					                break;  
					            case Cell.CELL_TYPE_BOOLEAN:  
					                Boolean val1 = cell.getBooleanCellValue();  
					                val = val1.toString();  
					                break;  
					            case Cell.CELL_TYPE_NUMERIC:  
//					              Double val3 = cell.getNumericCellValue();  
//					              val = val3.toString();  
					                if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {     
					                    Date theDate = cell.getDateCellValue();  
					                    SimpleDateFormat dff = new SimpleDateFormat(datePattren);  
					                    val = dff.format(theDate);
					                    
					                 
					                }else{  
					                    DecimalFormat df = new DecimalFormat(numberPattern); 
					                  
					                    val = df.format(cell.getNumericCellValue());
					                   
					                }  
					                break;  
					            case Cell.CELL_TYPE_BLANK:  
					                break;  
					            default:  
					                
					            }  
					        }  
						
						
						
						Integer key = cell.getColumnIndex();
						map.put(key, val);
						//System.out.println("内容为"+key+"  :"+val);
						
					}
					list.add(map);
				}

			} catch (Exception e) {
				throw new ExcelException("未知原因,解析失败");
				// ("保存Excel文件时,请不要将鼠标最终定位在Excel中的可以下拉选值的列上。");
			}
		}
		return list;
	}

}

例如解析


中模板的数据:将金额的格式设为保留小数点后两位:excelList = ExcelUtil.getExcelData(excelFile, "yyyy-MM-dd", "0.00");

,得到的结果集应该为:

{  {0:"哈尔滨",1:"",2:"601.00"},{0:"哈尔滨",1:"",2:"3010.00"},{0:"洛阳",1:"",2:"40000.00"},

{0:"邯郸",1:"丛台区",2:"22050.00"},{0:"洛阳",1:"",2:"6000.00"}}

猜你喜欢

转载自blog.csdn.net/w_t_y_y/article/details/80464614