专业Java报表工具Stimulsoft Reports.Java常见问题解答(二)

发布时间 : 2021-02-05 11:28:05.930|阅读 291 次

概述:本文介绍了Stimulsoft Reports.Java使用过程中常见问题解答。

Stimulsoft Reports.Java是一个专为在Java应用程序中的报表进行交互和处理的报表工具。 Java技术允许在不同的平台、不同的操作系统和不同的硬件上使用程序。正因为这样,Stimulsoft Reports.Java现在几乎在任何地方都可用,同时还是一个拥有高质量的功能和用户友好的报表工具。

立即点击下载Stimulsoft Reports.Java v2020.2.2最新版

5.如何使用报表设计器创建示例页面?

用报表设计器创建一个简单的页面。为此,请将以下库放入WebContent \ WEB-INF \ lib \目录中:stimulsoft.lib.jar,stimulsoft.reports-base.jar,stimulsoft.reports-report.jar,stimulsoft.reports-flex.jar, stimulsoft.reports-web.jar。结果,可以看到以下内容(下图):

在项目中放置库

图8.将库放置在项目中

接下来,打开web.xml进行编辑,它应该类似于清单2:
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/webapp_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee"
id="WebApp_ID" version="2.5">
<display-name>sti_webviewer</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- configuration, this parameter indicates the main application directory -->
<servlet>
<servlet-name>StimulsoftResource</servlet-name>
<servlet-class>com.stimulsoft.web.servlet.StiWebResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StimulsoftResource</servlet-name>
<url-pattern>/stimulsoft_web_resource</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>StimulsoftAction</servlet-name>
<servlet-class>com.stimulsoft.webviewer.servlet.StiWebViewerActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StimulsoftAction</servlet-name>
<url-pattern>/stimulsoft_webviewer_action</url-pattern>
</servlet-mapping>
</web-app>

清单2. web.xml的内容

其余的web.xml块保持不变,它们定义了工作所需的servlet。然后,编辑index.jsp(请参见下面的代码):
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %>
<%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Stimulsoft Reports.Fx for Java</title>
</head>
<body>
<h1 align="center">My first report!</h1>
<stidesignerfx:iframe
width="100%" height="90%" align="middle"
styleClass="" frameborder="0" styleId=""
marginheight="4" marginwidth="10" name="stiviewer"
scrolling="no" style="" title="report"/>
</body>
</html>
清单3. index.jsp的内容

在JSP中添加taglib指令(清单4)。它们将与页面上的自定义标签一起使用。
<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %>
<%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>

清单4.自定义Stimulsoft标签

添加标签<stidesignerfx:iframe />,它是html标签iframe的类似物,并支持其所有属性。作为应用程序部署的结果,请参见以下内容(图9):

我的第一份报告!

6.您是否有自定义标签的描述?

从清单5中可以看出,它分为两个组件:DesignerFx和ViewerFx。考虑一个DesignerFx组件。对于ViewerFx,其工作方式相同。
<stidesignerfx:link text="a link for jumping to the Designer"/>
<stidesignerfx:button value="a button for jumping to the Designer"/>
<stidesignerfx:frame title="analog of the html tag frame which contains a Designer"/>
<stidesignerfx:iframe title="analog of the html tag iframe which contains a Designer"/>

所有这些都是类似HTML标记的类似物,支持所有属性。扩展了标准属性列表,以显示报告和设置报告变量。report =“ SimpleList.mrt”属性将打开名称为SimpleList.mrt的报告。报表中的变量可以通过两种方式传递:

以以下格式将variableStr属性的值设置为字符串:“ Variable1 = value1&Variable2 = value2”。在这种情况下,将在报表中传递两个具有value1的变量Variable1和具有value2的Variable2。例如,您需要编辑index.jsp文件以通过单击按钮打开名为MyFirstReport.mrt的报告,并且MyVar报告变量具有stidesignerfx值(清单6)。
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %>
<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %>
<%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>

<html>
<head>
<title>Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<stidesignerfx:button value="Run the report designer" report="MyFirstReport.mrt" variableStr="MyVar=stidesignerfx" />
</html>
也可以将参数作为Map <string,string =>传递给报表。重新设计我们的网页,如下所示(清单6)。在这种情况下,名称为MyFirstReport.mrt的报表将被加载到报表中,并将两个参数传递到该报表中。
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page contentType="text/html;charset=UTF-8" import="java.util.*" %>
<%@ taglib uri="http://stimulsoft.com/designer" prefix="stidesignerfx" %>
<%@ taglib uri="http://stimulsoft.com/viewer" prefix="stiviewerfx" %>

<html>
<head>
<title>Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<%
Map<String, String>variableMap= new HashMap<String, String>();
variableMap.put("Variable1", "var1");
variableMap.put("Variable2", "var2");
request.setAttribute("myMap", variableMap);
%>
<stidesignerfx:iframe report="MyFirstReport.mrt" variableMap="myMap"
width="100%" height="100%" align="right"
styleClass="" frameborder="0" styleId=""
marginheight="1" marginwidth="1" name="stidesignerfx"
scrolling="no" style="" title="report" />
</body>
</html>
此处的数据作为HashMap传递,此参数应设置为请求或会话,并将其存在的键作为variableMap属性传递给标签。不允许应用两个属性variableMap和variableStr。

7.如何覆盖加载,保存和加载自定义数据的方法?

在运行之前,应先配置应用程序。为了进行配置,使用了web.xml中指定的
my.servlet.ApplicationInitializer类。
package my.servlet;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import my.actions.MyLoadAction;
import my.actions.MyLoadDataAction;
import my.actions.MyLocalizationAction;
import my.actions.MyMailAction;
import my.actions.MyRenderReportAction;
import my.actions.MySaveAction;

import com.stimulsoft.base.exception.StiException;
import com.stimulsoft.flex.StiFlexConfig;

/**
 * Application initialization.
 */
public class ApplicationInitializer implements ServletContextListener {

@Override
public void contextInitialized(final ServletContextEvent event) {
try {
// configuration application
StiFlexConfig stiConfig = initConfigWithoutDir();
// ---------------------------------------------------------
// need to override the standard methods
// another comment
stiConfig.setLoadClass(MyLoadAction.class);
stiConfig.setSaveClass(MySaveAction.class);
stiConfig.setLoadDataClass(MyLoadDataAction.class);
stiConfig.setMailAction(MyMailAction.class);
stiConfig.setLocalizationAction(MyLocalizationAction.class);
stiConfig.setRenderReportAction(MyRenderReportAction.class);
// ---------------------------------------------------------

StiFlexConfig.init(stiConfig);

// set variable in servlet context attribute
// Map<String, String> myVariableMap = new HashMap<String, String>();
// myVariableMap.put("Variable1", "myVariableMap");
// event.getServletContext().setAttribute("myMap", myVariableMap);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void contextDestroyed(final ServletContextEvent event) {
// empty
}

public StiFlexConfig initConfigWithoutDir() throws StiException, IOException {
Properties properties = new Properties();
// load your own Properties;
// InputStream inStream = getClass().getResourceAsStream("RESOURCE_PATH");
// properties.load(inStream);
return new StiFlexConfig(properties);
}
}
将在其中定义带有文件stimulsoft.properties的主应用程序目录。为了保存或加载自己的报告,有必要在配置中指定这些类,就像指定可以从xml加载数据的类一样。类如下:列出MyLoadAction.java
package my.actions;

import java.io.InputStream;

import com.stimulsoft.actions.LoadAction;
import com.stimulsoft.library.LoadFileReport;

public class MyLoadAction extends LoadAction {
@Override
public InputStream load(String repotrName) {
System.out.println("my load class" + repotrName);
return new LoadFileReport().getReport(repotrName);
}
}
package my.actions;

import com.stimulsoft.flex.StiSaveAction;
import com.stimulsoft.flex.utils.StiOperationResult;
import com.stimulsoft.flex.utils.StiSaveLoadFileReport;

public class MySaveAction extends StiSaveAction {
@Override
public StiOperationResult save(String report, String reportName, boolean newReportFlag) {
System.out.println("must override this method to specify your own save report");
return new StiSaveLoadFileReport().save(report, reportName, newReportFlag);
}

}
package my.actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.stimulsoft.flex.StiLoadDataAction;

public class MyLoadDataAction extends StiLoadDataAction {
@Override
protected String getConnectionString() {
System.out.println("must override this method to specify your own connection string");
// return
// "Data Source=localhost\\SQLEXPRESS;Initial Catalog=Mybase;User ID=UserName; Password=Password;";
return super.getConnectionString();
}

@Override
protected String getUserName() {
System.out.println("must override this method to specify your own user name");
// return "UserName";
return super.getUserName();
}

@Override
protected String getPassword() {
System.out.println("must override this method to specify your own password");
// return "Password";
return super.getPassword();
}

@Override
protected String getQuery() {
System.out.println("my Query " + super.getQuery());
return super.getQuery();
}

@Override
public Connection getConnection() throws ClassNotFoundException, SQLException {
System.out.println("must override this method to specify your own connection");
boolean overrideByConnectionString = getConnectionString() != null
&& getConnectionString().equals("needOverride");
boolean overrideByDataSource = getDataSourceName() != null
&& getDataSourceName().equals("DataSourceOverride");
if (overrideByConnectionString || overrideByDataSource) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Properties info = new Properties();
info.setProperty("user", "test");
info.setProperty("password", "test");
String connectionString = "jdbc:sqlserver://localhost\\SQLEXPRESS1:1433;databaseName=mybase;";
return DriverManager.getConnection(connectionString, info);
} else {
return super.getConnection();
}
}
}

8.如何使用SWT(标准小部件工具包)组件?

创建一个新的Java项目。为此,请选择文件>新建> Java项目。在项目向导中,输入项目的名称,例如“ swtfx”。在项目属性中,将一个关系添加到“ Standard Widget Toolkit(SWT)”库,以设计应用程序GUI。还要添加用于处理报告的库:stimulsoft.reports.fx-core-2011.1.1000.jarstimulsoft.reports.fx-designer-2011.1.1000.jar,stimulsoft.reports.fx-viewer-2011.1.1000.jar,stimulsoft。 reports.fx-swt-2011.1.1000.jar。

创建一个新的Java项目

然后使用包my.swt和报告名称创建一个新类。
package my.swt;
  
import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.stimulsoft.StiConfig;
import com.stimulsoft.StiReportSwt;
import com.stimulsoft.library.StiException;
import com.stimulsoft.widgets.DesignerFx;

public class Reports {
public static void main(String[] args) {
initReport();
Display display = new Display();
Shell shell = new Shell(display);
GridLayoutgridLayout = newGridLayout();
gridLayout.marginHeight=0;
gridLayout.marginWidth=0;

shell.setLayout(gridLayout);
shell.setText("My report");
try {
DesignerFx designer = new DesignerFx(shell, SWT.NO);
GridDatagridData = new GridData(GridData.FILL_BOTH);
designer.getComposite().setLayoutData(gridData);
} catch (StiException e) {
e.printStackTrace();
Label label = new Label(shell, SWT.NONE);
label.setLayoutData(newGridData(SWT.BEGINNING, SWT.CENTER, false, false));
label.setText("failed to create reports designer");
}
shell.setMaximized(true);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

Private static void initReport() {
try {
File stiDir = new File("D:/stimulsoft");
StiConfig stiConfig = new StiConfig(stiDir);
StiReportSwt.init(stiConfig);
} catch (StiException e) {
e.printStackTrace();
}
}

}
可以通过事件分发和修改报告和选项。为此,请按以下方式更改Reports.java中的代码。
package my.swt;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.stimulsoft.StiConfig;
import com.stimulsoft.StiReportSwt;
import com.stimulsoft.library.StiException;
import com.stimulsoft.widgets.ViewerFx;

public class Reports {
Display display = new Display();
Shell shell = new Shell(display);
private final ViewerFx viewer;

public Reports() throws StiException {
shell.setLayout(new GridLayout(2, false));

Map<String, String> params = new HashMap<String, String>();
params.put("Variable1", "My parameter");

viewer = new ViewerFx(shell, SWT.NO, "SimpleList.mrt", params);
GridDatagridData = new GridData(GridData.FILL_BOTH);
viewer.getComposite().setLayoutData(gridData);

Button button = new Button(shell, SWT.PUSH);
button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
button.setText("update report");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
try {
Map<String, String>paramsNew = new HashMap<String,String>();
paramsNew.put("Variable1", "My new parameter");
viewer.assignReport("SimpleList.mrt", paramsNew);
} catch (Exception e) {
e.printStackTrace();
}
}

public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
shell.setMaximized(true);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}

public static void main(String[] args) {
try {
initReport();
new Reports();
} catch (StiException e) {
e.printStackTrace();
}
}

private static void initReport() throws StiException {
StiConfigstiConfig = new StiConfig(new File("D:/stimulsoft"));
StiReportSwt.init(stiConfig);
}
}
这将导致以下结果:

报告参数

单击更新报告按钮后,报告将被更新:

新报告参数

使用呈现报告的组件时,应在运行该应用程序之前配置该应用程序。应该创建以下方法。

private static void initReport() throws StiException {
File stiDir = new File("D:/Stimulsoft");
new Resource().copyAll(stiDir);
StiSwtConfig stiConfig = new StiSwtConfig(stiDir);
StiReportSwt.init(stiConfig);
}
将在其中 定义带有stimulsoft.properties文件的主应用程序目录。初始化与针对Web应用程序描述的初始化相同。还可以为设计者和查看者覆盖应用程序的端口。例如:
private static void initReport() throws StiException {
// Initialization is identical to that described for the web application
File stiDir = new File("D:/Stimulsoft");
new Resource().copyAll(stiDir);
StiSwtConfig stiConfig = new StiSwtConfig(stiDir);

// ---------------------------------------------------------
// need to override the standard methods
// another comment
stiConfig.setLoadClass(MyLoadAction.class).setSaveClass(MySaveAction.class);
stiConfig.setXMLDataClass(MyXMLDataAction.class).setLoadDataClass(MyLoadDataAction.class);
stiConfig.setViewerPort(7845).setDesignerPort(8789);
// ---------------------------------------------------------

StiReportSwt.init(stiConfig);
}
也可以将设计器和查看器组件用作对话框。组件类别:
com.stimulsoft.widgets.DesignerFxDialog
com.stimulsoft.widgets.ViewerFxDialog

使用Designer组件的示例:

package my.swt;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.stimulsoft.StiConfig;
import com.stimulsoft.StiReportSwt;
import com.stimulsoft.library.StiException;
import com.stimulsoft.widgets.DesignerFxDialog;

public class DialogReports {
public static void main(String[] args) {
try {
initReport();
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
final DesignerFxDialog dialog = new DesignerFxDialog(shell,
SWT.RESIZE | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
dialog.setSize(1250, 800);
Button button = new Button(shell, SWT.PUSH);
button.setText("open dialog");
button.addSelectionListener(new SelectionListener() {
public void widgetSelected(SelectionEvent arg0) {
try {
dialog.open("SimpleList.mrt");
} catch (StiException e) {
}
}

public void widgetDefaultSelected(SelectionEvent arg0) {
}
});
shell.pack();
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
} catch (StiException e) {
e.printStackTrace();
}
}

private static void initReport() throws StiException {
File stiDir = new File("D:/stimulsoft");
StiConfig stiConfig = new StiConfig(stiDir);
StiReportSwt.init(stiConfig);
}
}

产品介绍 | 下载试用 | 优惠活动 | 在线客服


在线
客服
微信
QQ 电话
023-68661681
返回
顶部