poi判断单元格类型取值及判断2003还是2007

转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/106843391 谢谢

代码


	public boolean isExcel2003(String filePath) {
		return filePath.matches("^.+\\.(?i)(xls)$");
	}

	public boolean isExcel2007(String filePath) {
		return filePath.matches("^.+\\.(?i)(xlsx)$");
	}


	/**
	 * 获取文件后缀
	 *
	 * @author LHL
	 */
	private String getPostfix(String oldName) {
		if (StringUtils.isBlank(oldName) || !oldName.contains(".")) {
			return null;
		}
		return oldName.substring(oldName.lastIndexOf(".") + 1).trim();
	}

	/**
	 * 判断单元格类型取值
	 *
	 * @author LHL
	 */
	private String formGetCellType(Cell cell) {
		String value = null;
		try {
			if (cell.getCellTypeEnum() == CellType.BLANK) {
				// 空值
				value = "";
			} else if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
				// Boolean
				value = String.valueOf(cell.getBooleanCellValue());
			} else if (cell.getCellTypeEnum() == CellType.ERROR) {
				// 故障 "非法字符"
				value = "";
			} else if (cell.getCellTypeEnum() == CellType.FORMULA) {
				// 公式
				value = cell.getCellFormula();
			} else if (cell.getCellTypeEnum() == CellType.NUMERIC) {
				// 数字
				if (DateUtil.isCellDateFormatted(cell)) {
					Date tempValue = cell.getDateCellValue();
					SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					value = simpleFormat.format(tempValue);
				} else {
					value = new DecimalFormat("#").format(cell.getNumericCellValue());
					//value = String.valueOf(cell.getNumericCellValue());
				}
			} else if (cell.getCellTypeEnum() == CellType.STRING) {
				// 字符串
				value = cell.getStringCellValue().trim();
			} else {
				value = "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}


	/**
	 * 获取单词excel数据
	 *
	 * @author LHL
	 */
	public Map<String, Object> getWordExcelData(MultipartFile file) {
		Workbook wb = null;
		FileInputStream in = null;
		File remoteFile = null;
		Map<String, Object> readResult = null;
		try {
			String oldName = file.getOriginalFilename();
			//获取文件后缀
			String postfix = getPostfix(oldName);
			if (StringUtils.isBlank(postfix)) {
				return null;
			}
			// 得到新名字
			String newName = StrUtil.createFileNameUseTime() + "." + postfix;
			// 得到文件夹名(按日期管理 yyyy-MM)
			String folderName = StrUtil.createDirUseCurrentDate();
			// 根据文件夹名构造新的文件夹
			File folder = new File(leadPath + "/" + folderName);
			if (!folder.exists()) {
				folder.mkdirs();
			}
			// 在服务器的upload路径下构造一个文件
			String folderpath = folder.getAbsolutePath() + "/" + newName;
			remoteFile = new File(folderpath);
			// 将上传的文件写入新建的文件中
			file.transferTo(remoteFile);
			// 根据新建的文件实例化输入流
			in = new FileInputStream(remoteFile);
			//读取文件内容
			if (isExcel2003(oldName)) {
				wb = new HSSFWorkbook(in);
				readResult = readWordExcelValue(wb);  //读取需要根据自己的业务需求
			} else if (isExcel2007(oldName)) {
				wb = new XSSFWorkbook(in);
				readResult = readWordExcelValue(wb);  //读取需要根据自己的业务需求
			} else {
				log.error("文件类型错误");
				//throw new ServiceException("文件类型错误");
				readResult = new HashMap<>();
				readResult.put("list", new ArrayList<>());
				readResult.put("msg", "文件类型错误");
			}
		} catch (IOException e) {
			log.error("文件类型错误: {}", e.getMessage());
			//throw new ServiceException("文件类型错误");
			readResult = new HashMap<>();
			readResult.put("list", new ArrayList<>());
			readResult.put("msg", "文件类型错误");
			e.printStackTrace();
			return readResult;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			//删除导入文件
			if (remoteFile.exists()) {
				remoteFile.delete();
			}
		}
		return readResult;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章