1. 程式人生 > >潤乾報表頁面超過255列匯出excel

潤乾報表頁面超過255列匯出excel



最近接觸到一些客戶的需求,客戶想在頁面展現的時候不分頁,然後在匯出excel的時候分頁,而客戶的報表列數很多,超過了excel的255列的限制。這樣如果不做任何處理,直接在頁面上點選匯出excel的按鈕,就會彈出對話方塊提示excel不支援超過255列,也就無法正常匯出,所以要想匯出這樣的報表就必須採取一些特殊的設定,下面就做一個簡單的例子,實現超過255列的不分頁報表匯出excel。

有兩種辦法可以實現上面的需求,分別為:

1.展現時候展現一張不分頁的,匯出的時候匯出另外一張報表,在匯出的時候呼叫API匯出那個分頁的報表。

2.在jsp中設定width=”-1″ ,excelUsePaperSize=”yes”然後把報表設定成按紙分頁的,也可以正常匯出excel。

下面分別介紹一下這兩種方法的實現過程:

第一種:呼叫API的方法。

第一步:製作一張超過255列的報表,(這裡直接用to(1,300)函式實現了)

儲存這個報表為300col.raq。

第二步:設定分頁。

點選屬性-報表屬性-分頁,設定分頁方式為按紙分頁,然後另存這個報表為300col1.raq

第三步:編寫匯出的API

匯出excel的API內容如下:

<%@ page language=”java” contentType=”text/html; charset=gbk”
pageEncoding=”gbk”%>
<%@page
import=”com.runqian.report4.usermodel.*,com.runqian.report4.util.*,com.runqian.report4.model.ReportDefine”%>
<%@ page import=”java.io.*,com.runqian.report4.usermodel.DataSetConfig
“%>
<%
Context cxt = new Context();
String reporthome = Context.getInitCtx().getMainDir();
String path = application.getRealPath(reporthome);
String raq = path+File.separator+”300col1.raq”;
ReportDefine rd = (ReportDefine) ReportUtils.read(raq);
Engine engine = new Engine(rd,cxt);
IReport ir = engine.calc();
ReportUtils.exportToExcel(path+File.separator+”300col.xls”,ir,true);
%>

在釋出不分頁的報表(也就是300col.raq)的jsp中呼叫上面的jsp,將300col1.raq匯出,就可以實現匯出超過255列excel的需求了。

第二種:使用標籤實現。

在展現報表jsp的潤乾標籤中加入如下兩個內容: width=”-1″ , excelUsePaperSize=”yes”,width=”-1″的含義是頁面展現的時候紙張寬度無線大,這樣展現的時候就不分頁了,而excelUsePaperSize=”yes”的含義是匯出excel的時候按照設定的紙張大小來分頁,jsp的內容如下:

<%@ page contentType=”text/html;charset=GBK” %>
<%@ taglib uri=”/WEB-INF/runqianReport4.tld” prefix=”report” %>

<% //這裡為兩種方法準備圖片連線的公共部分
String appmap = request.getContextPath();
String printImage = “<img alt=列印 src=’” + appmap + “/images/print.gif’ border=no style=’vertical-align:middle’>”;
String wordImage = “<img alt=匯出Word src=’” + appmap + “/images/doc.gif’ border=no style=’vertical-align:middle’>”;
String excelImage = “<img alt=匯出excel src=’” + appmap + “/images/excel.gif’ border=no style=’vertical-align:middle’>”;
String pdfImage = “<img alt=匯出PDF檔案 src=’” + appmap + “/images/pdf.gif’ border=no style=’vertical-align:middle’>”;
String firstPageImage = “<img src=’” + appmap + “/images/firstpage.gif’ border=no style=’vertical-align:middle’>”;
String lastPageImage = “<img src=’” + appmap + “/images/lastpage.gif’ border=no style=’vertical-align:middle’>”;
String nextPageImage = “<img src=’” + appmap + “/images/nextpage.gif’ border=no style=’vertical-align:middle’>”;
String prevPageImage = “<img src=’” + appmap + “/images/prevpage.gif’ border=no style=’vertical-align:middle’>”;
String submitImage = “<img alt=儲存到資料庫 src=’” + appmap + “/images/savedata.gif’ border=no style=’vertical-align:middle’>”;
String importExcelImage = “<img alt=匯入Excel檔案 src=’” + appmap + “/images/importExcel.gif’ border=no style=’vertical-align:middle’>”;
%>
<form id=”form1″ action=”excel.jsp”>
<table align=center>
<tr><td>
<input type=”button” onclick=”form1.action” value=”匯出excel”>
<report:html name=”report1″ reportFileName=”300col.raq”
needSaveAsWord=”yes”
needSaveAsPdf=”yes”
needSaveAsExcel=”yes”
width=”-1″
excelUsePaperSize=”yes”
wordLabel=”<%=wordImage%>”
pdfLabel=”<%=pdfImage%>”
submit=”<%=submitImage%>”

/>
</td></tr>
</table>
</form>

使用上面的jsp釋出第一種方法制作的300col1.raq,點選匯出excel的按鈕,選擇分頁方式匯出。

點選確定,就會匯出按紙分頁的excel了,這樣在頁面上匯出超過255列不分頁報表的excel就實現了。