翻譯|使用教程|編輯:李顯亮|2021-08-27 09:44:18.413|閱讀 403 次
概述:此示例顯示如何將報(bào)告從 Java 打印到指定的打印機(jī) (Swing)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Stimulsoft Ultimate是用于創(chuàng)建報(bào)表和儀表板的通用工具集。該產(chǎn)品包括用于WinForms、ASP.NET、.NET Core、JavaScript、WPF、PHP、Java和其他環(huán)境的完整工具集。
Stimulsoft Reports不僅擁有強(qiáng)大的報(bào)表導(dǎo)出系統(tǒng),而且還支持多種報(bào)表導(dǎo)出格式,擁有簡(jiǎn)單且強(qiáng)大的報(bào)表引擎。Stimulsoft Reports基本原則是,用簡(jiǎn)單常規(guī)的方法創(chuàng)建報(bào)表,將不同的技術(shù)應(yīng)用于應(yīng)用程序。Stimulsoft Reports.Java是一個(gè)專為在Java應(yīng)用程序中的報(bào)表進(jìn)行交互和處理的報(bào)表工具。
點(diǎn)擊下載Stimulsoft Reports.Java v2021.3.1最新版
此示例顯示如何將報(bào)告從 Java 打印到指定的打印機(jī) (Swing)。
首先,創(chuàng)建JFrame并設(shè)置必要的選項(xiàng):
public static void main(final String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { try { JFrame frame = new JFrame(); frame.add(new Printing(frame)); frame.setSize(FRAME_SIZE); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } catch (Throwable e) { StiExceptionProvider.show(e, null); } } }); }
接下來(lái),在主面板上添加打印按鈕。作為操作,我們將打印報(bào)告定義為默認(rèn)打印機(jī):
JButton button = new JButton("Print"); leftPanel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StiReport report = getReport(); PrinterJob printerJob = StiPrintHelper.preparePrinterJob(report.getRenderedPages()); try { StiPrintHelper.printJob(printerJob, report, true); } catch (PrinterException pe) { StiExceptionProvider.show(pe, null); } } });
接下來(lái),添加用于選擇打印機(jī)的元素和用于將報(bào)告打印到所選打印機(jī)的按鈕:
final JComboBox printerList = new JComboBox(PrintServiceLookup.lookupPrintServices(null, null)); rightPanel.add(printerList); printerList.setSelectedIndex(0); button = new JButton("Print"); rightPanel.add(button); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StiReport report = getReport(); PrinterJob printerJob = StiPrintHelper.preparePrinterJob(report.getRenderedPages()); try { AttributeSet attr_set = new HashAttributeSet(); PrintService printService = (PrintService) printerList.getSelectedItem(); attr_set.add(new PrinterName(printService.getName(), null)); PrintService[] services = PrintServiceLookup.lookupPrintServices(null, attr_set); printerJob.setPrintService(services[0]); StiPrintHelper.printJob(printerJob, report); } catch (Exception e1) { StiExceptionProvider.show(e1, parentFrame); } } });
我們還需要加載報(bào)告進(jìn)行打印。我們使用“SimpleList”報(bào)告 - 加載它并將演示數(shù)據(jù)庫(kù)添加到報(bào)告對(duì)象。在這些操作呈現(xiàn)報(bào)告之后:
private StiReport getReport() { if (report == null) { try { String demoDir = "Data/"; StiXmlDatabase xmlDatabase = new StiXmlDatabase("Demo", demoDir + "Demo.xsd", demoDir + "Demo.xml"); StiReport renderReport = StiSerializeManager.deserializeReport(new File("Reports/SimpleList.mrt")); renderReport.getDictionary().getDatabases().add(xmlDatabase); renderReport.setCalculationMode(StiCalculationMode.Interpretation); renderReport.Render(false); report = renderReport; } catch (Exception e) { StiExceptionProvider.show(e, null); } } return report; }
在下面的屏幕截圖中,您可以看到示例代碼的結(jié)果:
Aspose、E-iceblue、FastReport、Stimulsoft等文檔/報(bào)表圖表類開發(fā)工具享超低折扣,如有需要可直接。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn