溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

報(bào)表如何批量導(dǎo)出成 excel 文件

發(fā)布時(shí)間:2020-08-10 02:21:37 來源:ITPUB博客 閱讀:152 作者:rqgxy 欄目:編程語言

需求說明

報(bào)表展現(xiàn)后可以通過工具欄中的導(dǎo)出按鈕將當(dāng)前展現(xiàn)的報(bào)表導(dǎo)出成 excel 文件,但是在實(shí)際使用中通常會(huì)要求報(bào)表不需要展現(xiàn),直接通過一些操作將報(bào)表導(dǎo)出成 excel 文件,并且往往會(huì)要求批量導(dǎo)出成 excel 文件,下面通過幾個(gè)示例介紹下報(bào)表不展現(xiàn),如何批量生成 excel 文件。

實(shí)現(xiàn)這種需求一般要用到 api 方式,批量生成 excel 文件,按照方式上來分大體上可以分為三類:

一:單表導(dǎo)出單 excel 多 sheet

二:多表導(dǎo)出單 excel 多 sheet

三:多表導(dǎo)出多 excel 文件

單表多 sheet

此種方式通常是報(bào)表格式固定,然后根據(jù)某個(gè)參數(shù)對數(shù)據(jù)過濾,導(dǎo)出 excel 時(shí)需要導(dǎo)出多個(gè)參數(shù)的數(shù)據(jù),并且每個(gè)參數(shù)的數(shù)據(jù)放到同一個(gè) excel 的不同 sheet 里,比如本例中按照地區(qū)統(tǒng)計(jì)訂單信息,要求導(dǎo)出時(shí)每個(gè)地區(qū)數(shù)據(jù)導(dǎo)出到一個(gè) sheet 中,地區(qū)名稱做為 sheet 名,下面看下這種做法:

報(bào)表設(shè)計(jì)界面不必多說,按照需求設(shè)計(jì)就行,如下圖:

報(bào)表如何批量導(dǎo)出成 excel 文件

報(bào)表中增加一個(gè)參數(shù):area,用于接收地區(qū)參數(shù),然后在數(shù)據(jù)集中通過這個(gè)參數(shù)過濾數(shù)據(jù)就行。

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" );//獲取報(bào)表名稱
if(report==null) report="訂單.rpx";//如果url上報(bào)表名為空,則取訂單表
String fileName=report.substring(0,report.length()-4);//讀取文件名,用于設(shè)置excel名稱
String reportName = request.getRealPath("WEB-INF\\\reportFiles\\\"+report);//
String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件
ReportDefine rd = (ReportDefine)ReportUtils.read(reportName);//讀取報(bào)表
String areas="華北,東北,西北,華南,西南";//此例按照地區(qū)循環(huán),實(shí)際中可以接收其他參數(shù),也可以從數(shù)據(jù)庫中獲取數(shù)據(jù)
String\[\] area=areas.split(",");
ExcelReport er=new ExcelReport();
for(int i=0;i<area.length;i++){//按照地區(qū)做循環(huán)
Context cxt = new Context();
cxt.setParamValue("area",area\[i\]);//area是報(bào)表中定義參數(shù),此處設(shè)置參數(shù)值
  Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎
  IReport iReport = engine.calc(); //運(yùn)算報(bào)表
 er.export(area\[i\],iReport);//將報(bào)表結(jié)果設(shè)置到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" );//獲取報(bào)表名稱 if(report==null) report="訂單.rpx";//如果url上報(bào)表名為空,則取訂單表 String fileName=report.substring(0,report.length()-4);//讀取文件名,用于設(shè)置excel名稱 String reportName = request.getRealPath("WEB-INF\\\reportFiles\\\"+report);// String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件 ReportDefine rd = (ReportDefine)ReportUtils.read(reportName);//讀取報(bào)表 String areas="華北,東北,西北,華南,西南";//此例按照地區(qū)循環(huán),實(shí)際中可以接收其他參數(shù),也可以從數(shù)據(jù)庫中獲取數(shù)據(jù) String\[\] area=areas.split(","); ExcelReport er=new ExcelReport(); for(int i=0;i<area.length;i++){//按照地區(qū)做循環(huán) Context cxt = new Context(); cxt.setParamValue("area",area\[i\]);//area是報(bào)表中定義參數(shù),此處設(shè)置參數(shù)值 Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎 IReport iReport = engine.calc(); //運(yùn)算報(bào)表 er.export(area\[i\],iReport);//將報(bào)表結(jié)果設(shè)置到excel sheet里 } er.saveTo(exportPath+"/"+fileName+".xls");//生成excel %>

這樣,就會(huì)在應(yīng)用根目錄的 export 目錄下生成對應(yīng)的 excel 文件,生成 excel 文件如下:

報(bào)表如何批量導(dǎo)出成 excel 文件

多表多 sheet

此種情況應(yīng)用于導(dǎo)出的 excel 由多個(gè)報(bào)表組成,然后每個(gè)報(bào)表導(dǎo)出到 excel 中不同 sheet 中,并且有可能每個(gè)報(bào)表的參數(shù)不同(如果參數(shù)相同,那么和示例一類似,只是按報(bào)表名稱循環(huán)就行),下面看下具體實(shí)現(xiàn)過程:

由于不同報(bào)表參數(shù)可能會(huì)不同,所以在 api 中解析每個(gè)報(bào)表以及對應(yīng)參數(shù)難度會(huì)比較大,此例要求通過 url 訪問報(bào)表時(shí),按照特定格式訪問,比如:

http://localhost:6868/demo/reportJsp/exportExcel1.jsp?report={無參數(shù)報(bào)表名 1}{無參數(shù)報(bào)表名 2}{報(bào)表 1( 參數(shù) 1=value1; 參數(shù) 2=value2;…)}{報(bào)表 2( 參數(shù) 1=value1; 參數(shù) 2=value2;…)},比如: http://localhost:6868/demo/reportJsp/exportExcel1.jsp?report={test1.rpx}{test2.rpx(arg1=11;arg2=22;arg3=33)}這種方式,現(xiàn)在在高版本的 tomcat 中,會(huì)有一些特殊符號(hào)的限定,使用時(shí)可以將 {} 轉(zhuǎn)換成對應(yīng)的 urlencode 方式,{為 %7B,}為 %7B,如果有其他值的話,做對應(yīng)轉(zhuǎn)換就行,url 設(shè)置完成后,接下來就看下如何解析這個(gè) 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參數(shù)格式為:report={無參數(shù)報(bào)表名1}{無參數(shù)報(bào)表名2}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}
request.setCharacterEncoding( "UTF-8" );
 String report = request.getParameter( "report" );
 if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報(bào)表文件名及參數(shù)串report={無參數(shù)報(bào)表名}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}..." );
String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件
String report1=report.replace("}","");//去掉串中的}
String report2=report1.substring(1,report1.length());//去掉串中的最左側(cè)的{
String\[\] a=report2.split("\\\{");//此時(shí)串中多個(gè)報(bào)表之間用{分隔,所以此處按照該符號(hào)split生成數(shù)組
ExcelReport er=new ExcelReport();
for(int i=0;i<a.length;i++){//按數(shù)組進(jìn)行循環(huán),也就是按報(bào)表循環(huán)
if(a\[i\].lastIndexOf("(")<=0)//判斷分割后的子串中是否包含(,如包含,代表有參數(shù),不包含,則沒有參數(shù)
{
String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\]);//獲取報(bào)表路徑
 String sheetName=a\[i\].substring(0,a\[i\].length()-4);//獲取sheet名稱
 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報(bào)表
 Context cxt = new Context();
 Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎
IReport iReport = engine.calc(); //計(jì)算報(bào)表
er.export(sheetName,iReport);//將報(bào)表結(jié)果放入sheet
}
else{
 System.out.println("報(bào)表有參數(shù),報(bào)表名為="+a\[i\].split("\\\(")\[0\]);//如果有參數(shù),則按(字符split,左側(cè)為報(bào)表名
 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(";");//右側(cè)為參數(shù)串,并且去掉參數(shù)串的),多個(gè)參數(shù)用;隔開,所以此處按照;split
 for(int j=0;j<cs.length;j++){//按參數(shù)循環(huán)
 cxt.setParamValue(cs\[j\].split("=")\[0\],cs\[j\].split("=")\[1\]);//設(shè)置參數(shù)
 }
 Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎
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參數(shù)格式為:report={無參數(shù)報(bào)表名1}{無參數(shù)報(bào)表名2}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)} request.setCharacterEncoding( "UTF-8" ); String report = request.getParameter( "report" ); if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報(bào)表文件名及參數(shù)串report={無參數(shù)報(bào)表名}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}..." ); String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件 String report1=report.replace("}","");//去掉串中的} String report2=report1.substring(1,report1.length());//去掉串中的最左側(cè)的{ String\[\] a=report2.split("\\\{");//此時(shí)串中多個(gè)報(bào)表之間用{分隔,所以此處按照該符號(hào)split生成數(shù)組 ExcelReport er=new ExcelReport(); for(int i=0;i<a.length;i++){//按數(shù)組進(jìn)行循環(huán),也就是按報(bào)表循環(huán) if(a\[i\].lastIndexOf("(")<=0)//判斷分割后的子串中是否包含(,如包含,代表有參數(shù),不包含,則沒有參數(shù) { String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\]);//獲取報(bào)表路徑 String sheetName=a\[i\].substring(0,a\[i\].length()-4);//獲取sheet名稱 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報(bào)表 Context cxt = new Context(); Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎 IReport iReport = engine.calc(); //計(jì)算報(bào)表 er.export(sheetName,iReport);//將報(bào)表結(jié)果放入sheet } else{ System.out.println("報(bào)表有參數(shù),報(bào)表名為="+a\[i\].split("\\\(")\[0\]);//如果有參數(shù),則按(字符split,左側(cè)為報(bào)表名 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(";");//右側(cè)為參數(shù)串,并且去掉參數(shù)串的),多個(gè)參數(shù)用;隔開,所以此處按照;split for(int j=0;j<cs.length;j++){//按參數(shù)循環(huán) cxt.setParamValue(cs\[j\].split("=")\[0\],cs\[j\].split("=")\[1\]);//設(shè)置參數(shù) } Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎 IReport iReport = engine.calc(); er.export(sheetName,iReport); } } er.saveTo(exportPath+"/test.xls");//生成excel %>

多表多 excel

此種方式和示例二類似,在實(shí)際使用中導(dǎo)出 excel 時(shí)要求每個(gè)報(bào)表導(dǎo)出成不同的 excel 文件,但是后續(xù)可能會(huì)涉及到下載問題,所以此種方式一般是要建立個(gè)臨時(shí)目錄,然后將多個(gè) excel 放到臨時(shí)目錄內(nèi),可以進(jìn)行打包下載。具體代碼如下:

<%@ 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參數(shù)格式為:report={無參數(shù)報(bào)表名1}{無參數(shù)報(bào)表名2}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}
request.setCharacterEncoding( "UTF-8" );
 String report = request.getParameter( "report" );
 if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報(bào)表文件名及參數(shù)串report={無參數(shù)報(bào)表名}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}..." );
String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件
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());//去掉串中的最左側(cè)的{
String\[\] a=report2.split("\\\{");//此時(shí)串中多個(gè)報(bào)表之間用{分隔,所以此處按照該符號(hào)split生成數(shù)組
ExcelReport er=new ExcelReport();
for(int i=0;i<a.length;i++){//按數(shù)組進(jìn)行循環(huán),也就是按報(bào)表循環(huán)
if(a\[i\].lastIndexOf("(")<=0)//判斷分割后的子串中是否包含(,如包含,代表有參數(shù),不包含,則沒有參數(shù)
{
String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\]);//獲取報(bào)表路徑
 String sheetName=a\[i\].substring(0,a\[i\].length()-4);//獲取sheet名稱
 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報(bào)表
 Context cxt = new Context();
 Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎
IReport iReport = engine.calc(); //計(jì)算報(bào)表
ReportUtils.exportToExcel2007(excelPath+"/"+sheetName+".xlsx",iReport,false);
er.export(sheetName,iReport);//將報(bào)表結(jié)果放入sheet
}
else{
 System.out.println("報(bào)表有參數(shù),報(bào)表名為="+a\[i\].split("\\\(")\[0\]);//如果有參數(shù),則按(字符split,左側(cè)為報(bào)表名
 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(";");//右側(cè)為參數(shù)串,并且去掉參數(shù)串的),多個(gè)參數(shù)用;隔開,所以此處按照;split
 for(int j=0;j<cs.length;j++){//按參數(shù)循環(huán)
 cxt.setParamValue(cs\[j\].split("=")\[0\],cs\[j\].split("=")\[1\]);//設(shè)置參數(shù)
 }
 Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎
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參數(shù)格式為:report={無參數(shù)報(bào)表名1}{無參數(shù)報(bào)表名2}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)} request.setCharacterEncoding( "UTF-8" ); String report = request.getParameter( "report" ); if( report == null || report.trim().length() == 0 ) throw new Exception( "請輸入報(bào)表文件名及參數(shù)串report={無參數(shù)報(bào)表名}{報(bào)表1(參數(shù)1=value1;參數(shù)2=value2;...)}{報(bào)表2(參數(shù)1=value1;參數(shù)2=value2;...)}..." ); String exportPath=request.getRealPath("/export");//在應(yīng)用根目錄下建export目錄,放置導(dǎo)出文件 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());//去掉串中的最左側(cè)的{ String\[\] a=report2.split("\\\{");//此時(shí)串中多個(gè)報(bào)表之間用{分隔,所以此處按照該符號(hào)split生成數(shù)組 ExcelReport er=new ExcelReport(); for(int i=0;i<a.length;i++){//按數(shù)組進(jìn)行循環(huán),也就是按報(bào)表循環(huán) if(a\[i\].lastIndexOf("(")<=0)//判斷分割后的子串中是否包含(,如包含,代表有參數(shù),不包含,則沒有參數(shù) { String reportPath = request.getRealPath("WEB-INF\\\reportFiles\\\"+a\[i\]);//獲取報(bào)表路徑 String sheetName=a\[i\].substring(0,a\[i\].length()-4);//獲取sheet名稱 ReportDefine rd = (ReportDefine)ReportUtils.read(reportPath);//讀取報(bào)表 Context cxt = new Context(); Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎 IReport iReport = engine.calc(); //計(jì)算報(bào)表 ReportUtils.exportToExcel2007(excelPath+"/"+sheetName+".xlsx",iReport,false); er.export(sheetName,iReport);//將報(bào)表結(jié)果放入sheet } else{ System.out.println("報(bào)表有參數(shù),報(bào)表名為="+a\[i\].split("\\\(")\[0\]);//如果有參數(shù),則按(字符split,左側(cè)為報(bào)表名 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(";");//右側(cè)為參數(shù)串,并且去掉參數(shù)串的),多個(gè)參數(shù)用;隔開,所以此處按照;split for(int j=0;j<cs.length;j++){//按參數(shù)循環(huán) cxt.setParamValue(cs\[j\].split("=")\[0\],cs\[j\].split("=")\[1\]);//設(shè)置參數(shù) } Engine engine = new Engine(rd, cxt); //構(gòu)造報(bào)表引擎 IReport iReport = engine.calc(); ReportUtils.exportToExcel2007(excelPath+"/"+sheetName+".xlsx",iReport,false); } } er.saveTo(exportPath+"/test.xls");//生成excel %>

打包下載

文件生成到對應(yīng)目錄下后,可以自己單獨(dú)做個(gè)鏈接指向這個(gè) excel 文件下載,也可以直接生成 excel 文件后直接進(jìn)行下載,在對應(yīng) 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 ( "出現(xiàn)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();//刪除文件
實(shí)際應(yīng)用中可能會(huì)需要將文件大成zip包方式,比如示例三,這個(gè)直接百度下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 ( "出現(xiàn)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();//刪除文件 實(shí)際應(yīng)用中可能會(huì)需要將文件大成zip包方式,比如示例三,這個(gè)直接百度下java程序打zip包,然后下載zip包就行。

總結(jié)

本文中介紹了如何通過 api 將報(bào)表批量導(dǎo)出成 excel 的方法,實(shí)際中也有可能生成 pdf 或者 txt 等,思路是一樣到,到時(shí)候換用不同的 api 就行。

詳情鏈接: http://c.raqsoft.com.cn/article/1571624596905?r=gxy

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI