帆软报表填报前期步骤按api或者百度就可以完成,今天我这里着重讲解自定义提交(访问web后台服务进行提交)。
填报提交有两种方式:
1)内置SQL:没什么难度,按教程来就行。
2)自定义提交:对应后台服务进行数据处理保存。
自定义提交大致分为以下几个步骤:
1)编写自定义帆软接收类(上图的Fill类)
自定义接收类我这里以ReportFillDataHandler类为例,继承TotalSubmitJob(可以一次处理多行数据),话不多说直接上代码
public class ReportFillDataHandler extends TotalSubmitJob{
private static final String SUFFIN_IN = "-NN";
protected JobValue hiddenLineNum;// 指定不保存入库的行号所存放的单元格,单元格中行号以逗号分隔,如:5,13
protected JobValue sortTable; //指定表执行的先后顺序,格式:类名-顺序编号,从小到大排序,以逗号分隔
protected boolean removeRepeat; //是否去点重复数据
protected List<Integer> delIndex = new ArrayList<>(); //纪录删除行索引
protected List<String> flagNameList; //保存定义成“单元格”或“常量”变量名,不再读取数据
protected HashMap<String, String> hideColMap = new HashMap<String, String>();
protected boolean removeNotNull; //对于propertyCode-NN形式的非空字段,如果值为空则忽略掉
private static ApplicationContext ac;
static {
ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();
ac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
}
@Override
protected void doTotalJob(Data data, Calculator calculator) throws Exception {
//初始化参数
initPara();
//初始化flagNameList
flagNameList = new ArrayList<String>();
flagNameList.add("hiddenLineNum");
flagNameList.add("SortTable");
flagNameList.add("removeRepeat");
flagNameList.add("removeNotNull");
//获取隐藏行号
if (hiddenLineNum != null && !"".equals(hiddenLineNum.getValue())) {
for (String hstr: hiddenLineNum.getValue().toString().split(",")) {
hideColMap.put(hstr,hstr);
}
}
//预处理
preProcess(data);
//读取数据集
Map<String,List<Map<String,String>>> sheets = new HashMap<>();//报表数据集,格式:<表名,行列名[<字段名,字段值>]>
try {
readDataSet(sheets,data);
} catch (Exception ex) {
ex.printStackTrace();
throw new WriteSubmitException("保存失败:" + ex.getMessage());
}
//表排序,用sortTable中带有序号的表名替换原表名
if (sortTable != null && !"".equals(sortTable.getValue())) {
String[] tableSorts = sortTable.getValue().toString().split(",");
String[] tbAndNum = null;
for (String tableSort: tableSorts) {
if (tableSort == null && "".equals(tableSort)) {
continue;
}
tbAndNum = tableSort.split("-");
if (tbAndNum.length < 2) {
continue;
}
if (sheets.get(tbAndNum[0]) != null) {
sheets.put(tableSort,sheets.get(tbAndNum[0]));
sheets.remove(tbAndNum[0]);//去除原数据
}
}
}
//去重
if (removeRepeat) {
sheets = getNewSheetMap(sheets);
}
//去除非空字段为null的行
if (removeNotNull) {
sheets = removeNullField(sheets);
}
//其他处理
sheets = afterProcess(sheets);
System.out.println("最终数据:"+sheets.toString());
String result = JSON.toJSONString(sheets);
System.out.println("最终数据jsonResult:"+result);
//调用入库服务接口---开始后台数据保存
ReportFillUtils reportFillUtils = (ReportFillUtils) ac.getBean("reportcenter.utils.reportFillUtils");
reportFillUtils.reportDataCommit(sheets);
}
/**
* 初始化公共参数,由于帆软不能实例化父类中的JobValue,故提供子类给父类赋值的变通方式
*/
protected void initPara() {}
/**
* 解析获取数据集前进行处理,供子类覆盖实现过程
*/
private void preProcess(Data data) {}
/** 从AbstractTotalJob.Data读取数据到Map中,这是默认实现,如有特殊需求在子类覆盖即可
* @param sheets
* @param data
*/
private void readDataSe