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;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章