翻譯|使用教程|編輯:王香|2018-11-22 15:48:58.000|閱讀 322 次
概述:此示例項(xiàng)目顯示了在運(yùn)行時(shí)創(chuàng)建報(bào)表并在查看器中顯示報(bào)表的可能性。您只需要顯示報(bào)表的本機(jī)Java查看器,以及使用組件創(chuàng)建報(bào)表的幾行代碼。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
此示例項(xiàng)目顯示了在運(yùn)行時(shí)創(chuàng)建報(bào)表并在查看器中顯示報(bào)表的可能性。您只需要顯示報(bào)表的本機(jī)Java查看器,以及使用組件創(chuàng)建報(bào)表的幾行代碼。
首先,我們需要?jiǎng)?chuàng)建Java查看器。創(chuàng)建JFrame,設(shè)置必要的選項(xiàng)并添加查看器控件。
public class CreateReport 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 CreateReport(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 CreateReport(final JFrame parentFrame) throws FileNotFoundException {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setPreferredSize(FRAME_SIZE);
    StiViewerFx viewerPanel = new StiViewerFx(parentFrame);
    add(viewerPanel);
...
接下來,創(chuàng)建新的報(bào)表對(duì)象,然后使用Demo XML和XSD文件的路徑創(chuàng)建XML數(shù)據(jù)集對(duì)象,并將其添加到報(bào)表中。
...
    StiReport report = new StiReport();
 
    StiPage page = new StiPage(report);
    report.getPages().add(page);
    page.setName(StiNameCreation.createName(report, StiNameCreation.generateName(page)));
    String xsdPath = "/samples/Demo.xsd";
    StiXmlDatabase xmlDatabase = new StiXmlDatabase("Demo",
        StiResourceUtil.getStream(xsdPath), StiResourceUtil.getStream("/samples/Demo.xml"));
    report.setDictionary(new StiDictionary(report));
    report.getDictionary().getDatabases().add(xmlDatabase);
...
接下來,從Demo數(shù)據(jù)庫中提取信息,并使用Categories名稱創(chuàng)建TableSource 。
...
    StiXmlTableFieldsRequest tables = StiDataColumnsUtil.parceXSDSchema(StiResourceUtil.getStream(xsdPath));
    StiDataTableSource tableSource = null;
    for (StiXmlTable table : tables.getTables()) {
        if (table.getName().equals("Categories")) {
            tableSource = new StiDataTableSource("Demo." + table.getName(), table.getName(), table.getName());
            tableSource.setColumns(new StiDataColumnsCollection());
            for (StiSqlField field : table.getColumns()) {
                StiDataColumn column = new StiDataColumn(field.getName(), field.getName(), field.getSystemType());
                tableSource.getColumns().add(column);
            }
            tableSource.setDictionary(report.getDictionary());
            report.getDictionary().getDataSources().add(tableSource);
        }
    }
...
現(xiàn)在我們需要?jiǎng)?chuàng)建報(bào)表組件。首先,使用Text組件將Header band添加到報(bào)告頁面。這些組件將顯示報(bào)表標(biāo)題。
...
    // 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);
 
    // Create HeaderBand
    StiHeaderBand headerBand = new StiHeaderBand();
    headerBand.setHeight(0.5);
    headerBand.setName("HeaderBand");
    page.getComponents().add(headerBand);
...
接下來,使用“Text”字段和“Image”字段添加“Data”區(qū)域。這些組件將顯示報(bào)表數(shù)據(jù)。
...
    double pos = 0;
    double columnWidth = page.getWidth() / tableSource.getColumns().size();
    Integer nameIndex = 1;
    for (StiDataColumn dataColumn : tableSource.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);
        headerBand.getComponents().add(hText);
 
        if (dataColumn.getName().equals("Picture")) {
            StiImage dataImage = new StiImage(new StiRectangle(pos, 0, columnWidth, 0.5));
            dataImage.setDataColumn("Categories." + dataColumn.getName());
            dataImage.setName("DataImage" + nameIndex.toString());
            dataImage.getBorder().setSide(StiBorderSides.All);
            dataBand.getComponents().add(dataImage);
        } else {
            StiText dataText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5));
            dataText.setText("{Categories." + dataColumn.getName() + "}");
            dataText.setName("DataText" + nameIndex.toString());
            dataText.getBorder().setSide(StiBorderSides.All);
            dataBand.getComponents().add(dataText);
        }
        pos = pos + columnWidth;
        nameIndex++;
    }
...
最后,渲染報(bào)表并在查看器中顯示它。
...
    report.Render();
 
    viewerPanel.getStiViewModel().getEventDispatcher().dispatchStiEvent(
        new StiViewCommonEvent(StiViewCommonEvent.DOCUMENT_FILE_LOADED, new StiDocument(report), null));
}
在下面的屏幕截圖中,您可以看到示例代碼的結(jié)果。

購買Stimulsoft正版授權(quán),請(qǐng)點(diǎn)擊“”喲!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn