客户有个需求是,提供一个excel,excel的某一列或几列有数据,想办法通过已有的数据为条件,查询出其它列数据补充完整。譬如:手头已经有个excel文件,这里面是他需要关注的客户,每次查也都是这批客户,希望直接查出这批客户的其他相关信息。
方案:通过excel模板导入数据到数据库表中,并且只为每个用户保留他自己最后一次导入的数据,然后将报表的参数的值来源于这张库表,最后导出成excel,即为客户需要的excel。
定制点:产品原有的excel导入功能只有全量(把以往导的全删)和增量,这里需要按用户全量,就是A用户导入excel文件时,需要删除A用户以往导入的数据,其他用户的还是要保留。 所以在excel导入数据到表中时,需根据用户名进行导入。
增加数据处理模式:按当前用户全量删除
增加数据处理类,可点击后面按钮自动添加
绑定数据表,必须有一列来存放用户名
3、主要代码
MyRowDataImporter类
package cn.com.smartbi; import java.sql.Connection; import java.sql.Statement; import javax.servlet.http.HttpSession; import org.apache.log4j.Logger; import smartbi.daq.excelimport.DefaultRowDataImporter; import smartbi.daq.excelimport.ImportFileConfig; import smartbi.daq.excelimport.RowData; import smartbi.daq.excelimport.RowDataImporterResult; import smartbi.net.sf.json.JSONArray; import smartbi.net.sf.json.JSONObject; import smartbi.state.IStateModule; import smartbi.state.StateModule; import smartbi.usermanager.User; import smartbi.usermanager.UserManagerModule; import smartbi.util.StringUtil; public class MyRowDataImporter extends DefaultRowDataImporter { private static final Logger LOG = Logger.getLogger(MyRowDataImporter.class); /** * 执行行数据导入逻辑,删除对应用户原有的记录 */ @Override public RowDataImporterResult process(RowData rowData, ImportFileConfig config) throws Exception { if(!isDeleteRows()){ JSONObject jObj = config.getConfig(); User user = (User) UserManagerModule.getInstance().getCurrentUser(); String username = user.getName(); String column = ""; String dataprocessMode = jObj.optString("dataprocessMode"); JSONArray json = jObj.optJSONArray("columnConfigs"); for(int i=0;i<json.length();i++){ JSONObject job = json.getJSONObject(i); if(job.has("javaClassName")&&"smartbi.daq.excelimport.CurrentUserIDCellDataGenerator".equals(job.getString("javaClassName"))){ column = job.getString("fieldName"); //获取用户ID字段 } } //新增的数据处理模式 if (StringUtil.equals("UserFull", dataprocessMode)) { String tableName = jObj.optString("tableName"); String deleteSql = "delete from " + tableName +" where "+column+"='"+username+"'"; // String dsId = jObj.optString("dataSourceId"); Connection conn = config.getConn(); try { Statement stat = conn.createStatement(); stat.execute(deleteSql); setIsDeleteRows("true"); //由于每导入一行数据会执行该方法,删除数据只需要执行一次,故加此状态判断 } catch (Exception e) { LOG.error(e.getMessage(), e); } } } return super.process(rowData, config); } //用户全量excel导入时,在处理每行数据时,判断是否已经处理过原有数据 public boolean isDeleteRows(){ IStateModule stateModule = StateModule.getInstance(); HttpSession session = stateModule.getSession(); String isDeleteRows = (String) session.getAttribute("isDeleteRows"); if("true".equals(isDeleteRows)){ return true; } return false; } public void setIsDeleteRows(String attr){ IStateModule stateModule = StateModule.getInstance(); HttpSession session = stateModule.getSession(); session.setAttribute("isDeleteRows", attr); } } |