发布时间 : 2021-08-27 09:44:18.413|阅读 416 次
概述:此示例显示如何将报告从 Java 打印到指定的打印机 (Swing)。
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最新版
此示例显示如何将报告从 Java 打印到指定的打印机 (Swing)。
首先,创建JFrame并设置必要的选项:
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);
}
}
});
}
接下来,在主面板上添加打印按钮。作为操作,我们将打印报告定义为默认打印机:
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);
}
}
});
接下来,添加用于选择打印机的元素和用于将报告打印到所选打印机的按钮:
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);
}
}
});
我们还需要加载报告进行打印。我们使用“SimpleList”报告 - 加载它并将演示数据库添加到报告对象。在这些操作呈现报告之后:
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;
}
在下面的屏幕截图中,您可以看到示例代码的结果:
Aspose、E-iceblue、FastReport、Stimulsoft等文档/报表图表类开发工具享超低折扣,如有需要可直接联系在线客服。
渝ICP备12000582号-15/渝公网安备 50010702501010号