多个润乾报表,导出到一个excel中的不同sheet页中

润乾报表作为报表编辑工具使用起来十分的简单方便,只需要简单的属性设置便可以实现导出、打印等功能(具体参见润乾开发应用文档),令人很郁闷的是润乾的导出只是单表的导出。

最近就有这么一个需求:有很多.raq报表文件,在一个页面中列出所有的文件的名称,通过复选框选中下载,要求导出到一个excel中,分不同的sheet页展示(不知各位是否明白此需求,我的表达能力有限请见谅)。

郁闷了我好一阵子,问同事都没有这么做过!

闲着没事翻看润乾jar包,发现了一些端倪。在润乾安装目录里有润乾API,闲着没事可以翻阅。

API中有ExcelReport类可以帮助我们实现上面的需求。

话不多说看代码:

Java代码 复制代码 收藏代码

  1. package cn.com.victorysoft.wellinfo.util;
  2. import java.io.FileOutputStream;
  3. import java.sql.Connection;
  4. import java.util.Map;
  5. import javax.sql.DataSource;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import com.runqian.base4.util.DBTypes;
  8. import com.runqian.report4.usermodel.Context;
  9. import com.runqian.report4.usermodel.DataSourceConfig;
  10. import com.runqian.report4.usermodel.Engine;
  11. import com.runqian.report4.usermodel.IReport;
  12. import com.runqian.report4.util.ReportUtils;
  13. import com.runqian.report4.view.excel.ExcelReport;
  14.  
  15. public class ExportReport {
  16.  
  17.  
  18. private String reportPath = "";
  19.  
  20. private String excelPath="";
  21.  
  22. private String excelFileName;
  23.  
  24. private DataSource dataSource;
  25.  
  26. private String reportDataSource;
  27.  
  28.  
  29. private int dbType = DBTypes.ORACLE;
  30.  
  31. private boolean needTranContent = true;
  32.  
  33. private String dbCharset = "GBK";
  34.  
  35. private String clientCharset = "GBK";
  36.  
  37. private boolean needTranSentence = false;
  38.  
  39. public void saveExcel(Map param, Map macro, String[] reportName, String[] sheetName)
  40. throws Exception{
  41. int flag = 0;
  42. ExcelReport erp=null;
  43. JdbcTemplate JT = new JdbcTemplate();
  44. JT.setDataSource(this.dataSource);
  45. Connection con = JT.getDataSource().getConnection();
  46. DataSourceConfig dsoc = new DataSourceConfig(this.dbType, this.needTranContent,
  47. this.dbCharset, this.clientCharset, this.needTranSentence);
  48. //判断excelFileName是不是为“”或null,以及是不是excel类型
  49. if(!"".equals(this.null2blank(this.excelFileName)) && this.isExcelFile(this.excelFileName)){
  50. FileOutputStream fos = new FileOutputStream((this.null2blank(this.excelPath)+this.excelFileName).trim());
  51. try {
  52. erp = new ExcelReport();
  53. } catch (Throwable e) {
  54. e.printStackTrace();
  55. }
  56. if(reportName != null && reportName.length>0){
  57. //初始化数组
  58. IReport[] rd = new IReport[reportName.length];
  59. Context[] context = new Context[reportName.length];
  60. Engine engine[] = new Engine[reportName.length];
  61. IReport iReport[] = new IReport[reportName.length];
  62. //如果sheetName为空或者长度为零,那么设置默认选项
  63. if (sheetName == null || sheetName.length<=0) {
  64. sheetName = new String[reportName.length];
  65. for (int i = 0; i < reportName.length; i++) {
  66. sheetName[i] = "report_"+String.valueOf(i);
  67. }
  68. }
  69. for (int i = 0; i < reportName.length; i++) {
  70. if(!"".equals(this.null2blank(reportName[i]))){
  71. //读取报表模板,把*.raq文件读入内存,并实例化ReportDefine
  72. rd[i] = ReportUtils.read((this.null2blank(this.reportPath)+reportName[i]).trim());
  73. //新建上下文对象,在上下文对象中设置数据源
  74. context[i] = new Context();
  75. context[i].setDefDataSourceName(this.reportDataSource);
  76. context[i].setConnection(this.reportDataSource,con);
  77. context[i].setDataSourceConfig(this.reportDataSource, dsoc);
  78. //设置参数和宏
  79. if(param != null && param.size()>0){
  80. context[i].setParamMap(param);
  81. }
  82. if(macro != null && macro.size()>0){
  83. Object[] array = macro.keySet().toArray();
  84. for (int j = 0; j < array.length; j++) {
  85. context[i].setMacroValue(array[j].toString(), macro.get(array[j]).toString());
  86. }
  87. }
  88. //运算报表,并在页面上输出报表
  89. engine[i] = new Engine(rd[i],context[i]);
  90. iReport[i] = engine[i].calc();
  91. //构建每个sheet页
  92. erp.addPage(iReport[i],sheetName[i]);
  93. flag ++;
  94. }
  95. }
  96. }else {
  97. throw new Exception("reportName为“”或null");
  98. }
  99. //导出Excel
  100. if (flag != 0) {
  101. erp.out(fos);
  102. }
  103. fos.flush();
  104. fos.close();
  105. }else {
  106. throw new Exception("excelFileName为“”或null或不是excel类型");
  107. }
  108. }
  109.  
  110. private String null2blank(String variable) {
  111. if (variable == null)
  112. variable = "";
  113. return variable;
  114. }
  115.  
  116. private boolean isExcelFile(String excelName){
  117. boolean is = false;
  118. if(excelName.endsWith(".xls") || excelName.endsWith(".xlsx")){
  119. is = true;
  120. }
  121. return is;
  122. }
  123.  
  124. public String getReportPath() {
  125. return reportPath;
  126. }
  127. public void setReportPath(String reportPath) {
  128. this.reportPath = reportPath;
  129. }
  130. public String getExcelPath() {
  131. return excelPath;
  132. }
  133. public void setExcelPath(String excelPath) {
  134. this.excelPath = excelPath;
  135. }
  136. public String getExcelFileName() {
  137. return excelFileName;
  138. }
  139. public void setExcelFileName(String excelFileName) {
  140. this.excelFileName = excelFileName;
  141. }
  142. public DataSource getDataSource() {
  143. return dataSource;
  144. }
  145. public void setDataSource(DataSource dataSource) {
  146. this.dataSource = dataSource;
  147. }
  148. public String getReportDataSource() {
  149. return reportDataSource;
  150. }
  151. public void setReportDataSource(String reportDataSource) {
  152. this.reportDataSource = reportDataSource;
  153. }
  154. public int getDbType() {
  155. return dbType;
  156. }
  157. public void setDbType(int dbType) {
  158. this.dbType = dbType;
  159. }
  160. public boolean isNeedTranContent() {
  161. return needTranContent;
  162. }
  163. public void setNeedTranContent(boolean needTranContent) {
  164. this.needTranContent = needTranContent;
  165. }
  166. public String getDbCharset() {
  167. return dbCharset;
  168. }
  169. public void setDbCharset(String dbCharset) {
  170. this.dbCharset = dbCharset;
  171. }
  172. public String getClientCharset() {
  173. return clientCharset;
  174. }
  175. public void setClientCharset(String clientCharset) {
  176. this.clientCharset = clientCharset;
  177. }
  178. public boolean isNeedTranSentence() {
  179. return needTranSentence;
  180. }
  181. public void setNeedTranSentence(boolean needTranSentence) {
  182. this.needTranSentence = needTranSentence;
  183. }
  184. }

以上是关键代码,完整代码示例请参见附加。代码排版有些乱,请童鞋们见谅,如果上面的代码不想看,就下载附件吧,附加注释很详细

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值