翻譯|使用教程|編輯:李顯亮|2021-08-23 10:18:31.157|閱讀 248 次
概述:此示例項目展示了如何使用 MySQL 字段創建新報告并提取 MySQL 數據庫信息。
# 界面/圖表報表/文檔/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最新版
此示例項目展示了如何使用 MySQL 字段創建新報告并提取 MySQL 數據庫信息。
首先,您需要創建一個新報告并添加一個 MySqlDatabase。在StiMySqlDatabase類的構造函數中,您應該設置數據庫名稱、別名和連接字符串:
public static StiReport createReport() throws ClassNotFoundException, SQLException, StiException, FileNotFoundException { StiReport report = StiReport.newInstance(); StiPage page = report.getPages().get(0); report.setDictionary(new StiDictionary(report)); StiMySqlDatabase db = new StiMySqlDatabase( "test", "test", "url=jdbc:mysql://localhost:3306/sakila;user=root;password=terra2;database=sakila"); report.getDictionary().getDatabases().add(db); ...
接下來,我們需要創建一個數據源。創建StiMySqlSource對象,提取“演員”數據表字段并填充此數據源:
... StiMySqlSource source = new StiMySqlSource("test.actors", "actors", "actors", "select * from actor"); source.setDictionary(report.getDictionary()); report.getDictionary().getDataSources().add(source); source.setColumns(new StiDataColumnsCollection()); StiMySqlAdapter adapter = new StiMySqlAdapter(db.getConnectionString()); Class.forName(adapter.getDriverName()); Connection con = com.stimulsoft.webdesigner.helper.StiDictionaryHelper.getConnection(adapter.getJdbcParameters()); StiTableFieldsRequest request = StiDataColumnsUtil.getFields(con, source.getQuery(), source); for (StiSqlField field : request.getColunns()) { source.getColumns().add(new StiDataColumn(field.getName(), field.getName(), field.getSystemType())); } ...
數據源已創建,現在我們需要在報表頁面上顯示數據。StiDataBand向報告頁面和StiText所有數據列的組件添加一個新組件。還要為標題添加標題:
... StiDataBand dataBand = new StiDataBand(); dataBand.setDataSourceName("actors"); dataBand.setHeight(0.5); dataBand.setName("DataBand"); page.getComponents().add(dataBand); double pos = 0; double columnWidth = page.getWidth() / source.getColumns().size(); Integer nameIndex = 1; for (StiDataColumn dataColumn : source.getColumns()) { // Create text on header StiText hText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5)); hText.setTextInternal(dataColumn.getName()); hText.setHorAlignment(StiTextHorAlignment.Center); hText.setName("HeaderText" + nameIndex.toString()); hText.setBrush(new StiSolidBrush(StiColorEnum.Orange.color())); hText.getBorder().setSide(StiBorderSides.All); StiText dataText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5)); dataText.setText("{actors." + dataColumn.getName() + "}"); dataText.setName("DataText" + nameIndex.toString()); dataText.getBorder().setSide(StiBorderSides.All); dataBand.getComponents().add(dataText); pos = pos + columnWidth; nameIndex++; } ...
最后,使用Render()report對象的方法渲染創建的報表,并返回結果:
... report.Render(); return report; }
為了顯示報告,我們使用本機 Java 查看器。我們需要創建查看器對象,添加必要的事件偵聽器并分配報告:
public static void showReport(StiReport report) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setPreferredSize(new Dimension(1000, 1000)); StiViewerFx viewerPanel = new StiViewerFx(frame); panel.add(viewerPanel); frame.add(panel); frame.setSize(new Dimension(1000, 1000)); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); viewerPanel.getStiViewModel().getEventDispatcher() .dispatchStiEvent(new StiViewCommonEvent(StiViewCommonEvent.DOCUMENT_FILE_LOADED, new StiDocument(report), null)); } public static void main(String[] args) throws ClassNotFoundException, SQLException, StiException, FileNotFoundException { StiReport report = createReport(); showReport(report); }
在下面的屏幕截圖中,您可以看到示例代碼的結果:
Aspose、E-iceblue、FastReport、Stimulsoft等文檔/報表圖表類開發工具享超低折扣,如有需要可直接。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn