報表如何批量導出成 excel 文件

需求說明
報表展現後可以通過工具欄中的導出按鈕將當前展現的報表導出成 excel 文件,但是在實際使用中通常會要求報表不需要展現,直接通過一些操作將報表導出成 excel 文件,並且往往會要求批量導出成 excel 文件,下面通過幾個示例介紹下報表不展現,如何批量生成 excel 文件。

實現這種需求一般要用到 api 方式,批量生成 excel 文件,按照方式上來分大體上可以分爲三類:

一:單表導出單 excel 多 sheet

二:多表導出單 excel 多 sheet

三:多表導出多 excel 文件

單表多 sheet
此種方式通常是報表格式固定,然後根據某個參數對數據過濾,導出 excel 時需要導出多個參數的數據,並且每個參數的數據放到同一個 excel 的不同 sheet 裏,比如本例中按照地區統計訂單信息,要求導出時每個地區數據導出到一個 sheet 中,地區名稱做爲 sheet 名,下面看下這種做法:

報表設計界面不必多說,按照需求設計就行,如下圖:

報表如何批量導出成 excel 文件

報表中增加一個參數:area,用於接收地區參數,然後在數據集中通過這個參數過濾數據就行。

Api 中用到 jsp 代碼如下:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.excel.ExcelReport"%>

<%

String report = request.getParameter( "report" );//獲取報表名稱

if(report==null) report="訂單.rpx";//如果url上報表名爲空,則取訂單表

String fileName=report.substring(0,report.length()-4);//讀取文件名,用於設置excel名稱

String reportName = request.getRealPath("WEB-INF\\reportFiles\\"+report);//

String exportPath=request.getRealPath("/export");//在應用根目錄下建export目錄,放置導出文件

ReportDefine rd = (ReportDefine)ReportUtils.read(reportName);//讀取報表

String areas="華北,東北,西北,華南,西南";//此例按照地區循環,實際中可以接收其他參數,也可以從數據庫中獲取數據

String[] area=areas.split(",");

ExcelReport er=new ExcelReport();

for(int i=0;i<area.length;i++){//按照地區做循環

Context cxt = new Context();

cxt.setParamValue("area",area[i]);//area是報表中定義參數,此處設置參數值

Engine engine = new Engine(rd, cxt); //構造報表引擎

IReport iReport = engine.calc(); //運算報表

er.export(area[i],iReport);//將報表結果設置到excel sheet裏

}

er.saveTo(exportPath+"/"+fileName+".xls");//生成excel

%>
這樣,就會在應用根目錄的 export 目錄下生成對應的 excel 文件,生成 excel 文件如下:

報表如何批量導出成 excel 文件

多表多 sheet
此種情況應用於導出的 excel 由多個報表組成,然後每個報表導出到 excel 中不同 sheet 中,並且有可能每個報表的參數不同(如果參數相同,那麼和示例一類似,只是按報表名稱循環就行),下面看下具體實現過程:

由於不同報表參數可能會不同,所以在 api 中解析每個報表以及對應參數難度會比較大,此例要求通過 url 訪問報表時,按照特定格式訪問,比如:

http://localhost:6868/demo/reportJsp/exportExcel1.jsp?report={無參數報表名 1}{無參數報表名 2}{報表 1( 參數 1=value1; 參數 2=value2;…)}{報表 2( 參數 1=value1; 參數 2=value2;…)},比如:http://localhost:6868/demo/reportJsp/exportExcel1.jsp?report={test1.rpx}{test2.rpx(arg1=11;arg2=22;arg3=33)}這種方式,現在在高版本的 tomcat 中,會有一些特殊符號的限定,使用時可以將 {} 轉換成對應的 urlencode 方式,{爲 %7B,}爲 %7B,如果有其他值的話,做對應轉換就行,url 設置完成後,接下來就看下如何解析這個 url,並且批量生成 excel,代碼如下:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.excel.ExcelReport"%>

<%

//此JSP參數格式爲:report={無參數報表名1}{無參數報表名2}{報表1(參數1=value1;參數2=value2;...)}{報表2(參數1=value1;參數2=value2;...)}

request.setCharacterEncoding( "UTF-8" );

String report = request.getParameter( "report" );

if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報表文件名及參數串report={無參數報表名}{報表1(參數1=value1;參數2=value2;...)}{報表2(參數1=value1;參數2=value2;...)}..." );

String exportPath=request.getRealPath("/export");//在應用根目錄下建export目錄,放置導出文件

String report1=report.replace("}","");//去掉串中的}

String report2=report1.substring(1,report1.length());//去掉串中的最左側的{

String[] a=report2.split("\{");//此時串中多個報表之間用{分隔,所以此處按照該符號split生成數組

ExcelReport er=new ExcelReport();

for(int i=0;i<a.length;i++){//按數組進行循環,也就是按報表循環

if(a[i].lastIndexOf("(")<=0)//判斷分割後的子串中是否包含(,如包含,代表有參數,不包含,則沒有參數

{

String reportPath = request.getRealPath("WEB-INF\\reportFiles\\"+a[i]);//獲取報表路徑

String sheetName=a[i].substring(0,a[i].length()-4);//獲取sheet名稱

ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報表

Context cxt = new Context();

Engine engine = new Engine(rd, cxt); //構造報表引擎

IReport iReport = engine.calc(); //計算報表

er.export(sheetName,iReport);//將報表結果放入sheet

}

else{

System.out.println("報表有參數,報表名爲="+a[i].split("\(")[0]);//如果有參數,則按(字符split,左側爲報表名

String reportPath = request.getRealPath("WEB-INF\\reportFiles\\"+a[i].split("\(")[0]);

String sheetName=a[i].split("\(")[0].substring(0,a[i].split("\(")[0].length()-4);

ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);

Context cxt = new Context();

String[] cs=a[i].split("\(")[1].replace(")","").split(";");//右側爲參數串,並且去掉參數串的),多個參數用;隔開,所以此處按照;split

for(int j=0;j<cs.length;j++){//按參數循環

cxt.setParamValue(cs[j].split("=")[0],cs[j].split("=")[1]);//設置參數

}

Engine engine = new Engine(rd, cxt); //構造報表引擎

IReport iReport = engine.calc();

er.export(sheetName,iReport);

}

}

er.saveTo(exportPath+"/test.xls");//生成excel

%>
多表多 excel
此種方式和示例二類似,在實際使用中導出 excel 時要求每個報表導出成不同的 excel 文件,但是後續可能會涉及到下載問題,所以此種方式一般是要建立個臨時目錄,然後將多個 excel 放到臨時目錄內,可以進行打包下載。具體代碼如下:

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="com.raqsoft.report.model.*"%>

<%@ page import="com.raqsoft.report.usermodel.*"%>

<%@ page import="com.raqsoft.report.view.*"%>

<%@ page import="com.raqsoft.report.util.*"%>

<%@ page import="com.raqsoft.report.view.excel.ExcelReport"%>

<%

//此JSP參數格式爲:report={無參數報表名1}{無參數報表名2}{報表1(參數1=value1;參數2=value2;...)}{報表2(參數1=value1;參數2=value2;...)}

request.setCharacterEncoding( "UTF-8" );

String report = request.getParameter( "report" );

if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報表文件名及參數串report={無參數報表名}{報表1(參數1=value1;參數2=value2;...)}{報表2(參數1=value1;參數2=value2;...)}..." );

String exportPath=request.getRealPath("/export");//在應用根目錄下建export目錄,放置導出文件

String fileName=Double.toString(Math.random()*100000000).toString().substring(0,6);

String excelPath=exportPath+"\\"+fileName;

java.io.File file = new java.io.File(excelPath);

if(!file.exists()) {

file.mkdirs();

} else {

}

String report1=report.replace("}","");//去掉串中的}

String report2=report1.substring(1,report1.length());//去掉串中的最左側的{

String[] a=report2.split("\{");//此時串中多個報表之間用{分隔,所以此處按照該符號split生成數組

ExcelReport er=new ExcelReport();

for(int i=0;i<a.length;i++){//按數組進行循環,也就是按報表循環

if(a[i].lastIndexOf("(")<=0)//判斷分割後的子串中是否包含(,如包含,代表有參數,不包含,則沒有參數

{

String reportPath = request.getRealPath("WEB-INF\\reportFiles\\"+a[i]);//獲取報表路徑

String sheetName=a[i].substring(0,a[i].length()-4);//獲取sheet名稱

ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報表

Context cxt = new Context();

Engine engine = new Engine(rd, cxt); //構造報表引擎

IReport iReport = engine.calc(); //計算報表

ReportUtils.exportToExcel2007(excelPath+"/"+sheetName+".xlsx",iReport,false);

er.export(sheetName,iReport);//將報表結果放入sheet

}

else{

System.out.println("報表有參數,報表名爲="+a[i].split("\(")[0]);//如果有參數,則按(字符split,左側爲報表名

String reportPath = request.getRealPath("WEB-INF\\reportFiles\\"+a[i].split("\(")[0]);

String sheetName=a[i].split("\(")[0].substring(0,a[i].split("\(")[0].length()-4);

ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);

Context cxt = new Context();

String[] cs=a[i].split("\(")[1].replace(")","").split(";");//右側爲參數串,並且去掉參數串的),多個參數用;隔開,所以此處按照;split

for(int j=0;j<cs.length;j++){//按參數循環

cxt.setParamValue(cs[j].split("=")[0],cs[j].split("=")[1]);//設置參數

}

Engine engine = new Engine(rd, cxt); //構造報表引擎

IReport iReport = engine.calc();

ReportUtils.exportToExcel2007(excelPath+"/"+sheetName+".xlsx",iReport,false);

}

}

er.saveTo(exportPath+"/test.xls");//生成excel

%>
打包下載
文件生成到對應目錄下後,可以自己單獨做個鏈接指向這個 excel 文件下載,也可以直接生成 excel 文件後直接進行下載,在對應 jsp 文件中增加如下代碼:

response.setContentType("application/msword");

response.setHeader("Content-disposition","attachment; filename="+java.net.URLEncoder.encode(fileName+".xls", "UTF-8"));

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

bis = new BufferedInputStream(new FileInputStream( exportPath+"/"+fileName+".xls"));

bos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff,0,bytesRead);

}

} catch(final IOException e) {

System.out.println ( "出現IOException." + e );

} finally {

if (bis != null)

bis.close();

if (bos != null)

bos.close();

}

System.out.println ( "下載完成----------------" );

File file = new File(exportPath+"/"+fileName+".xls");

if (file.exists()) file.delete();//刪除文件

實際應用中可能會需要將文件大成zip包方式,比如示例三,這個直接百度下java程序打zip包,然後下載zip包就行。
總結
本文中介紹瞭如何通過 api 將報表批量導出成 excel 的方法,實際中也有可能生成 pdf 或者 txt 等,思路是一樣到,到時候換用不同的 api 就行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章