-1

这是我打印发票页的源线。我的报告在Java包中。我将它保存在一个名为“report”的文件夹中。

    try {

        String date1 = new SimpleDateFormat("yyyy-MM-dd").format(isdate.getDate());
        String time1 = istime.getValue().toString().split(" ")[3];
        date1 = date1 + " " + time1;

        String date2 = new SimpleDateFormat("yyyy-MM-dd").format(redate.getDate());
        String time2 = retime.getValue().toString().split(" ")[3];
        date2 = date2 + " " + time2;

        JRTableModelDataSource dataSource = new JRTableModelDataSource(jTable1.getModel());
        String reportsource = " D:/Catering/report/report1.jrxml";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("inid", txtInvoiceID.getText());
        params.put("cuname", txtCuName.getText());
        params.put("cuadd", txtCuid.getText());
        params.put("cutp", txtTPNo.getText());
        params.put("isdate", date1);
        params.put("redate", date2);
        params.put("advance", txtAdvance.getText());
        params.put("due", txtDue.getText());
        params.put("total", txtGtotal.getText());
        JasperReport jasperReport = JasperCompileManager.compileReport(reportsource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
        JasperViewer.viewReport(jasperPrint, true);
        JOptionPane.showMessageDialog(null, "Done");
    } catch (Exception e) {
       JOptionPane.showMessageDialog(null, "jasper error"+e);
    }
4

1 回答 1

1

最好将报告作为嵌入式资源,但在可能的情况下,您不应依赖绝对路径,因为这些路径可能会在安装之间发生变化......

尝试改变

String reportsource = " D:/Catering/report/report1.jrxml";
                       ^---- Did you notice the white space here?

String reportsource = "report/report1.jrxml";

此外,预编译报告通常会更好,这样您就不需要在运行时进行...

您可以编写一个简单的程序,使用类似的东西为您编译它...

String templateFile = "report/report1.jrxml"
String compiledReport = "report/report1.jasper"
JasperCompileManager.compileReportToFile(templateFile, compiledReport);

该方法需要两个Strings,基本上一个是jrxml文件,另一个是预期的jasper文件。

您可以使用JasperReports 的 Ant 任务并将您的构建过程分开。

或者你可以使用 iReports...

编译后,您可以简单地jasper在运行时加载文件...

String compiledReport = "report/report1.jasper";
JasperReport report = (JasperReport)JRLoader.loadObjectFromFile(compiledReport );
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

使用快速编译示例更新

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

public class QuickCompile {

    public static void main(String[] args) {
        try {
            String template = "...";
            String compiled = "...";
            JasperCompileManager.compileReportToFile(template, compiled);

            // Just as a test...
            JasperReport jr = (JasperReport) JRLoader.loadObjectFromFile(compiled);            

        } catch (JRException exp) {
            exp.printStackTrace();
        }            
    }

}
于 2013-10-03T03:03:20.527 回答