Crystal 水晶报表的数据源

背景知识:

1. 水晶报表主要用于设计和产生报表, 很多企业级系统都可以拿来集成,使得系统数据展示给客户

2. 水晶报表的模板是rpt文件,其中包含了一些数据源,以及具体的展示内容,rpt文件需要用Crytal软件创建编辑


本文记录一下水晶报表的数据源配置(1,2,3)

ReportClientDocument clientDoc = new ReportClientDocument();
clientDoc.setReportAppServer(ReportClientDocument.inprocConnectionString);
clientDoc.open(reportPath, OpenReportOptions._openAsReadOnly);
ITable table = clientDoc.getDatabaseController().getDatabase().getTables().getTable(0);
IConnectionInfo oldConnectionInfo =  new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();
DatabaseController dbController = clientDoc.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);  

1.通过JNDI

PropertyBag newPropertyBag = new PropertyBag();
propertyBag.put("Trusted_Connection", "false");
propertyBag.put("Server Type", "JDBC (JNDI)");
propertyBag.put("Use JDBC", true);
propertyBag.put("JNDIOptionalName", jndiName);  
propertyBag.put("Use JNDI", true);
propertyBag.put("JNDI Data Source Name", jndiName);
propertyBag.put("JNDI Datasource Name", jndiName);

newConnectionInfo.setAttributes(newPropertyBag);
newConnectionInfo.setKind(ConnectionInfoKind.SQL);

2.直接连接数据库 需要DB URL和DB用户名密码

PropertyBag newPropertyBag = new PropertyBag();
newPropertyBag.put("Trusted_Connection", "false");
newPropertyBag.put("PreQEServerName", "jdbc:microsoft:sqlserver://<servername>:<port>/DBNAME");
newPropertyBag.put("Server Type", "JDBC (JNDI)");
newPropertyBag.put("Database DLL", "crdb_jdbc.dll");
newPropertyBag.put("Database Class Name", "com.microsoft.jdbc.sqlserver.SQLServerDriver");
newPropertyBag.put("Use JDBC", "true");
newPropertyBag.put("Server Name", "jdbc:microsoft:sqlserver://<servername>:<port>/DBNAME");
newPropertyBag.put("Connection URL","jdbc:microsoft:sqlserver://<servername>:<port>/DBNAME");

newConnectionInfo.setUserName(<username>);
newConnectionInfo.setPassword(<password>);

newConnectionInfo.setAttributes(newPropertyBag);
newConnectionInfo.setKind(ConnectionInfoKind.SQL);

3.通过Result Set

ResultSet rs = ...;
String tableAlias = table.getAlias();
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
if ("Command".equalsIgnoreCase(tableAlias.trim())) {
 dbController.addDataSource(rs);
}else{  
dbController.setDataSource(rs, tableAlias, "resultsetTable");
}


水晶报表可以通过CrystalReportViewer 在前端展示 

CrystalReportViewer viewer = new CrystalReportViewer();
viewer.setReportSource(reportSource);
viewer.setOwnPage(true);
viewer.setHasExportButton(true);
viewer.setHasToggleParameterPanelButton(false);
viewer.setPrintMode(CrPrintMode.PDF);
viewer.setHasRefreshButton(false);
viewer.setHasSearchButton(true);
viewer.setHasPrintButton(true);
.................

也可以在后台生成PDF文件

ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) clientDoc.getPrintOutputController().export(ReportExportFormat.PDF);


记录于此 备用 有不对的地方 请指正


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值