翻譯|使用教程|編輯:李顯亮|2021-08-24 10:05:26.647|閱讀 232 次
概述:此示例項目展示了在運行時使用關系和自定義對象數據庫創建報告的可能性。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Stimulsoft Ultimate是用于創建報表和儀表板的通用工具集。該產品包括用于WinForms、ASP.NET、.NET Core、JavaScript、WPF、PHP、Java和其他環境的完整工具集。
Stimulsoft Reports不僅擁有強大的報表導出系統,而且還支持多種報表導出格式,擁有簡單且強大的報表引擎。Stimulsoft Reports基本原則是,用簡單常規的方法創建報表,將不同的技術應用于應用程序。Stimulsoft Reports.Java是一個專為在Java應用程序中的報表進行交互和處理的報表工具。
點擊下載Stimulsoft Reports.Java v2021.3.1最新版
此示例項目展示了在運行時使用關系和自定義對象數據庫創建報告的可能性。
首先,創建一個將存儲數據的對象類:
public class ObjectCell { public String val; @Override public boolean equals(Object obj) { return val.equals(((ObjectCell) obj).val); } public String toString() { return val; } public ObjectCell(String val) { this.val = val; } }
接下來,創建從 StiDatabase 類擴展的父數據庫并在連接事件上填充數據:
public class ParentDatabase extends StiDatabase { public ParentDatabase() { super("Demo.Parent"); // Database name } public void connect(StiDataStoreSource stiDataStoreSource, Boolean fillTable) throws StiException { DataTable dataTable = stiDataStoreSource.createNewTable(); for (int i = 0; i < 5; i++) { DataRow dataRow = dataTable.createNewRow(); dataRow.addCell("cId", new ObjectCell("Object" + i)); dataRow.addCell("cName", "Parent cId: " + i); } stiDataStoreSource.setDataTable(dataTable); } public void disconnect() { } public void connect(StiDataStoreSource stiDataStoreSource) throws StiException { connect(stiDataStoreSource, true); } }
接下來,創建相同的子數據庫:
public class ChildDatabase extends StiDatabase { public ChildDatabase() { super("Demo.Child"); // Database name } public void connect(StiDataStoreSource stiDataStoreSource, Boolean fillTable) throws StiException { DataTable dataTable = stiDataStoreSource.createNewTable(); for (int i = 0; i < 5; i++) { for (int k = 0; k < 5; k++) { DataRow dataRow = dataTable.createNewRow(); dataRow.addCell("cId", new ObjectCell("Object" + i)); for (int j = 1; j < dataRow.getColumns().size(); j++) { // fill row wiht my data dataRow.addCell(dataRow.getColumns().get(j).getColumnName(), "Child cId: " + i + " value: " + j); } } } stiDataStoreSource.setDataTable(dataTable); } public void disconnect() { } public void connect(StiDataStoreSource stiDataStoreSource) throws StiException { connect(stiDataStoreSource, true); } }
要顯示報告,我們需要創建 Java 查看器。創建 JFrame,設置必要的選項并添加查看器控件:
public class CreateRelationsReport extends JPanel { private static final long serialVersionUID = 330448692680237867L; private static final Dimension FRAME_SIZE = new Dimension(800, 800); public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { JFrame frame = new JFrame(); frame.add(new CreateRelationsReport(frame)); frame.setSize(FRAME_SIZE); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } catch (Throwable e) { StiExceptionProvider.show(e, null); } } }); } public CreateRelationsReport(final JFrame parentFrame) throws IOException { setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); setPreferredSize(FRAME_SIZE); StiViewerFx viewerPanel = new StiViewerFx(parentFrame); add(viewerPanel); ...
接下來,創建新的報表對象,然后向其中添加父數據庫和子數據庫:
... StiReport report = new StiReport(); StiPage page = new StiPage(report); report.getPages().add(page); page.setName(StiNameCreation.createName(report, StiNameCreation.generateName(page))); report.setDictionary(new StiDictionary(report)); report.getDictionary().getDatabases().add(new ParentDatabase()); report.getDictionary().getDatabases().add(new ChildDatabase()); ...
接下來,在對象表源中設置自定義列:
... ListtableSources = new ArrayList(); // parent StiDataTableSource tSource = new StiDataTableSource("Demo.Parent", "Parent", "Parent"); tSource.setColumns(new StiDataColumnsCollection()); tSource.getColumns().add( new StiDataColumn("cId", "cId", StiSystemType.getSystemType("System.Object"))); tSource.getColumns().add( new StiDataColumn("cName", "cName", StiSystemType.getSystemType("System.String"))); tSource.setDictionary(report.getDictionary()); report.getDictionary().getDataSources().add(tSource); tableSources.add(tSource); // child tSource = new StiDataTableSource("Demo.Child", "Child", "Child"); tSource.setColumns(new StiDataColumnsCollection()); tSource.getColumns().add( new StiDataColumn("cId", "cId", StiSystemType.getSystemType("System.Object"))); for (int i = 0; i < 4; i++) { tSource.getColumns().add( new StiDataColumn("cData" + i, "cData" + i, StiSystemType.getSystemType("System.String"))); } tSource.setDictionary(report.getDictionary()); report.getDictionary().getDataSources().add(tSource); tableSources.add(tSource); ...
將帶有文本框的標題欄和數據欄添加到報告中:
... // Create TitleBand StiHeaderBand titleBand = new StiHeaderBand(); titleBand.setHeight(0.85); titleBand.setName("TitleBand"); page.getComponents().add(titleBand); // Create Title text on header StiText headerText = new StiText(new StiRectangle(0, 0, page.getWidth(), 0.85)); headerText.setTextInternal("Tacticdescription"); headerText.setHorAlignment(StiTextHorAlignment.Left); headerText.setName("TitleHeader"); headerText.setFont(new StiFont("Arial", 12F, StiFontStyle.Bold)); titleBand.getComponents().add(headerText); Integer nameIndex = 1; ListdataBands = new ArrayList(); for (StiDataTableSource tableSource : tableSources) { // Create Databand StiDataBand dataBand = new StiDataBand(); dataBand.setDataSourceName(tableSource.getName()); dataBand.setHeight(0.5); dataBand.setName("DataBand" + nameIndex); nameIndex++; page.getComponents().add(dataBand); double pos = 0; double columnWidth = page.getWidth() / tableSource.getColumns().size(); for (StiDataColumn dataColumn : tableSource.getColumns()) { StiText dataText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5)); dataText.setText("{" + tableSource.getName() + "." + dataColumn.getName() + "}"); dataText.setName("DataText" + nameIndex.toString()); dataText.getBorder().setSide(StiBorderSides.All); dataBand.getComponents().add(dataText); pos = pos + columnWidth; nameIndex++; } dataBands.add(dataBand); } ...
報告的所有組件都已創建,我們現在可以配置數據關系:
... dataBands.get(1).setDataRelationName("Relation"); dataBands.get(1).setMasterComponent(dataBands.get(0)); ArrayListcols = new ArrayList(); cols.add("cId"); StiDataRelation rel = new StiDataRelation("Relation", tableSources.get(0), tableSources.get(1), cols, cols); report.getDictionary().getRelations().add(rel); ...
最后,呈現報告并將其顯示在 Java 查看器中:
... report.Render(); viewerPanel.getStiViewModel().getEventDispatcher().dispatchStiEvent( new StiViewCommonEvent(StiViewCommonEvent.DOCUMENT_FILE_LOADED, new StiDocument(report), null)); }
在下面的屏幕截圖中,您可以看到示例代碼的結果:
Aspose、E-iceblue、FastReport、Stimulsoft等文檔/報表圖表類開發工具享超低折扣,如有需要可直接。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn