润乾报表作为报表编辑工具使用起来十分的简单方便,只需要简单的属性设置便可以实现导出、打印等功能(具体参见润乾开发应用文档),令人很郁闷的是润乾的导出只是单表的导出。
最近就有这么一个需求:有很多.raq报表文件,在一个页面中列出所有的文件的名称,通过复选框选中下载,要求导出到一个excel中,分不同的sheet页展示(不知各位是否明白此需求,我的表达能力有限请见谅)。
郁闷了我好一阵子,问同事都没有这么做过!
闲着没事翻看润乾jar包,发现了一些端倪。在润乾安装目录里有润乾API,闲着没事可以翻阅。
API中有ExcelReport类可以帮助我们实现上面的需求。
话不多说看代码:
package cn.com.victorysoft.wellinfo.util;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.util.Map;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import com.runqian.base4.util.DBTypes;
import com.runqian.report4.usermodel.Context;
import com.runqian.report4.usermodel.DataSourceConfig;
import com.runqian.report4.usermodel.Engine;
import com.runqian.report4.usermodel.IReport;
import com.runqian.report4.util.ReportUtils;
import com.runqian.report4.view.excel.ExcelReport;
/**
* @Description 多个润乾报表导出到一个excel中的不同sheet页中
* @ClassName ExportReport2
* @author Gao Jie-quan
* @Created 2010 Oct 12, 2010 3:23:35 PM
*/
public class ExportReport {
/**
* @Description 一些必须设置的属性
* @created 2010 Oct 12, 2010 4:32:39 PM
*/
/**
* @Description 润乾.raq文件存储路径,
* 此属性可以不设置,但是方法传值reportName时必须包含物理路径,
* @field String reportPath
* @created 2010 Oct 12, 2010 5:14:21 PM
*/
private String reportPath = "";
/**
* @Description 导出的excel存储路径
* 此属性可以不设置,但是方法传值excelFileName的时候必须包含物理路径,
* @field String excelPath
* @created 2010 Oct 12, 2010 5:14:34 PM
*/
private String excelPath="";
/**
* @Description excel文件名
* @field String excelFileName
* @created 2010 Oct 12, 2010 5:14:47 PM
*/
private String excelFileName;
/**
* @Description 数据源
* @field DataSource dataSource
* @created 2010 Oct 12, 2010 5:14:57 PM
*/
private DataSource dataSource;
/**
* @Description 润乾中使用的默认数据源名称
* @field String reportDataSource
* @created 2010 Oct 12, 2010 5:15:08 PM
*/
private String reportDataSource;
/**
* @Description 构造数据源配置的相关属性,包含默认初始值
* @created 2010 Oct 12, 2010 4:32:39 PM
*/
/**
* @Description 数据源类型,默认为Oracle 参见com.runqian.base4.util.DBTypes
* @field int dbType
* @created 2010 Oct 12, 2010 5:05:12 PM
*/
private int dbType = DBTypes.ORACLE;
/**
* @Description 是否需要转换检索内容的编码
*@field boolean needTranContent
* @created 2010 Oct 12, 2010 5:09:48 PM
*/
private boolean needTranContent = true;
/**
* @Description 数据源使用的字符集名,默认为GBK
* @field String dbCharset
* @created 2010 Oct 12, 2010 5:11:18 PM
*/
private String dbCharset = "GBK";
/**
* @Description 客户端使用的字符集名,默认为GBK
* @field String clientCharset
* @created 2010 Oct 12, 2010 5:11:40 PM
*/
private String clientCharset = "GBK";
/**
* @Description 是否需要转换检索语句的编码
* @field boolean needConvert
* @created 2010 Oct 12, 2010 5:12:50 PM
*/
private boolean needTranSentence = false;
/**
* @Description 保存excel
* @param param 报表参数
* @param macro 报表宏
* @param reportName .raq名称(要包含.raq后缀),
* 如果reportPath属性没有设置,那么reportName必须包含完整的物理路径
* @param sheetName 保存到excel中sheet页的名称,
* @throws Exception
* @ReturnType void
* @author Gao Jie-quan
* @Created 2010 Oct 12, 20103:23:46 PM
*/
public void saveExcel(Map param, Map macro, String[] reportName, String[] sheetName)
throws Exception{
int flag = 0;
ExcelReport erp=null;
JdbcTemplate JT = new JdbcTemplate();
JT.setDataSource(this.dataSource);
Connection con = JT.getDataSource().getConnection();
DataSourceConfig dsoc = new DataSourceConfig(this.dbType, this.needTranContent,
this.dbCharset, this.clientCharset, this.needTranSentence);
//判断excelFileName是不是为“”或null,以及是不是excel类型
if(!"".equals(this.null2blank(this.excelFileName)) && this.isExcelFile(this.excelFileName)){
FileOutputStream fos = new FileOutputStream((this.null2blank(this.excelPath)+this.excelFileName).trim());
try {
erp = new ExcelReport();
} catch (Throwable e) {
e.printStackTrace();
}
if(reportName != null && reportName.length>0){
//初始化数组
IReport[] rd = new IReport[reportName.length];
Context[] context = new Context[reportName.length];
Engine engine[] = new Engine[reportName.length];
IReport iReport[] = new IReport[reportName.length];
//如果sheetName为空或者长度为零,那么设置默认选项
if (sheetName == null || sheetName.length<=0) {
sheetName = new String[reportName.length];
for (int i = 0; i < reportName.length; i++) {
sheetName[i] = "report_"+String.valueOf(i);
}
}
for (int i = 0; i < reportName.length; i++) {
if(!"".equals(this.null2blank(reportName[i]))){
//读取报表模板,把*.raq文件读入内存,并实例化ReportDefine
rd[i] = ReportUtils.read((this.null2blank(this.reportPath)+reportName[i]).trim());
//新建上下文对象,在上下文对象中设置数据源
context[i] = new Context();
context[i].setDefDataSourceName(this.reportDataSource);
context[i].setConnection(this.reportDataSource,con);
context[i].setDataSourceConfig(this.reportDataSource, dsoc);
//设置参数和宏
if(param != null && param.size()>0){
context[i].setParamMap(param);
}
if(macro != null && macro.size()>0){
Object[] array = macro.keySet().toArray();
for (int j = 0; j < array.length; j++) {
context[i].setMacroValue(array[j].toString(), macro.get(array[j]).toString());
}
}
//运算报表,并在页面上输出报表
engine[i] = new Engine(rd[i],context[i]);
iReport[i] = engine[i].calc();
//构建每个sheet页
erp.addPage(iReport[i],sheetName[i]);
flag ++;
}
}
}else {
throw new Exception("reportName为“”或null");
}
//导出Excel
if (flag != 0) {
erp.out(fos);
}
fos.flush();
fos.close();
}else {
throw new Exception("excelFileName为“”或null或不是excel类型");
}
}
/**
* 判断字符串是否为null,如果为null,则转换为“”,否则返回源字符串
*
* @param variable
* @return
*/
private String null2blank(String variable) {
if (variable == null)
variable = "";
return variable;
}
/**
* @Description 判断是不是excel文件类型
* @param excelName
* @return
* @ReturnType boolean
* @author Gao Jie-quan
* @Created 2010 Oct 13, 20108:58:24 AM
*/
private boolean isExcelFile(String excelName){
boolean is = false;
if(excelName.endsWith(".xls") || excelName.endsWith(".xlsx")){
is = true;
}
return is;
}
/**
* @Description 相关属性的Get/Set方法
* @ReturnType String
* @author Gao Jie-quan
* @Created 2010 Oct 12, 20104:39:39 PM
*/
public String getReportPath() {
return reportPath;
}
public void setReportPath(String reportPath) {
this.reportPath = reportPath;
}
public String getExcelPath() {
return excelPath;
}
public void setExcelPath(String excelPath) {
this.excelPath = excelPath;
}
public String getExcelFileName() {
return excelFileName;
}
public void setExcelFileName(String excelFileName) {
this.excelFileName = excelFileName;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public String getReportDataSource() {
return reportDataSource;
}
public void setReportDataSource(String reportDataSource) {
this.reportDataSource = reportDataSource;
}
public int getDbType() {
return dbType;
}
public void setDbType(int dbType) {
this.dbType = dbType;
}
public boolean isNeedTranContent() {
return needTranContent;
}
public void setNeedTranContent(boolean needTranContent) {
this.needTranContent = needTranContent;
}
public String getDbCharset() {
return dbCharset;
}
public void setDbCharset(String dbCharset) {
this.dbCharset = dbCharset;
}
public String getClientCharset() {
return clientCharset;
}
public void setClientCharset(String clientCharset) {
this.clientCharset = clientCharset;
}
public boolean isNeedTranSentence() {
return needTranSentence;
}
public void setNeedTranSentence(boolean needTranSentence) {
this.needTranSentence = needTranSentence;
}
}
以上是关键代码,完整代码示例请参见附加。代码排版有些乱,请童鞋们见谅,如果上面的代码不想看,就下载附件吧,附加注释很详细
分享到:
相关推荐
Aspose.Cells写入一个excel多个活页内容进行导出
AdvancedDataGrid导出复杂表头excel
一个Maven项目,下载直接使用,直接找到 JxlsTest运行demo即可,功能强大,基本能满足所有报表的导出需求,支持多个Sheet导出,支持复杂的导出组合,可以自己写模板试玩下。
本文主要介绍了Java根据模板导出Excel报表并复制模板生成多个Sheet页的方法,具有很好的参考价值。下面跟着小编一起来看下吧
labVIEW 数据存储到Excel,并分布到不同Sheet中,
在实际的应用中会经常需要将数据导出成excel,导出的方式除原样导出还有分页导出、分页分sheet导出和大数据量导出,这些导出方式在JAVA程序中分别有不同的接口来实现。
jxls实现多sheet的报表导出-附件资源
主要功能包含:一是VBA连接SQL Server数据库查询的功能,二是根据自动获取的日期动态将结果数据导出为多个Excel文档的功能。导出的文件命名为按机构编码和日期动态命名方法,格式:机构编码+主文件名+日期,生成的...
2、 多报表运行环境:能在多个报表运行环境中切换设计报表。 3、 多数据源关联:同一张报表可以从多个数据库或者多个数据表中取数;并且在报表中允许对 多个数据集进行关联运算。 4、 参数查询界面:通过拖拽参数...
支持多个DataWindow输出到同一个Excel文件 输出时进度条展示,避免大量数据输出时,因为耗时让用户误以为软件无响应了。 软件支持中文简体、繁体和英文三种提示信息,以适应不同的操作系统环境。 纯c++开发组件库,...
4.进一步完善导出Excel的算法,将多个页面格式迥异的报表导出在一个Sheet上时,改进了算法,使得在Excel中所需列的数量和宽度计算精确度更高,真正做到无失真导出,容错性更好,对于任何复杂的报表,都可以正确导出 ...
该组件是个超完美的报表解决方案,支持在同个Excel中的多个sheet,这样就不受Excel(2003)仅能放65535条记录的限制了.支持模板,我在多个项目中用到,觉得很棒奉献给大家
在数据管理系统中,添加大量数据,如果通过手动录入的话,相当费事费力。最近开发一款数据管理系统,所以我花了半天时间将该功能梳理出来。该资料将框架和其他功能都简化,主要实现该功能。
DocWriter:导出类,其方法分为非终端操作与终端操作,终端操作会输出并关闭该流,非终端操作则可以继续接着读取,应对一张excel中含有多个sheet的情况。 DocReader:读取类,与上述ExcelWriter一样的操作。 ...
【工作表折分】:将当前工作表的数据按条件拆分成多个工作表,可以用任意列的数据做为拆分条件 【合并工作簿】:将指文件夹中所有工作簿中所有工作表数据合并起来。有两种合并方式:将每个工作簿中的工作表合到当前...
【多区域复制】突破Excel的限制,可以对选择的多个区域进复制与粘贴,在右键菜单中调用 【返回首页】配合“建立工作表目录”工具使用,可以在任何工作表中瞬间返回第一个工作表 Excel百宝箱 26个函数功能介绍如下: ...
目的是主要应用于在项目中生成Excel,比如在报表中导出Excel等,所以不包括像公式这样的在导出时用不到的功能。 代码分为头文件和Cpp文件,但不包括里面用到的一个自定义类CTArray,这个类是我自己实现的。看情况...
【工作表折分】:将当前工作表的数据按条件拆分成多个工作表,可以用任意列的数据做为拆分条件 【合并工作簿】:将指文件夹中所有工作簿中所有工作表数据合并起来。有两种合并方式:将每个工作簿中的工作表合到当前...