使用jsp页面显示水晶报表还是存在这样的问题:
1. 配置麻烦。
水晶报表自身的网页版crytalviewer显示内容存在失真,和实际的不一致。
水晶报表的XML文件配置不正确导致无法加载报表文件,难以排除问题。
以下方法可以更加简单的在Spring boot 项目中使用Crytal reports.
在src\main\resources下创建lib文件夹,并在通过build path->Add Extern Achives 将水晶报表的依赖库加入到项目中。
使用CR4E (Crytal Reports for Eclipse) 提供的CRJavaHelper 灵活操作报表,例如直接用PDF文件预览,以及导出PDF,Word等操作。
使用@RestContorller 创建控制器,设置RequestMapper, 使用绝对路径加载报表即可
@RestController
public class ReportPOController {
@GetMapping("/Report/PO/showPdf")
public void showPdf()
throws ReportSDKExceptionBase, IOException {
ReportClientDocument reportClientDocument = new ReportClientDocument();
reportClientDocument.open("reports/ReportPO.rpt", 0);
// This will be used by the viewer to display the desired report. True to
// Download this report.
CRJavaHelper.exportPDF(reportClientDocument, response, false);
}
@GetMapping("/Report/PO/exportPdf")
public void exportPdf()throws ReportSDKExceptionBase, IOException {
ReportClientDocument reportClientDocument = new ReportClientDocument();
reportClientDocument.open("reports/ReportPO.rpt", 0);
// This will be used by the viewer to display the desired report. True to
// Download this report.
CRJavaHelper.exportPDF(reportClientDocument, response, true);
}
}
注意:我的项目中, 我在src/main/resource下创建了一个reports文件夹,所以上面的报表绝对路径为reports/ReportPO.rpt,
无需tld标签定义文件, 无需xml配置文件,也不需要加载jsp支持。