历史版本2 :通过代码创建模板 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

目录:

1. 描述编辑

有时候需要批量生成cpt模板,手动操作比较繁琐;

FineReport开放了大量的API接口供应用开发人员进行深入的开发与控制,借此可以通过java代码批量生成cpt模板。

2. 示例编辑

2.1完整可执行代码

打开编译器,例如:eclipse。新建class,复制如下代码。运行之后可在E盘下找到生成的3.cpt文件。

package com.winnning; import com.fr.base.TableData; import com.fr.data.impl.DBTableData; import com.fr.data.impl.NameDatabaseConnection; import com.fr.general.ModuleContext; import com.fr.general.data.TableDataColumn; import com.fr.report.cell.DefaultTemplateCellElement; import com.fr.report.cell.TemplateCellElement; import com.fr.report.cell.cellattr.core.group.DSColumn; import com.fr.report.module.EngineModule; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import com.fr.main.impl.WorkBook; import com.fr.report.report.TemplateReport; import com.fr.report.worksheet.WorkSheet; public class GenericTemplate { public static void main(String[] args) throws Exception { ModuleContext.startModule(EngineModule.class.getName()); WorkBook wb = new WorkBook(); //添加新的模板数据集 TableData td = genericTableData("dbLink", "exec usp_admin_debug @id=${id}"); wb.putTableData("科室信息2", td); //创建第一个report 也就是sheet页 WorkSheet report = new WorkSheet(); wb.addReport(report); //数据库暂时先不读,弄点假数据 HashMap<Integer, String> map = new HashMap<>(); map.put(0, "id"); map.put(1, "name"); //添加列头 DefaultTemplateCellElement title = genericCell(0, 0, null, "这是标题"); title.setRowSpan(2); title.setColumnSpan(2); report.addCellElement(title); //添加数据列 for (Integer key : map.keySet()) { TemplateCellElement cellHeaer = genericCell(2, key, null, map.get(key)); report.addCellElement(cellHeaer); TemplateCellElement cell = genericCell(3, key, "科室信息2", map.get(key)); report.addCellElement(cell); } //导出模板 FileOutputStream outputStream = new FileOutputStream(new File("E:\\Temp\\3.cpt")); wb.export(outputStream); outputStream.close(); ModuleContext.stopModules(); System.out.println("finished"); } /* *生成TableData */ private static TableData genericTableData(String conString, String sqlQuery) { NameDatabaseConnection database = new NameDatabaseConnection(conString); TableData td = new DBTableData(database, sqlQuery); // NameDatabaseConnection database = new NameDatabaseConnection("winningreport"); // TableData td = new DBTableData(database, "exec usp_admin_debug @id=${id}"); return td; } /** * 添加单元格 * row 行号 * column 列号 * dsName 数据集名称 如 ds1 * 数据列名称如 ds1中的id列,则输入 id */ private static DefaultTemplateCellElement genericCell(int row, int column, String dsName, String columnName) { DefaultTemplateCellElement dtCell = new DefaultTemplateCellElement(row, column); if (dsName != null) { DSColumn dsColumn = new DSColumn(); dsColumn.setDSName(dsName); dsColumn.setColumn(TableDataColumn.createColumn(columnName)); dtCell.setValue(dsColumn); } else { dtCell.setValue(columnName); } return dtCell; } } //自动执行存储过程,从里面获取结果集结的部分等研究一下jdbc再补