1. 程式人生 > >【Stimulsoft Reports Java教程】在執行時使用MySQL資料庫建立報表

【Stimulsoft Reports Java教程】在執行時使用MySQL資料庫建立報表

下載Stimulsoft Reports Java最新版本

此示例專案顯示如何使用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"%t.setName("DataText" + nameIndex.toString());
        dataText.getBorder().setSide(StiBorderSides.All);
        dataBand.getComponents().add(dataText);
        pos = pos + columnWidth;
        nameIndex++;
    }
...

最後,使用報表物件的Render()方法呈現建立的報表,並返回結果。

...
    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);
}

示例程式碼的結果如下圖所示:

Stimulsoft

檢視原文,下載示例