EAS如何实现导入多页签的Excel功能?

涎涎原创约 4375 字大约 15 分钟...KingdeeKingdee

127-EAS中如何实现导入多页签的Excel功能?.md

注意

本博文仅供学术研究和交流参考,严禁将其用于商业用途。如因违规使用产生的任何法律问题,使用者需自行负责。

需求场景

最近项目中有遇到需要在EAS中实现导入的功能,但是是多页签Excel导入,由于Eas中标准不支持多页签的导入,那么此刻需求来了,EAS中如何实现导入多页签的Excel功能呢?

实现思路

  1. 禁用标准的导入弹出框

  2. 自定义导入模板

  3. 编码读取Excel数据

  4. 过滤以判断数据的合法性,不合法则给出提示

  5. 数据通过校验则存入数据库,不通过则给出提示并中断导入

实现步骤

禁用标准的导入弹出框

  1. 找到导入按钮对应的方法
  1. 在List类中复写 actionImportData 方法体,去掉super即可禁用标准导入

画出点击导入出弹的自定义弹出框

  1. 画出弹出框
  1. 为对应的按钮绑定事件
  1. 补充:新建与发布

自定义导入的Excel模板并放入对应的路径

  1. 自定义Excel模板
  1. 放入对应的路径,例笔者的路径为

E:\soft\Kingdee\easClient82\eas\client\template\LeaseConBillTemplate

Q:\项目名\runtime\server\deploy\fileserver.ear\easWebClient\template\LeaseConBillTemplate

编码实现对应的功能

  1. 本地开发有个前置操作,参考博文 EAS引入引出模板配置完成后点击引出模板之后报错如何解决?open in new window

  2. ListUI类

package com.kingdee.eas.custom.bill.leaseconbill.client;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.bos.ui.face.IUIFactory;
import com.kingdee.bos.ui.face.IUIWindow;
import com.kingdee.bos.ui.face.UIFactory;
import com.kingdee.eas.basedata.org.OrgType;
import com.kingdee.eas.common.client.UIContext;
import com.kingdee.eas.custom.bill.leaseconbill.client.util.ExcelImportUtil;
import com.kingdee.eas.custom.bill.leaseconbill.client.util.LeaseConBillImportHelper;
import com.kingdee.eas.custom.bill.leaseconbill.client.util.LeaseconbillImportUI;
import com.kingdee.eas.util.client.MsgBox;

/**
 * <p>Title: LeaseConBillListUI</p>
 * <p>
 *    Description:
 *    List前端界面
 * </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * @author xx
 * @date 2020-5-19   上午11:49:38
 * @version 1.0
 */
public class LeaseConBillListUI extends AbstractLeaseConBillListUI
{
    private static final Logger logger = CoreUIObject.getLogger(LeaseConBillListUI.class);
    
	private File importDataFile = null;//导入文件
    
    /**
     * <p>Title: LeaseConBillListUI</p>
     * <p>
     *    Description:
     *    构造方法
     * </p>
     * <p>Copyright: Copyright (c) 2020</p>
     * @author xx
     * @date 2020-5-19   上午11:50:12
     * @param param the bare_field_name
     * @throws Exception
     * @version 1.0
     */
    public LeaseConBillListUI() throws Exception
    {
        super();
    }

    @Override
	protected String getPropertyOfBizOrg(OrgType orgType) {
		return "FICompany.id";
	}

    /**
     * <p>Title: </p>
     * <p>
     *    Description:
     *    导入数据
     * </p>
     * <p>Copyright: Copyright (c) 2020</p>
     * @author xx
     * @date 2020-5-19   上午11:53:42
     * @param param the bare_field_name
     * @param e
     * @throws Exception
     * @see com.kingdee.eas.st.common.client.STBillBaseListUI#actionImportData_actionPerformed(java.awt.event.ActionEvent)
     * @version 1.0
     */
    @Override
    public void actionImportData_actionPerformed(ActionEvent e)
    		throws Exception {
    	File file = getImportFile();
		if (file == null) {
		  return;
		}
		this.importDataFile = file;
		ExcelImportUtil importUtil = new ExcelImportUtil(this,importDataFile);
		Map<String, List<Map<String, String>>> readFile = importUtil.readFile();
		LeaseConBillImportHelper helper = new LeaseConBillImportHelper(readFile);

		Map<String,String> mapping = new HashMap<String, String>();
		mapping.put("projectCode", "projectName=name,costCenter=costCenter");
		mapping.put("materialNumber", "materialName=name,materialType=model,unit=baseUnit.name");
		helper.dealWithData();
		MsgBox.showConfirm2("导入成功!");
		refreshList();
    }
    
    /**
     * File</br>
     * 
     * <p>Title: getImportFile</p>
     * <p>
     *    Description:
     *     获取需要导入的文件对象
     * </p>
     * <p>Copyright: Copyright (c) 2020</p>
     * @author xx
     * @date 2020-5-19   上午11:55:28
     * @param param the bare_field_name
     * @return the bare_field_name
     * @return
     * @throws Exception
     * @version 1.0
     */
	private File getImportFile() throws Exception {
		UIContext uiCtx = new UIContext(this);
		uiCtx.put("selectedFile", this.importDataFile);
		IUIFactory createUIFactory = UIFactory.createUIFactory("com.kingdee.eas.base.uiframe.client.UIModelDialogFactory");
		
		IUIWindow uiFile = createUIFactory.create(LeaseconbillImportUI.class.getName(), uiCtx);
		uiFile.show();
		LeaseconbillImportUI fileUI = (LeaseconbillImportUI) uiFile.getUIObject();
		return fileUI.getSelectedFile();
	}
}
  1. ExcelImportUtil类
package com.kingdee.eas.custom.bill.leaseconbill.client.util;

import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.kingdee.eas.util.client.MsgBox;

/**
 * <p>Title: ExcelImportUtil</p>
 * <p>
 *    Description:
 *    Excel导入类
 * </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * @author xx
 * @date 2020-5-19   下午01:51:00
 * @version 1.0
 */
public class ExcelImportUtil {
	/**
	 * 需要读取的文件
	 */
	private File selectFile = null;
	/**
	 * 父UI
	 */
	private Component owner = null;
	/**
	 * 表头行 从下标0开始
	 */
	private int keyLine = 2;
	/**
	 * 表头行 中文 从下标0开始
	 */
	private int keyCnLine = 3;
	/**
	 * 数据开始行号  从下标0开始
	 */
	private int dataLine = 4;
	/**
	 * 时间格式
	 */
	private SimpleDateFormat df = null;

	/**
	 * <p>Title: </p>
	 * <p>
	 *    Description:
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-25   下午04:03:59
	 * @param param the bare_field_name
	 * @version 1.0
	 */
	public ExcelImportUtil() {}
	
	/**
	 * <p>Title: ExcelImportUtil</p>
	 * <p>
	 *    Description:
	 *    (构造函数描述XXXX)
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:00:11
	 * @param param the bare_field_name
	 * @param owner 父UI对象
	 * @param selectFile 需要导入的Excel文件对象
	 * @version 1.0
	 */
	public ExcelImportUtil(Component owner, File selectFile) {
		this(owner, selectFile, null);
	}

	/**
	 * <p>Title: ExcelImportUtil</p>
	 * <p>
	 *    Description:
	 *    (构造函数描述XXXX)
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:00:51
	 * @param param the bare_field_name
	 * @param owner 父UI对象
	 * @param selectFile 需要导入的Excel文件对象
	 * @param param 导入时需要用的参数集合,
	 *       包含表头(执行系统能识别的英文key的行号,默认为2,
	 *       传入时键值为:keyLine),数据开始行(excel表中数据开始的行号,默认为4,
	 *       传入时键值为:dataLine),时间格式的格式化字符串(例:"yyyy-MM-dd",默认为:"yyyy-MM-dd")允许传空
	 * @version 1.0
	 */
	public ExcelImportUtil(Component owner, File selectFile, Map param) {
		this.selectFile = selectFile;
		this.owner = owner;
		if (param != null) {
			Object keyLineObj = param.get("keyLine");
			if (keyLineObj != null && keyLineObj instanceof Integer) {
				this.keyLine = Integer.parseInt(keyLineObj.toString());
			}

			Object dataLineObj = param.get("dataLine");
			if (dataLineObj != null && dataLineObj instanceof Integer) {
				this.dataLine = Integer.parseInt(dataLineObj.toString());
			}
			Object dateForm = param.get("dateFormat");
			if (dateForm != null) {
				df = new SimpleDateFormat(dateForm.toString());
			} 
		}
		if(df==null){
			df = new SimpleDateFormat("yyyy-MM-dd");
		}
	}

	/** 
	 * Map<String,List<Map<String,String>>></br>
	 * 
	 * <p>Title: readFile</p>
	 * <p>
	 *    Description:
	 *    读取文件
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:02:50
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @return
	 * @version 1.0
	 */
	public Map<String, List<Map<String, String>>> readFile() {
		FileInputStream out = null;
		try {
			Workbook workbook = null;
			out = new FileInputStream(selectFile);
			if (selectFile.getName().endsWith(".xls")) {
				workbook = new XSSFWorkbook(out);
			} else {
				workbook = new HSSFWorkbook(out);
			}
			int numberOfSheets = workbook.getNumberOfSheets();//读取页签数量
			Map<String, List<Map<String, String>>> data = new HashMap<String, List<Map<String, String>>>();
			for (int i = 0; i < numberOfSheets; i++) {
				Sheet sheet = workbook.getSheetAt(i);
				String sheetName = sheet.getSheetName();
				data.put(sheetName, readFileData(sheet));
			}
			return data;
		} catch (FileNotFoundException e) {
			MsgBox.showWarning(owner, "导入文件为找到!");
		} catch (IOException e) {
			MsgBox.showError(owner, "读取文件出现错误!", ExceptionUtils.getFullStackTrace(e));
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	/** 
	 * List<Map<String,String>></br>
	 * <p>Title: readFileData</p>
	 * <p>
	 *    Description:
	 *    按照页签读取excel中的数据
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:03:44
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param sheet excel页签对象
	 * @return List<Map<String, String>> 每行数据存放在Map中,最后所有的行数据存放如List集合
	 * @version 1.0
	 */
	private List<Map<String, String>> readFileData(Sheet sheet) {
		Map<String, String> lineValue;
		List<Map<String, String>> data = new ArrayList<Map<String, String>>();
		List<String> columnNames = getColumnNames(sheet);
		for (int i = dataLine; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			lineValue = new TreeMap<String, String>();
			for (int j = 0; j < columnNames.size(); j++) {
				lineValue.put(columnNames.get(j), getCellFormatValue(row.getCell(j)));
			}
			data.add(lineValue);
		}
		return data;
	}

	/** 
	 * List<String></br>
	 * 
	 * <p>Title: getColumnNames</p>
	 * <p>
	 *    Description:
	 *    获取execl中指定页签的表头
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:05:04
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param sheet 页签对象
	 * @return
	 * @version 1.0
	 */
	private List<String> getColumnNames(Sheet sheet) {
		List<String> cols = new ArrayList<String>();
		Row row = sheet.getRow(keyLine);
		Row rowcn = sheet.getRow(keyCnLine);
		for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
			Cell cell = row.getCell(i);
			Cell cellcn = rowcn.getCell(i);//存入中文的列名
			cols.add(getCellFormatValue(cell)+ "-" + getCellFormatValue(cellcn));
		}
		return cols;
	}

	/** 
	 * String</br>
	 * 
	 * <p>Title: getCellFormatValue</p>
	 * <p>
	 *    Description:
	 *    解析Excel中指定单元格的数据
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:05:32
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param cell excel中的单元格对象
	 * @return
	 * @version 1.0
	 */
	private String getCellFormatValue(Cell cell) {
		String cellValue = "";
		if (cell != null) {
			//判断cell类型
			switch (cell.getCellType()) {
				case Cell.CELL_TYPE_NUMERIC: {
					if (DateUtil.isCellDateFormatted(cell)) {
						cellValue = df.format(cell.getDateCellValue());
					} else {
						DataFormatter formatter = new DataFormatter();
						cellValue = formatter.formatCellValue(cell);
					}
					break;
				}
				case Cell.CELL_TYPE_STRING: {
					cellValue = cell.getStringCellValue();
					break;
				}
				default:
					cellValue = "";
			}
		}
		return cellValue;
	}
}
  1. LeaseConBillImportHelper类
package com.kingdee.eas.custom.bill.leaseconbill.client.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.common.util.list.exception.ObjectNotFoundException;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.framework.DynamicObjectFactory;
import com.kingdee.bos.metadata.MetaDataLoaderFactory;
import com.kingdee.bos.metadata.entity.DataType;
import com.kingdee.bos.metadata.entity.EntityObjectInfo;
import com.kingdee.bos.metadata.entity.LinkPropertyInfo;
import com.kingdee.bos.metadata.entity.OwnPropertyInfo;
import com.kingdee.bos.metadata.entity.PropertyInfo;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.eas.base.codingrule.CodingRuleManagerFactory;
import com.kingdee.eas.basedata.scm.common.IRowType;
import com.kingdee.eas.basedata.scm.common.RowTypeFactory;
import com.kingdee.eas.basedata.scm.common.RowTypeInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillEntryCollection;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillEntryInfo;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillFactory;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillInfo;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillPayPlanEntryCollection;
import com.kingdee.eas.custom.bill.leaseconbill.LeaseConBillPayPlanEntryInfo;
import com.kingdee.util.NumericExceptionSubItem;

/**
 * <p>Title: </p>
 * <p>
 *    Description:
 *    
 * </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * @author xx
 * @date 2020-5-19   下午02:01:14
 * @version 1.0
 */
public class LeaseConBillImportHelper {
	/**
	 * 时间格式化方式为:yyyy-MM-dd
	 */
	protected static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * Excel导入后的数据
	 */
	protected Map<String, List<Map<String, String>>> data = null;
	/**
	 * 是否只导入一行数据
	 */
	protected boolean isOneLine = true;
	/**
	 * 字段带出字段的映射,例:项目编码带出项目名称和成本中心 {projectCode="projectName=name,costCenter=costCenter"}
	 * {materialNumber="materialName=name,materialType=model,unit=baseUnit.name"}
	 */
	protected Map<String,String> mapping = null;
	
	/**
	 * 错误提示信息
	 */
	protected String exceptionTipMsg;
	
	public LeaseConBillImportHelper(Map<String, List<Map<String, String>>> data) {
		super();
		this.data = data;
	}

	public LeaseConBillImportHelper(Map<String, List<Map<String, String>>> data, boolean isOneLine) {
		super();
		this.data = data;
		this.isOneLine = isOneLine;
	}
	

	
	/**
	 * Map<String,String></br>
	 * 
	 * <p>Title: getOtherField</p>
	 * <p>
	 *    Description:
	 *    得到出租人的字段映射 bostype
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-20   下午03:14:47
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @return
	 * @version 1.0
	 */
	protected Map<String, String> getLessorBostype(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("lessor", "0C5D4387");
		return map;
	}
	
	/** 
	 * void</br>
	 * 
	 * <p>Title: dealWithData</p>
	 * <p>
	 *    Description:
	 *    处理数据
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午07:08:19
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @throws Exception
	 * @version 1.0
	 */
	public void dealWithData()throws Exception{
		if(data == null){
			return;
		}
		exceptionTipMsg = "";
		/**
		 * 1. 处理单据头
		 */
		LeaseConBillInfo billInfo = dealBillHead();
		/**
		 * 2. 处理车辆信息分录
		 */
		List<Map<String, String>> vehicleList = data.get("分录-车辆信息");		//获取车辆信息页签的所有数据
		List<String> vehicleEntryNeedCols = getVehicleEntryNeedColumns();		//获取车辆信息分录必录字段
		for(int i = 0; i < vehicleList.size(); i++){
			Map<String, String> map = vehicleList.get(i);	//在所有数据中取一条数据
			LeaseConBillEntryInfo vehicleEntryInfo = new LeaseConBillEntryInfo();
			dealModel(vehicleEntryInfo, vehicleEntryNeedCols, map,"分录-车辆信息");	//为实体赋值
			billInfo.getEntries().add(vehicleEntryInfo);
		}
		/**
		 * 3. 处理付款计划分录
		 */
		List<Map<String, String>> payPlanList = data.get("分录-付款计划");		//获取付款计划页签的所有数据
		List<String> payPlanEntryNeedCols = getPayPlanEntryNeedColumns();		//获取付款计划分录必录字段
		for(int i = 0; i<payPlanList.size(); i++){
			Map<String, String> map = payPlanList.get(i);	//在所有数据中取一条数据
			LeaseConBillPayPlanEntryInfo payPlanEntryInfo = new LeaseConBillPayPlanEntryInfo();
			dealModel(payPlanEntryInfo, payPlanEntryNeedCols, map,"分录-付款计划");	//为实体赋值
			billInfo.getPayPlanEntry().add(payPlanEntryInfo);
		}
		String companyID = SysContext.getSysContext().getCurrentAdminUnit().getId().toString();
		String number = CodingRuleManagerFactory.getRemoteInstance().getNumber(billInfo, companyID);
		billInfo.setNumber(number);
		LeaseConBillFactory.getRemoteInstance().addnew(billInfo);
	}


	/** 说明:处理构造单据头
	 * @author xx
	 * 2020-5-22	下午06:58:21
	 * @throws EASBizException 
	 */
	private LeaseConBillInfo dealBillHead() throws Exception {
		LeaseConBillInfo info = new LeaseConBillInfo();
		/**
		 * 1. 获取单头必录字段
		 */
		List<String> needCols = getBillHeadNeedColumns();
		/**
		 * 2. 获取表格中单头的数据
		 */
		Map<String, String> map = data.get("单头").get(0);
		/**
		 * 3. 实体对象赋值
		 */
		dealModel(info, needCols, map,"单头");
		return info;
	}

	/**
	 * 说明:实体对象赋值
	 * @author xx
	 * 2020-5-22	下午08:10:11
	 * @param info 需要赋值的实体对象
	 * @param needCols 必录字段集合
	 * @param map 数据
	 * @param sheetName 页签名
	 * @throws EASBizException
	 * @throws Exception
	 */
	private void dealModel(IObjectValue info, List<String> needCols,
			Map<String, String> map, String sheetName) throws EASBizException, Exception {
		/**
		 * 1. 通过创建的单头对象bosType,获取通用实体对象
		 */
		EntityObjectInfo entity = MetaDataLoaderFactory.getRemoteMetaDataLoader().getEntity(info.getBOSType());
		/**
		 * 2. 开始构造赋值
		 */
		Set<String> keySet = map.keySet();
		Iterator<String> iterator = keySet.iterator();
		while(iterator.hasNext()){
			String keyEc = iterator.next();
			String key = keyEc.split("-")[0];
			String keyCn = keyEc.split("-")[1];
			String value = map.get(keyEc);
			/**
			 * 2.1 判断是否在必录字段中,如果在则不能为空
			 */
			if(needCols.size()>0 && needCols.contains(key)){
				if(value == null || "".equals(value.trim())){
					/**
					 * 要在此处将Key处理成中文名,给用户提示
					 */
					throw new EASBizException(new NumericExceptionSubItem("提示信息",sheetName + ": " + keyCn+" 键为必录项,对应的值不能为空。"));
				}
			}
			/**
			 * 2.2. 字段值为空则跳过
			 */
			if(value == null || "".equals(value)){
				continue;
			}
			/**
			 * 2.3. 获取映射key值在对应单据中的属性对象
			 */
			PropertyInfo propertyByName = entity.getPropertyByName(key);
			/**
			 * 2.4、处理每一个字段
			 */
			dealWithKey(info, keyEc, value, propertyByName, sheetName);
		}
	}

	/** 
	 * List<String></br>
	 * 
	 * <p>Title: </p>
	 * <p>
	 *    Description:
	 *    获取单据头必录字段
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-25   下午02:45:03
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @return
	 * @version 1.0
	 */
	private List<String> getBillHeadNeedColumns() {
		List<String> cols = new ArrayList<String>();
		cols.add("FICompany");			//所属组织
		cols.add("bank");				//金融机构
		cols.add("address");			//签约地点
		cols.add("ract");				//合同编号
		cols.add("busType");			//业务类型
		cols.add("budgetProject");      //预算项目
		cols.add("lessor");				//出租人
		cols.add("carPurchase");		//购车款
		cols.add("payPeriod");			//支付期间
		cols.add("payPeriodUnit");		//支付期间单位
		cols.add("financeModel");		//融资模式
		cols.add("settleStatus");		//结清状态
		cols.add("firstRepaymentDate");	//首期还款日期
		cols.add("beginDate");			//租赁开始日期
		cols.add("endDate");			//租赁结束日期
		return cols;
	}
	
	
	/**
	 * 说明:获取车辆信息分录必录字段
	 * @author xx
	 * 2020-5-22	下午07:00:49
	 * @return
	 */
	private List<String> getVehicleEntryNeedColumns() {
		List<String> cols=new ArrayList<String>();
		return cols;
	}
	
	/**
	 * 说明:获取付款计划必录字段
	 * @author xx
	 * 2020-5-22	下午07:01:42
	 * @return
	 */
	private List<String> getPayPlanEntryNeedColumns() {
		List<String> cols=new ArrayList<String>();
		return cols;
	}
	
	
	/**
	 * 说明:处理每一个键值对
	 * @author xx
	 * 2020-5-22	下午07:53:08
	 * @param model 需要添加值的对象
	 * @param key 实体字段名称
	 * @param value 字段对应值
	 * @param propertyByName 属性对象
	 * @param sheetName 页签名
	 * @throws Exception
	 */
	private void dealWithKey(IObjectValue model, String keyEc, String value, PropertyInfo propertyByName, String sheetName) throws Exception {
		String key = keyEc.split("-")[0];
		String keyCn = keyEc.split("-")[1];
		try{
			// 判断对象属性
			if(propertyByName instanceof LinkPropertyInfo && value != null && !"".equals(value)){
				// 处理连接属性数据
				dealWithLinkProperty(propertyByName, model, "number", keyEc, value, sheetName);
			}else if(propertyByName instanceof OwnPropertyInfo){
				// 处理自有属性数据
				OwnPropertyInfo ownInfo = (OwnPropertyInfo) propertyByName;
				if(ownInfo.getDataType().isDateTime() && value != null && !"".equals(value)){
					//日期属性
					try {
						model.put(key, df.parse(value));
					} catch (ParseException e) {
						toException(sheetName + "页签: " + keyCn+" 键中值"+value+"时间格式不正确");
					}
				}else if(ownInfo.getDataType().getName().equalsIgnoreCase(DataType.DECIMAL.getName()) && value != null && !"".equals(value)){
					//BigDecimal属性
					model.put(key, new BigDecimal(value));
				}else if(ownInfo.getDataType().getName().equalsIgnoreCase(DataType.STRING.getName()) && value != null && !"".equals(value)){
					//STRING属性
					model.put(key, value);
				}else if(ownInfo.getDataType().getName().equalsIgnoreCase(DataType.ENUM.getName()) && value != null && !"".equals(value)){
					//枚举ENUM属性
					model.put(key, value);
				}else if(ownInfo.getDataType().getName().equalsIgnoreCase(DataType.BOOLEAN.getName()) && value != null && !"".equals(value)){
					//BOOLEAN属性
					if(!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false")){
						toException(sheetName + "页签: " + keyCn+" 键中值"+value+"值错误,仅能填写'true'或者'false'");
					}
					model.put(key, Boolean.valueOf(value));
				}else if(ownInfo.getDataType().getName().equalsIgnoreCase(DataType.INTEGER.getName()) && value != null && !"".equals(value)){
					//INTEGER属性
					model.put(key, value);
				}
			}
		}catch(Exception exe){
			toException(sheetName + "页签: " + keyCn+" 键中值"+value+"处理出错");
		}
	}

	/**
     * 方法返回类型:String</br>
     * 方法描述:处理连接属性
     * @author DZ_dengbaolan
     * @date 2019-9-27
     * @param propertyByName 属性名对象
     * @param model 需要填值的对象
     * @param field 所要查的字段,即F7传id则传"id",传number则传"number"
     * @param key 实体里对应的名称
     * @param value 传输的值
	 * @param sheetName 页签名
     * @param ctx 上下文
     * @return String 提示信息
	 * @throws Exception 总异常
     */
    private void dealWithLinkProperty(PropertyInfo propertyByName, IObjectValue model, 
    		String field, String keyEc, String value, String sheetName) throws Exception {
    	String key = keyEc.split("-")[0];
    	String keyCn = keyEc.split("-")[1];
		// 将属性对象转换为连接属性对象
    	LinkPropertyInfo linkInfo = (LinkPropertyInfo) propertyByName;
    	if(linkInfo != null){
    		// 根据连接属性对象获取关系再获取所属对象
    		EntityObjectInfo supplierObject = linkInfo.getRelationship().getSupplierObject();
    		if(supplierObject != null){
    			// 获取该连接属性所属对象的bosType
    			BOSObjectType type = supplierObject.getType();
    			if(value != null && !"".equals(value)){
    				// 获取查询语句,可根据传入field动态查询
    				boolean exists = DynamicObjectFactory.getRemoteInstance().exists(type, "where "+ field + " = '" + value + "'");
    				if(exists){
    					// 动态获取该连接属性所属对象的指定查询条件的对应对象
    					IObjectValue objectValue = DynamicObjectFactory.getRemoteInstance().getValue(type, "where "+ field + " = '" + value + "'");
    					// 写值
    					model.put(key, objectValue);
    				}else{
    					toException("EAS中没有" + sheetName + "页签: " + keyCn + " 键F7值为" + value + "的对应对象");
    				}
    			}
    		}
    	}
    }
    
    /** 说明:异常处理方法
	 * @author xx
	 * 2020-5-21	下午02:36:11
	 * @param string 自定义的错误信息
	 * @param exc	Exception对象
	 * @throws BOSException 
     * @throws EASBizException 
	 */
    
	private void toException(String msg) throws EASBizException{
		if("".equals(exceptionTipMsg)){
			exceptionTipMsg = msg;
			throw new EASBizException(new NumericExceptionSubItem("提示信息",msg));
		}else{
			throw new EASBizException(new NumericExceptionSubItem("提示信息",exceptionTipMsg));
		}
	}
	
}
  1. LeaseconbillImportUI类
package com.kingdee.eas.custom.bill.leaseconbill.client.util;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Map;

import org.apache.log4j.Logger;

import com.kingdee.bos.ctrl.swing.KDFileChooser;
import com.kingdee.bos.ctrl.swing.util.SimpleFileFilter;
import com.kingdee.bos.ui.face.CoreUIObject;
import com.kingdee.eas.custom.bill.leaseconbill.client.LeaseConBillListUI;
import com.kingdee.eas.tools.datatask.client.ServerTemplateExport;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;

/**
 * <p>Title: LeaseconbillImportUI</p>
 * <p>
 *    Description:
 *    
 * </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * @author xx
 * @date 2020-5-19   下午01:47:02
 * @version 1.0
 */
public class LeaseconbillImportUI extends AbstractLeaseconbillImportUI
{
	private static final long serialVersionUID = 2429091156900989329L;
    private static final Logger logger = CoreUIObject.getLogger(LeaseconbillImportUI.class);
    
	private int closeType = -1;//不等于-1:确定后关闭的窗口;等于-1:取消后关闭的窗口
	private SimpleFileFilter fileFilter = null;//文件过滤器 默认为:"xls", "xlsx" 
	private File selectedFile = null;//选中的需要导入的文件
	
    /**
     * <p>Title: LeaseconbillImportUI</p>
     * <p>
     *    Description:
     *    构造方法
     * </p>
     * <p>Copyright: Copyright (c) 2020</p>
     * @author xx
     * @date 2020-5-19   下午02:51:08
     * @param param the bare_field_name
     * @throws Exception
     * @version 1.0
     */
    public LeaseconbillImportUI() throws Exception
    {
        super();
        this.fileFilter = new SimpleFileFilter(new String[] { "xls", "xlsx" });
    }

	public LeaseconbillImportUI(SimpleFileFilter fileFilter) throws Exception {
		super();
		this.fileFilter = fileFilter;
	}
    
    /**
     * output storeFields method
     */
    public void storeFields()
    {
        super.storeFields();
    }
	/**
	 * 
	 * <p>Title: onLoad</p>
	 * <p>
	 *    Description:
	 *    页面加载事件
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午02:56:10
	 * @param param the bare_field_name
	 * @throws Exception
	 * @see com.kingdee.bos.ui.face.CoreUIObject#onLoad()
	 * @version 1.0
	 */
	@Override
	public void onLoad() throws Exception {
		super.onLoad();
		this.btnFileChooser.setIcon(EASResource.getIcon("imgTbtn_open"));
		this.btnFileChooser.setText(null);
		Map uiCtx = getUIContext();
		this.selectedFile = ((File) uiCtx.get("selectedFile"));
		if ((this.selectedFile == null) || (!(this.selectedFile.exists())))
			return;
		this.textFilePath.setText(this.selectedFile.getPath());
	}
	
	/** void</br>
	 * 
	 * <p>Title: btnConfirm_actionPerformed</p>
	 * <p>
	 *    Description:
	 *    确定按钮事件  判断选中的文件是否合规或者存在
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午02:57:55
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param e
	 * @throws Exception
	 * @version 1.0
	 */
	protected void btnConfirm_actionPerformed(ActionEvent e) throws Exception {
		String sFile = this.textFilePath.getText();
		this.selectedFile = new File(sFile);
		if ((sFile == null) || (sFile.trim().length() == 0)) {
			MsgBox.showWarning(this, EASResource.getString("com.kingdee.eas.scm.sd.sale.client.GTaxEasResource", "FileNull"));
			return;
		}

		if (!(this.fileFilter.accept(this.selectedFile))) {
			MsgBox.showWarning(this, EASResource.getString("com.kingdee.eas.scm.sd.sale.client.GTaxEasResource", "FileTypeWrong"));
			return;
		}

		if (!(this.selectedFile.exists())) {
			MsgBox.showWarning(this, EASResource.getString("com.kingdee.eas.scm.sd.sale.client.GTaxEasResource", "FileNotExist"));
			return;
		}

		this.closeType = 1;
		disposeUIWindow();
	}
	
	
	/** 
	 * void</br>
	 * 
	 * <p>Title: btnFileChooser_actionPerformed</p>
	 * <p>
	 *    Description:
	 *    选择导入文件
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午02:59:10
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param e
	 * @throws Exception
	 * @version 1.0
	 */

	protected void btnFileChooser_actionPerformed(ActionEvent e) throws Exception {
		KDFileChooser fileChooser = new KDFileChooser();
		fileChooser.setFileSelectionMode(0);
		fileChooser.setFileFilter(fileFilter);
		if ((this.selectedFile != null) && (this.selectedFile.length() > 0L)) {
			fileChooser.setSelectedFile(this.selectedFile);
		}

		if (fileChooser.showOpenDialog(this) == 0) {
			String sFile = fileChooser.getSelectedFile().getPath();
			selectedFile = fileChooser.getSelectedFile();
			this.textFilePath.setText(sFile);
		} else {
			return;
		}
	}
	
	
	/**
	 * <p>Title: btnExpTemplate_actionPerformed</p>
	 * <p>
	 *    Description:
	 *    导出模板
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午03:01:00
	 * @param param the bare_field_name
	 * @param e
	 * @throws Exception
	 * @see com.kingdee.eas.custom.bill.leaseconbill.client.util.AbstractLeaseconbillImportUI#btnExpTemplate_actionPerformed(java.awt.event.ActionEvent)
	 * @version 1.0
	 */
	@Override
	protected void btnExpTemplate_actionPerformed(ActionEvent e) throws Exception {
		String templateName = null;
		Object owner = getUIContext().get("Owner");
		if (owner != null) {
			if (owner instanceof LeaseConBillListUI) {
				templateName = "LeaseConBill.xls";
			}
		}

		if (templateName == null) {
			return;
		}
		ServerTemplateExport te = new ServerTemplateExport("LeaseConBillTemplate/" + templateName);
		te.setParentComp(this);
		try {
			te.export();
		} catch (Exception ex) {
			logger.error(ex);
		}
	}
	/**
	 *  void</br>
	 * 
	 * <p>Title: btnCancel_actionPerformed</p>
	 * <p>
	 *    Description:
	 *    取消
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午03:01:37
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @param e
	 * @throws Exception
	 * @version 1.0
	 */
	protected void btnCancel_actionPerformed(ActionEvent e) throws Exception {
		this.closeType = -1;
		disposeUIWindow();
	}
	
	/** File</br>
	 * 
	 * <p>Title: getSelectedFile</p>
	 * <p>
	 *    Description:
	 *    获取选中的需要导入的文件对象(如果非正常关闭,返回空;否则返回选中的文件对象)
	 * </p>
	 * <p>Copyright: Copyright (c) 2020</p>
	 * @author xx
	 * @date 2020-5-19   下午01:46:06
	 * @param param the bare_field_name
	 * @return the bare_field_name
	 * @return
	 * @version 1.0
	 */
	public File getSelectedFile() {
		if (this.closeType == -1) {
			return null;
		}
		return this.selectedFile;
	}
}

分割线


相关信息

以上就是我关于 EAS中如何实现导入多页签的Excel功能? 知识点整理与总结的全部内容,希望对你有帮助。。。。。。。

上次编辑于:
贡献者: 涎涎
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.4