ylx hace 7 años
padre
commit
511e380838

+ 45 - 0
ccb_fund_trusteeship/src/main/java/com/fuzamei/utils/Area.java

@@ -0,0 +1,45 @@
+package com.fuzamei.utils;
+
+public class Area{
+	public static final int NUMERIC = 1; 
+	public static final int STRING = 2; 
+	public static final int DATE = 3; 
+	private Integer fromCol;	//从第几列开始(第一列是0)
+	private Integer toCol;		//到第几列为止(第一列是0)
+	private Integer fromRow;	//从第几行开始(第一行是0)
+	private Integer toRow;		//到第几行为止(第一行是0)
+	private int compareType;	//比较的数据类型
+	
+	//针对一块区域
+	public Area(int fromCol,int toCol,int fromRow,int toRow,int compareType) {
+		this.fromCol=fromCol;
+		this.toCol=toCol;
+		this.fromRow=fromRow;
+		this.toRow=toRow;
+		this.compareType = compareType;
+	}
+	//针对一个cell区域
+	public Area(int colPos,int rowPos,int compareType) {
+		this.fromCol=colPos;
+		this.toCol=colPos;
+		this.fromRow=rowPos;
+		this.toRow=rowPos;
+		this.compareType = compareType;
+	}
+	public Integer getFromCol() {
+		return fromCol;
+	}
+
+	public Integer getToCol() {
+		return toCol;
+	}
+	public Integer getFromRow() {
+		return fromRow;
+	}
+	public Integer getToRow() {
+		return toRow;
+	}
+	public int getCompareType() {
+		return compareType;
+	}
+}

+ 122 - 88
ccb_fund_trusteeship/src/main/java/com/fuzamei/utils/ExcelUtil.java

@@ -1,14 +1,12 @@
 package com.fuzamei.utils;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Date;
 
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.usermodel.Cell;
@@ -18,64 +16,110 @@ import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 import com.mchange.util.AssertException;
-
-public class ExcelUtil {
+/**
+ * 
+ * @author ylx
+ * @since 2018-1-10 10:37 AM
+ *	用于比对两个Excel表中数据的比对,如果有一个数据不一致返回false,只有全部一致返回true
+ */
+public final class ExcelUtil {
 	private ExcelUtil(){
 		throw new AssertException("instaniation is not permitted");
 	}
 	
-	public static final boolean matchExcel(File file1,File file2,Column...c){
-		boolean flag=false;
-		InputStream is1=null;
-		InputStream is2=null;
-		Workbook wb1=null;
-		Workbook wb2=null;
-		try {
-			is1 = new FileInputStream(file1);
-			is2 = new FileInputStream(file2);
-		} catch (FileNotFoundException e) {
-			throw new RuntimeException("未找到文件");
-		}
-		try {
-			wb1=new XSSFWorkbook(is1);
-		} catch (IOException e) {
-			try {
-				wb1=new HSSFWorkbook(is1);
-			} catch (IOException e1) {
-				throw new RuntimeException("非Excel文件");
-			}finally{
-				closeStream(wb1,wb2,is1,is2);
-			}
-		}
-		try {
-			wb2=new XSSFWorkbook(is2);
-		} catch (IOException e) {
-			try {
-				wb2=new HSSFWorkbook(is2);
-			} catch (IOException e1) {
-				throw new RuntimeException("非Excel文件");
-			}finally{
-				closeStream(wb2,is2);
-			}
+	/**
+	* @Title: matchExcel
+	* @Description: TODO(针对多个区域进行比较,同时还有数据类型的选定)
+	* @param @param file1(要进行比较的文件1)
+	* @param @param file2(要进行比较的文件2)
+	* @param @param c(Column类对象,包含有开始列,开始行,结束行还有要比较的数据类型)
+	* @return boolean    返回类型true说明划定的区域中数据一致。false说明划定的区域中数据有不一致的情况
+	* @author ylx
+	* @date 2018年1月10日 上午11:27:26
+	 */
+	public static final boolean matchExcel(File file1,File file2,Area...c){
+		if(c.length==0){
+			throw new RuntimeException("必须放置Colunm");
 		}
+		Workbook wb1 = readWorkbook(file1);
+		Workbook wb2 = readWorkbook(file2);
 		Sheet sheet1 = wb1.getSheet("Sheet1");
 		Sheet sheet2 = wb2.getSheet("Sheet1");
-		Row row1 = sheet1.getRow(1);
-		Row row2 = sheet2.getRow(1);
-		Cell cell1 = row1.getCell(0);
-		Cell cell2 = row2.getCell(0);
-		double numericCellValue1 = cell1.getNumericCellValue();
-		double numericCellValue2 = cell2.getNumericCellValue();
-		flag = (numericCellValue1==numericCellValue2);
-		
-		closeStream(wb1,wb2,is1,is2);
-		
-		return flag;
+		for (Area column : c) {
+			Integer fromCol = column.getFromCol();
+			Integer toCol = column.getToCol();
+			Integer fromRow = column.getFromRow();
+			Integer toRow = column.getToRow();
+			int compareType = column.getCompareType();
+			for (int j = fromCol; j <= toCol; j++) {
+				for (int i = fromRow; i <= toRow; i++) {
+					Row row1 = sheet1.getRow(i);
+					Row row2 = sheet2.getRow(i);
+					Cell cell1 = row1.getCell(j);
+					Cell cell2 = row2.getCell(j);
+					if(Area.NUMERIC==compareType){
+						double numericCellValue1 = cell1.getNumericCellValue();
+						double numericCellValue2 = cell2.getNumericCellValue();
+						if(numericCellValue1!=numericCellValue2)
+							return false;	
+					}else if(Area.STRING==compareType){
+						String str1 = cell1.getStringCellValue();
+						String str2 = cell2.getStringCellValue();
+						if(!str1.equals(str2))
+							return false;	
+					}else if(Area.DATE==compareType){
+						Date date1 = cell1.getDateCellValue();
+						Date date2 = cell2.getDateCellValue();
+						if(!date1.equals(date2))
+							return false;
+					}else{
+						closeStream(wb1,wb2);
+						throw new RuntimeException("读取的数据类型错误");
+					}
+					
+				}
+			}
+			
+		}
+		closeStream(wb1,wb2);
+		return true;
+	}
+	
+	/**
+	* @Title: matchExcel
+	* @Description: TODO(针对单个区域进行比较,同时还有数据类型的选定)
+	* @param @param file1(要进行比较的文件1)
+	* @param @param file2(要进行比较的文件2)
+	* @param @param c(Column类对象,包含有开始列,开始行,结束行还有要比较的数据类型)
+	* @return boolean    返回类型true说明划定的区域中数据一致。false说明划定的区域中数据有不一致的情况
+	* @author ylx
+	* @date 2018年1月10日 上午11:27:26
+	 */
+	public static final boolean matchExcel(File file1,File file2,Area c){
+		return matchExcel(file1,file2,c);
 	}
 	
-	//要改的
-	public static final boolean matchExcel(File file1,File file2){
-		return false;
+	/**
+	* @Title: matchExcel
+	* @Description: TODO(这里用一句话描述这个方法的作用)
+	* @param @param colPos 选定的列数(第一列为0)
+	* @param @param fromRow	开始的行号(第一行为0)
+	* @param @param toRow	结束的行号
+	* @param @param compareType 1代表数字,2代表字符串,3代表日期格式,除此以外的数据类型会报错
+	* @param @return    设定文件
+	* @return boolean    返回类型
+	* @author ylx
+	* @date 2018年1月10日 上午11:33:47
+	 */
+	public static final boolean matchExcel(File file1,File file2,int colPos,int fromRow,int toRow,int compareType){
+		return matchExcel(file1,file2,new Area(colPos,colPos, fromRow, toRow, compareType));
+	}
+	
+	public static final boolean matchNumericExcel(File file1,File file2,int colPos,int fromRow,int toRow){
+		return matchExcel(file1,file2,new Area(colPos,colPos, fromRow, toRow, Area.NUMERIC));
+	}
+	public static final boolean matchStringExcel(File file1,File file2,int colPos,int fromRow,int toRow){
+		return matchExcel(file1,file2,new Area(colPos,colPos, fromRow, toRow, Area.STRING));
 	}
 	
 	private static final void closeStream(Closeable...c){//关闭流的内部方法
@@ -90,47 +134,37 @@ public class ExcelUtil {
 		}
 	}
 	
-	private class Column{
-		private Integer colPos;	//列的位数
-		private Integer fromRow;//从第几行开始
-		private Integer toRow;	//到第几行为止
-		public Column(int colPos,int fromRow,int toRow) {
-			this.colPos=colPos;
-			this.fromRow=fromRow;
-			this.toRow=toRow;
-		}
-		public Integer getColPos() {
-			return colPos;
-		}
-		public Integer getFromRow() {
-			return fromRow;
+	private static final Workbook readWorkbook(File file){//读取workbook
+		InputStream is=null;
+		Workbook wb=null;
+		try {
+			is = new FileInputStream(file);
+		} catch (FileNotFoundException e) {
+			closeStream(is);
+			throw new RuntimeException("未找到文件");
 		}
-		public Integer getToRow() {
-			return toRow;
+		try {
+			wb=new XSSFWorkbook(is);
+		} catch (IOException e) {
+			try {
+				wb=new HSSFWorkbook(is);
+			} catch (IOException e1) {
+				throw new RuntimeException("非Excel文件");
+			}finally{
+				closeStream(is);
+			}
 		}
-		
+		return wb;
 	}
 	
 	public static void main(String[] args) throws Exception{
-		File file=new File("C:\\Users\\fuzamei\\Desktop\\估值核算表.xlsx");
-		FileInputStream fis = new FileInputStream(file);
-		Workbook wb=new XSSFWorkbook(fis);
-		Sheet sheet = wb.getSheetAt(0);
-		Row row = sheet.getRow(2);
-		Cell cell = row.getCell(5);
-//		System.out.println(cell.getStringCellValue());
-		System.out.println(cell.getRichStringCellValue());
+		File file1=new File("C:\\Users\\fuzamei\\Desktop\\测试用上传文件\\估值核算表.xlsx");
+		File file2=new File("C:\\Users\\fuzamei\\Desktop\\测试用上传文件\\估值核算表2.xlsx");
+		Area[] columns=new Area[]{new Area(4,5,2,4,Area.NUMERIC),new Area(7,8,2,4,Area.NUMERIC),new Area(9,9,3,3,Area.NUMERIC)};
+		boolean matchExcel = matchExcel(file1, file2, columns);
+		System.out.println(matchExcel);
 	}
 	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
-	
 }
+
+

+ 41 - 27
ccb_fund_trusteeship/src/main/java/com/fuzamei/web/ValuationAccountingAction.java

@@ -31,6 +31,7 @@ import com.fuzamei.entity.ValuationAccount;
 import com.fuzamei.service.AttachmentService;
 import com.fuzamei.service.UserAuthoricationService;
 import com.fuzamei.service.ValuationAccountingService;
+import com.fuzamei.utils.Area;
 import com.fuzamei.utils.ExcelUtil;
 import com.fuzamei.utils.JSONUtil;
 import com.fuzamei.utils.PageDTO;
@@ -58,7 +59,6 @@ public class ValuationAccountingAction {
 	
 	private static final int ROW_NUM = 10; // 分页每页显示数据的数量
 	/**
-	 * 
 	* @Title: queryOperationHistoryInformation
 	* @Description: TODO(估值核算的查询结果,包括所有的查询条件也包括进去了)
 	* 				前端传过来的json格式为:
@@ -73,7 +73,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月18日 下午3:17:30
-	* @throws
 	 */
 	@RequestMapping(value="/queryValuationAccountingInformation",method=RequestMethod.POST)
 	@ResponseBody
@@ -110,9 +109,7 @@ public class ValuationAccountingAction {
 		}
 	}
 	
-	
 	/**
-	 * 
 	* @Title: queryOperationHistoryInformation
 	* @Description: TODO(查看估值核算基金的下载信息的小弹框)
 	* @param @param data
@@ -125,7 +122,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月28日 下午1:48:43
-	* @throws
 	 */
 	@RequestMapping(value="/checkoutDownloadInformation",method=RequestMethod.POST)
 	@ResponseBody
@@ -146,7 +142,6 @@ public class ValuationAccountingAction {
 	
 	
 	/**
-	 * 
 	* @Title: checkoutDownloadInformation
 	* @Description: TODO(管理人首次上传估值报表弹出的界面需要显示的ID号)
 	* 				前端传过来的json数据格式
@@ -157,7 +152,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月28日 下午3:39:18
-	* @throws
 	 */
 	@RequestMapping(value="/fistUploadFundDataByAdmin",method=RequestMethod.POST)
 	@ResponseBody
@@ -174,10 +168,7 @@ public class ValuationAccountingAction {
 		}
 	}
 	
-	
-	
 	/**
-	 * 
 	* @Title: download
 	* @Description: TODO(估值核算界面,下载功能实现)
 	* 				前端通过get或post发来请求
@@ -188,7 +179,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月18日 下午5:26:46
-	* @throws
 	 */
 	@RequestMapping(value="/downloadValuationPOI")
 	@ResponseBody
@@ -225,7 +215,6 @@ public class ValuationAccountingAction {
 	}
 	
 	/**
-	 * 
 	* @Title: upload
 	* @Description: TODO(实现管理员估值核算的报表文件上传功能---------->>暂时按照一次上传一个文件进行实现)
 	* 
@@ -245,7 +234,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月18日 下午5:29:38
-	* @throws
 	 */
 	@RequestMapping(value="/uploadValuationPOIByAdminForTheFirstTime",method=RequestMethod.POST)
 	@ResponseBody
@@ -303,7 +291,6 @@ public class ValuationAccountingAction {
 	}
 	
 	/**
-	 * 
 	* @Title: upload
 	* @Description: TODO(实现银行估值核算的报表文件上传功能---------->>暂时按照一次上传一个文件进行实现)
 	* 
@@ -317,7 +304,6 @@ public class ValuationAccountingAction {
 	* @return Map<String,Object>    返回类型
 	* @author ylx
 	* @date 2017年12月18日 下午8:17:47
-	* @throws
 	 */
 	@RequestMapping(value="/uploadValuationPOIByBankOrByAdmin",method=RequestMethod.POST)
 	@ResponseBody
@@ -379,10 +365,19 @@ public class ValuationAccountingAction {
 //				oldFile.delete();
 				file.transferTo(tempFile);
 				
-				boolean matchResult = ExcelUtil.matchExcel(tempFile, bankFile);
-				
-				//比对完之后暂时先这个文件删除
-				tempFile.delete();
+				boolean matchResult;
+				try {
+					matchResult = ExcelUtil.matchExcel(tempFile, 
+															   bankFile,
+															   new Area(4, 5, 2, 4, Area.NUMERIC),
+															   new Area(7, 8, 2, 4, Area.NUMERIC),
+															   new Area(9, 9, 3, 3, Area.NUMERIC));
+				} catch (Exception e) {
+					throw new RuntimeException(e.getMessage());
+				}finally{
+					//对比完要暂时删除文件
+					tempFile.delete();
+				}
 				
 				
 				//获取比对结果
@@ -391,14 +386,13 @@ public class ValuationAccountingAction {
 				mapToService.put("userDetail", userDetail);			//将用户的全部信息传入service层
 				mapToService.put("fundId", fundId);
 				mapToService.put("url", urlOfCustodianNewFile);		//将文件下载路径url通过map传到service层进行校验
-				mapToService.put("attachmentName", filename);			//将文件以附件名的形式通过map传递到service层中进行校验
+				mapToService.put("attachmentName", filename);		//将文件以附件名的形式通过map传递到service层中进行校验
 				mapToService.put("battleResult", result);			//将文件比对结果封装进去
 				mapToService.put("attachmentId", valuationAccount.getAttachment_id_of_custodian());	//获取之前上传的附件id号在附件表中修改
 				valuationAccountingService.updateInformationByAdmin(mapToService);
 				
 				//文件的覆盖放到最后操作(重要!!!)
 				oldFile.delete();
-//				file.transferTo(newFile);
 				String custodianNewFilePath = custodianOldFilePath.substring(0, custodianOldFilePath.lastIndexOf("/")+1)+filename;
 				File newFile=new File(custodianNewFilePath);
 				file.transferTo(newFile);
@@ -422,7 +416,7 @@ public class ValuationAccountingAction {
 				String relativePath = RelativePathUtil.formatPath("/"+userId,"");
 				
 				//银行上传文件的新路径
-				String bankNewFilePath = path + relativePath + filename;			//=========================>>新上传的文件路径怎么确定到时候再说TODO
+				String bankNewFilePath = path + relativePath + filename;//=========================>>新上传的文件路径怎么确定到时候再说TODO
 				File dir=new File(path + relativePath);
 				if(!dir.exists()){
 					dir.mkdirs();
@@ -434,13 +428,23 @@ public class ValuationAccountingAction {
 				 * 如过不是excel文件类型,导入也会报错,然后就把这个新上传的文件删了
 				 * 这里对两份文件进行估值报价的比对得出比对的结果是一致还是不一致
 				 */
-				boolean matchResult = ExcelUtil.matchExcel(newFile, oldFile);
-				newFile.delete();
+				boolean matchResult;
+				try {
+					matchResult = ExcelUtil.matchExcel(newFile, 
+															   oldFile,
+															   new Area(4, 5, 2, 4, Area.NUMERIC),
+															   new Area(7, 8, 2, 4, Area.NUMERIC),
+															   new Area(9, 9, 3, 3, Area.NUMERIC));
+				} catch (Exception e) {
+					throw new RuntimeException(e.getMessage());
+				}finally{
+					newFile.delete();
+				}
 				
 				//获取比对结果
 				String result = matchResult?BattleResult.CONSISTENT:BattleResult.INCONSISTENT;
 				
-				Integer attachmentId = attachmentService.generateAtachmentId();					//生成一个不重复的附件Id号==================>>生成id号的机制待定TODO
+				Integer attachmentId = attachmentService.generateAtachmentId();//生成一个不重复的附件Id号==================>>生成id号的机制待定TODO
 				
 				Map<String, Object> mapToService =new LinkedHashMap<String, Object>();
 				mapToService.put("valuationAccount", valuationAccount);  //将valuationAccount对象传过去,对其中的对账结果进行判断
@@ -487,8 +491,18 @@ public class ValuationAccountingAction {
 				 * 如过不是excel文件类型,导入也会报错,然后就把这个新上传的文件删了(这里旧文件不能先删了)
 				 * 这里对两份文件进行估值报价的比对得出比对的结果是一致还是不一致
 				 */
-				boolean matchResult = ExcelUtil.matchExcel(tempBankFile, oldCustodianFile);
-				tempBankFile.delete();
+				boolean matchResult;
+				try {
+					matchResult = ExcelUtil.matchExcel(tempBankFile, 
+															   oldCustodianFile,
+															   new Area(4, 5, 2, 4, Area.NUMERIC),
+															   new Area(7, 8, 2, 4, Area.NUMERIC),
+															   new Area(9, 9, 3, 3, Area.NUMERIC));
+				} catch (Exception e) {
+					throw new RuntimeException(e.getMessage());
+				}finally{
+					tempBankFile.delete();
+				}
 				
 				//获取比对结果
 				String result = matchResult?BattleResult.CONSISTENT:BattleResult.INCONSISTENT;