1、需求背景
客户有个需求是,通过excel的某几列数据,补充完其它列数据。
我们给出的方案是:通过excel模板导入数据到数据库中,然后创建报表通过sql查询出来所需要的数据,最后导出成excel,即为客户需要的excel。
定制点:这里涉及到多个用户都要查询该报表时,需要查询各自的信息。产品原有的excel导入只有全量很增量,无法区分数据是谁导入的。
所以在excel导入数据到表中时,需根据用户名进行导入。在制作报表时,通过sql过滤即可获取当前用户导入的数据。
2、实现方案
增加数据处理模式

增加数据处理类,可点击后面按钮自动添加

绑定数据表,必须有一列来存放用户名

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);
}
}
Excelimport.rar
excelimport.ext