CrystalReports > 安装配置和简单应用

水晶报表Crystal Reports的简单应用

一.安装

官网下载试用安装工具Crystal Reports,注册获得试用期30天。

二.配置数据源

https://www.cnblogs.com/hongten/p/3787236.html

三.优缺点

使用Crystal Reports尝试配置模板的体验,主要与Jasper Studio对比。

3.1 优点

1.流式布局,交互类似office

2.官方demo丰富

3.2 缺点

1.通过URL加载的图片不支持https,只接受http

https://stackoverflow.com/questions/23331095/does-crystal-reports-support-secure-https-images

2.相关教程博客很少

3.集成至开发环境对C#友好,JAVA对eclipse友好,无idea开发控件,官方demo通过jsp的形式查看报表

四.集成到项目

4.1 SDK

由于官方没有提供idea插件,需要额外引入依赖项文件,下载地址如下,在该页面中下载 Runtime Libraries:

https://archive.sap.com/documents/docs/DOC-29757

值得注意的是,直接从Crystal Reports工具的lib中也可以找到看上去相似的sdk文件,接口完全相同但是实现完全不同,不可引入项目使用。

maven资源库无所需依赖项,需要在项目配置中单独添加lib文件。

官方API文档:

https://help.sap.com/doc/javadocs_bip_41/4.1/en-US/ras/en/overview-summary.html

4.2 实战

官方提供了大量demo程序:

https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports+Java++SDK+Samples

下面结合OEM MES的需求总结一些常用API范例:

4.2.1导出PDF到浏览器

配置模板如下:

导出PDF2

输出PDF样例代码如下:

// 读取模板文件
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open("./reports/jrc_export_report.rpt", 0);
ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) reportClientDoc.
        getPrintOutputController().export(ReportExportFormat.PDF);
reportClientDoc.close();
// 输出PDF
byte[] buffer = new byte[byteArrayInputStream.available()];
int bytesRead = 0;
response.reset();
response.setHeader("Content-disposition", "inline;filename=report.pdf");
response.setContentType("application/pdf");
while ((bytesRead = byteArrayInputStream.read(buffer)) != -1) {
    response.getOutputStream().write(buffer, 0, bytesRead);
}
response.getOutputStream().flush();
response.getOutputStream().close();

PDF效果图如下所示:

导出PDF

4.2.2数据库连接

在第一个样例中,数据库连接配置在模板中,不需要由代码配置,这也是默认的连接方式。

如需要更改数据库连接,参考官方实例change_db_location(CRJ12_Sample_Codes\RCAPI),大致如下:

String REPORT_NAME = "change_db_location/simplereport.rpt";
String DBUSERNAME = "vantech";
String DBPASSWORD = "vantech";

String CONNECTION_STRING = "!com.microsoft.jdbc.sqlserver.SQLServerDriver!jdbc:microsoft:sqlserver://10.50.212.103:1433;DatabaseName={database};user={userid};password={password}";
String TRUSTEDCON = "false";
String PREQESERVERNAME = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
String SERVERTYPE = "JDBC (JNDI)";
String DATABASE_DLL = "crdb_jdbc.dll";
String DATABASE = "Xtreme";
String DBCLASSNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String USEJDBC = "true";
String DATABASE_NAME = "Xtreme";
String SERVERNAME = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
String CONNECTIONURL = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
String SERVER = "10.50.212.103";

//Open report.
ReportClientDocument boReportClientDocument = new ReportClientDocument();
boReportClientDocument.open(REPORT_NAME, 0);

// Set DB Username and Password
boReportClientDocument.getDatabaseController().logon(DBUSERNAME, DBPASSWORD);

// Create the two connectioninfo objects to use
IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();

// Assign the old Connection info to the reports current info
DatabaseController dbController = boReportClientDocument.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);

// If this connection needed parameters, we would use this field.
Fields pFields = null;

// Create a new propertybag for the new location
PropertyBag boPropertyBag1 = new PropertyBag();

// Set new table logon properties
boPropertyBag1.put("JDBC Connection String", CONNECTION_STRING);
boPropertyBag1.put("Trusted_Connection", TRUSTEDCON);
boPropertyBag1.put("PreQEServerName", PREQESERVERNAME);
boPropertyBag1.put("Server Type", SERVERTYPE);
boPropertyBag1.put("Database DLL", DATABASE_DLL);
boPropertyBag1.put("Database", DATABASE);
boPropertyBag1.put("Database Class Name", DBCLASSNAME);
boPropertyBag1.put("Use JDBC", USEJDBC);
boPropertyBag1.put("Database Name", DATABASE_NAME);
boPropertyBag1.put("Server Name", SERVERNAME);
boPropertyBag1.put("Connection URL", CONNECTIONURL);
boPropertyBag1.put("Server", SERVER);

// Assign the properties to the connection info
newConnectionInfo.setAttributes(boPropertyBag1);

// Set the DB Username and Pwd
newConnectionInfo.setUserName(DBUSERNAME);
newConnectionInfo.setPassword(DBPASSWORD);

// The Kind of connectionInfos is SQL
newConnectionInfo.setKind(ConnectionInfoKind.SQL);

// set the parameters to replace.
// The 4 options are:
// _doNotVerifyDB
// _ignoreCurrentTableQualifiers
// _mapFieldByRowsetPosition
// _useDefault
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;

// Now replace the connections
dbController.replaceConnection(oldConnectionInfo, newConnectionInfo, pFields, replaceParams);
4.2.3动态注入参数

参考样例:Java_CRJ12_Web_Set_Parameters(CRJ12_Sample_Codes\ViewingExport)

模板配置如下:

注入参数模板配置

数据填充示例如下:

ReportClientDocument document = new ReportClientDocument();
document.setReportAppServer(ReportClientDocument.inprocConnectionString);
document.open("./reports/Java_CRJ12_Web_Set_Parameters.rpt", OpenReportOptions._openAsReadOnly);
ParameterFieldController parameterFieldController = document.getDataDefController()
        .getParameterFieldController();
parameterFieldController.setCurrentValue("", "StringParam", "Hello");
parameterFieldController.setCurrentValue("sub", "StringParam", "Subreport string value");
parameterFieldController.setCurrentValue("", "BooleanParam", true);
parameterFieldController.setCurrentValue("", "CurrencyParam", 123.45);
parameterFieldController.setCurrentValue("", "NumberParam", 123);
Calendar calendar = Calendar.getInstance();
calendar.clear();
// Ensure h:m:s and milliseconds cleared
calendar.set(2004, Calendar.FEBRUARY, 17);
// February 17, 2004 - Month is 0-based.
parameterFieldController.setCurrentValue("", "DateParam", calendar.getTime());
calendar.clear();
calendar.set(2002, Calendar.JUNE, 12, 8, 23, 15);
parameterFieldController.setCurrentValue("", "DateTimeParam", calendar.getTime());
calendar.clear();
calendar.set(2002, Calendar.JUNE, 12, 13, 44, 59);
parameterFieldController.setCurrentValue("", "TimeParam", calendar.getTime());
// Multi-valued string
parameterFieldController.setCurrentValues("", "StringMultiParam",
        new Object[]{"One String", "Two String", "Three String"});
// Range value
ParameterFieldRangeValue parameterFieldRangeValue = new ParameterFieldRangeValue();
parameterFieldRangeValue.setBeginValue(1);
parameterFieldRangeValue.setEndValue(100);
parameterFieldRangeValue.setLowerBoundType(RangeValueBoundType.inclusive);
parameterFieldRangeValue.setUpperBoundType(RangeValueBoundType.inclusive);
parameterFieldController.setCurrentValue("", "NumberRangeParam", parameterFieldRangeValue);
// Multi Range values
ParameterFieldRangeValue[] parameterFieldRangeValues = new ParameterFieldRangeValue[2];
parameterFieldRangeValues[0] = new ParameterFieldRangeValue();
parameterFieldRangeValues[0].setBeginValue(12);
parameterFieldRangeValues[0].setEndValue(15);
parameterFieldRangeValues[0].setLowerBoundType(RangeValueBoundType.inclusive);
parameterFieldRangeValues[0].setUpperBoundType(RangeValueBoundType.exclusive);
parameterFieldRangeValues[1] = new ParameterFieldRangeValue();
parameterFieldRangeValues[1].setBeginValue(100);
parameterFieldRangeValues[1].setEndValue(200);
parameterFieldRangeValues[1].setLowerBoundType(RangeValueBoundType.exclusive);
parameterFieldRangeValues[1].setUpperBoundType(RangeValueBoundType.exclusive);
parameterFieldController.setCurrentValues("", "NumberMultiRangeParam", parameterFieldRangeValues);

最终输出PDF效果:

展示报表

4.2.4子报表

与4.2.3采用相同示例,配置模板如下:

子报表模板配置

实际展示效果如4.2.3

五.收费模式

1.配置工具

工具名称:SAP Crystal Reports2016

软件单价8600元,在线备份服务76.25元,数据来源为官网,查阅时间为2019年8月8日。

2.开发工具包

免费。