⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 templatematching.java

📁 利用poi插件和Java语言在web中实现excel的合并功能
💻 JAVA
字号:
/**
 * 
 */
package com.dao.join;

import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * <p/> Title:匹配模板与文件
 * </p>
 * <p/> Description:
 * </p>
 * <p/> Date:2006-7-14 17:24:05
 * </p>
 * 
 * @author TEAM3 曲静波
 * @version 1.0
 */

public class TemplateMatching {
	
	private ValidatorTemplate vt = new ValidatorTemplate();

	/**
	 * 验证文件(要合并的excel)是否与模板一致
	 * 
	 * @param templatePath
	 *            模板地址
	 * @param filePath
	 *            文件地址(要合并的excel)
	 * @param rowSize
	 *            在读rowCount时要用到的,建议设为20-50.设的越大精度越高,
	 *            但效率低.可以使用ValidatorTemplate.VALIDATOR_TEMPLATE_ROW_SIZE
	 * @throws ExcelException
	 */
	public void validator(String templatePath, String filePath, int rowSize)
			throws ExcelException {
		System.out.println("entered T validator");

		// 模板相关POI
		POIFSFileSystem templateFs = null;
		HSSFWorkbook templateWb = null;
		// 文件相关POI
		POIFSFileSystem fileFs = null;
		HSSFWorkbook fileWb = null;
		// 模板行数
		int tRowCount = 0;
		// 模板sheet数
		int tSheetCount = 0;
		// 模板sheet
		HSSFSheet tSheet = null;
		// 文件sheet
		HSSFSheet fSheet = null;

		// 模板row
		HSSFRow tRow = null;
		// 文件row
		HSSFRow fRow = null;

		try {
			templateFs = new POIFSFileSystem(new FileInputStream(templatePath));
			templateWb = new HSSFWorkbook(templateFs);

			fileFs = new POIFSFileSystem(new FileInputStream(filePath));
			fileWb = new HSSFWorkbook(fileFs);

			// 验证文件的sheet数如果小于模板的sheet数则返回false,认为非法文件
			// 若大于的话,则在合并时将以最小sheet为准
			if (!validatorSheet(templateWb, fileWb)) {
				throw new ExcelException(ExcelExceptionMsg.FILE_ERROR1_MSG);
			}
			// 取sheet总数
			tSheetCount = vt.getSheetCount(templateWb);
			// 遍历sheet
			for (int i = 0; i < tSheetCount; i++) {
				// 取模板sheet
				tSheet = templateWb.getSheetAt(i);
				// 取文件sheet
				fSheet = fileWb.getSheetAt(i);

				// 验证两sheet的行数是否有效
				// 模板行数小于文件行数则有效
				if (!this.validatorRowCount(tSheet, fSheet, rowSize)) {
					throw new ExcelException(ExcelExceptionMsg.FILE_ERROR2_MSG);
				}
				// 取模板的行数
				tRowCount = vt.getRowCount(tSheet, rowSize);

				// 以模板行数为基础,遍历验证row
				for (int j = 0; j < tRowCount; j++) {
					// 取模板row
					tRow = tSheet.getRow(j);
					// 取文件row
					fRow = fSheet.getRow(j);
					// 验证模板row与文件row当中的cell是否一致
					if (!this.validatorRow(tRow, fRow)) {
						throw new ExcelException(
								ExcelExceptionMsg.FILE_ERROR3_MSG);
					}
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new ExcelException(
					ExcelExceptionMsg.TEMPLATE_FILE_PATH_ERROR_MSG);
		}

	}

	/**
	 * 验证文件的sheet数如果小于模板的sheet数则返回false,认为非法文件
	 * 
	 * @param templateWb
	 *            模板HSSFWorkbook
	 * @param fileWb
	 *            要合并文件的HSSFWorkbook
	 * @return 返回模板份数是否大于文件份数
	 */
	protected boolean validatorSheet(HSSFWorkbook templateWb,
			HSSFWorkbook fileWb) {
		System.out.println("entered T validatorSheet");
		// 模板sheet数
		int tSheetCount = 0;
		// file sheet数
		int fSheetCount = 0;
		// 模板sheet数
		tSheetCount = vt.getSheetCount(templateWb);
		// file sheet数
		fSheetCount = vt.getSheetCount(fileWb);

		// 如果文件sheet数小于模板sheet数,则返回false
		if (fSheetCount < tSheetCount)
			return false;
		return true;
	}

	/**
	 * 判断文件行数是否大于模板行数
	 * 
	 * @param templateSheet
	 *            模板sheet
	 * @param fileSheet
	 *            文件sheet
	 * @param rowSize
	 *            继续下查数
	 * 
	 * @return 返回文件行数是否大于模板行数,返回boolean值
	 */
	protected boolean validatorRowCount(HSSFSheet templateSheet,
			HSSFSheet fileSheet, int rowSize) {
		System.out.println("entered T validatorRowCount");
		if (null == templateSheet || null == fileSheet)
			return false;
		// 模板行数
		int tRowCount = vt.getRowCount(templateSheet, rowSize);
		// 要合并的文件的行数
		int fRowCount = vt.getRowCount(fileSheet, rowSize);

		// 如果模板的行数大于文件的行数,则返回false
		if (tRowCount > fRowCount) {
			return false;
		}

		return true;
	}

	/**
	 * 验证每行的模板与文件的cell是否一致
	 * 
	 * @param templateRow
	 *            模板row
	 * @param fileRow
	 *            文件row
	 * @return 返回boolean值,模板与文件cell是否一致
	 */
	protected boolean validatorRow(HSSFRow templateRow, HSSFRow fileRow) {
		System.out.println("entered T validatorRow");
		if (null == templateRow || null == fileRow) {
			return false;
		}

		// 模板行列数
		int tRowSize = vt.getColumnCount(templateRow);
		// 文件行列数
		int fRowSize = vt.getColumnCount(fileRow);

		// 若文件的列数与模板的列数不相等,则返回false
		if (tRowSize != fRowSize)
			return false;

		// 返回模板行的列数(cell有效列数,不包括合并的cell)
		int tRowCount = vt.getCellCount(templateRow);
		// 返回文件行的列数(cell有效列数,不包括合并的cell)
		int fRowCount = vt.getCellCount(fileRow);

		// 若文件的cell有效列数(不包括合并的cell)与模板的cell有效列数(不包括合并的cell)不相等,则返回false
		// 或文件cell有效列数(不包括合并的cell)与模板cell有效列数(不包括合并的cell)相等且都等于-1则返回false
		if (tRowCount != fRowCount || -1 == tRowCount)
			return false;

		// 遍历出该行的每一个CELL,进行验证是否一致
		for (int i = 0; i < tRowSize; i++) {
			// 取模板cell
			HSSFCell tCell = templateRow.getCell((short) i);
			// 取文件cell
			HSSFCell fCell = fileRow.getCell((short) i);
			// 判断模板cell 与 文件cell是否一则,不一致则返回
			if (!this.validatorCell(tCell, fCell)) {
				return false;
			}
		}

		return true;

	}

	/**
	 * 验证cell的合法性,一般分为合并后的cell与有值的cell
	 * 
	 * @param templateCell
	 *            模板cell
	 * @param fileCell
	 *            文件cell
	 * @return 返回boolean值,两cell是否一致
	 */
	protected boolean validatorCell(HSSFCell templateCell, HSSFCell fileCell) {
		System.out.println("entered T validatorCell");
		// 若模板cell为空且文件cell为空,返回true
		if (null == templateCell && null == fileCell) {
			return true;
		}

		// 若模板cell不为空且文件cell为空,返回false
		if (null != templateCell && null == fileCell) {
			return false;
		}

		// 若模板cell为空且文件cell不为空,返回false
		if (null == templateCell && null != fileCell) {
			return false;
		}

		// 若模板cell为合并的单元格,而文件cell不为合并的单元格,则不一致,返回false
		if (HSSFCell.CELL_TYPE_BLANK == templateCell.getCellType()
				&& HSSFCell.CELL_TYPE_BLANK != fileCell.getCellType()) {
			return false;
		}

		// 若模板cell不为合并的单元格,而文件cell为合并的单元格,则不一致,返回false
		if (HSSFCell.CELL_TYPE_BLANK != templateCell.getCellType()
				&& HSSFCell.CELL_TYPE_BLANK == fileCell.getCellType()) {
			return false;
		}

		return true;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -