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

需求說明

報表展示後能夠經過工具欄中的導出按鈕將當前展示的報表導出成 excel 文件,可是在實際使用中一般會要求報表不須要展示,直接經過一些操做將報表導出成 excel 文件,而且每每會要求批量導出成 excel 文件,下面經過幾個示例介紹下報表不展示,如何批量生成 excel 文件。html

實現這種需求通常要用到 api 方式,批量生成 excel 文件,按照方式上來分大致上能夠分爲三類:java

一:單表導出單 excel 多 sheet數據庫

二:多表導出單 excel 多 sheetapi

三:多表導出多 excel 文件數組

單表多 sheettomcat

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

報表設計界面沒必要多說,按照需求設計就行,以下圖:dom

1jpg

報表中增長一個參數:area,用於接收地區參數,而後在數據集中經過這個參數過濾數據就行。jsp

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

%>

<%@ 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 文件以下:

2jpg

多表多 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

%>

<%@ 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

%>

<%@ 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包就行。

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 就行。

相關文章
相關標籤/搜索