在使用Stimulsoft Reports.Fx for Java报表控件时,有些客户需要使用自己的方法来加载或者保存报表,要实现该功能就需要对控件提供的默认Loading和Saving方法进行重载,并加入自己的一些功能到里面,这篇文章主要举例说明如何进行打开和保存方法的重载,具体代码如下:
//MySaveReport.java
package my.servlet;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.stimulsoft.library.SaveResult;
import com.stimulsoft.servlet.SaveReportServlet;
import com.stimulsoft.utils.FileUtil;
/**
* The class demonstrates how you can make your save reports. It needs to
* extends from {@link SaveReportServlet} and override the
* {@link SaveReportServlet#save(String,String,boolean)} method.
*/
public class MySaveReport extends SaveReportServlet {
private static final long serialVersionUID = -2129063200286482193L;
private static final Logger LOG = Logger.getLogger(MySaveReport.class.getName());
private static final String MSG_SAVE = "Save report as '%s'";
private static final String ERROR_SAVE = "Error save report '%s'";
@Override
protected SaveResult save(String report, String reportName,
boolean newReportFlag) {
try {
// directory reports, if it is set in stimulsoft.properties,
// then use the method
// com.stimulsoft.library.ConfigureProperties.getReportsDir()
File reportDir = new File("D:/stimulsoft/reports/");
File file = new File(reportDir, reportName);
General 16
© 2012 Enter your company name
LOG.info(String.format(MSG_SAVE, file.getAbsolutePath()));
FileUtil.writeStringToFile(file, report);
} catch (Exception e) {
String msg = String.format(ERROR_SAVE, e.toString());
LOG.log(Level.SEVERE, msg, e);
return SaveResult.specifiedText(msg);
} return SaveResult.notShownMessage();
}
}
//MyLoadReport.java
package my.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.stimulsoft.servlet.LoadReportServlet;
/**
* The class demonstrates how you can make your own loading reports. It needs to
* extends from {@link LoadReportServlet} and override the
* {@link LoadReportServlet#load(String)} method.
*/
public class MyLoadReport extends LoadReportServlet {
private static final long serialVersionUID = -5262723990767315359L;
private static final Logger LOG = Logger.getLogger(MyLoadReport.class.getName());
private static final String MSG_REPOTR_FILE = "Load file report ''{0}''";
@Override
protected InputStream load(String repotrName) {
LOG.severe("must override this method to create your own loading reports");
// directory reports, if it is set in stimulsoft.properties,
// then use the method
// com.stimulsoft.library.ConfigureProperties.getReportsDir()
File reportDir = new File("D:/stimulsoft/reports/");
return getReport(reportDir, repotrName);
}
public InputStream getReport(File reportDir, String repotrName) {
17
© 2012 Enter your company name
File report = new File(reportDir, repotrName);
LOG.log(Level.INFO,
MessageFormat.format(MSG_REPOTR_FILE, report.getAbsolutePath()));
try {
return new FileInputStream(report);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}