欢迎使用普元产品知识库,本知识库包含普元应用开发平台EOSPlatform,流程平台BPS,企业服务总线ESB,微服务平台Microservice,运维管理平台Devops,数据集成平台DI

页面树结构

欢迎使用普元文档库

Skip to end of metadata
Go to start of metadata

/**
* 导出Excel文件,根据指定路径下的模板生成输出的Excel文件
*
* @param exportObjects 待导出的对象数组
* @param exportInfo 模板文件的其他附加信息(非结果集内容)
* @param templateFilename 模板文件名(不带扩展名),对应到在user-config.xml配置路径下的模板文件
* @param autoPagination 是否分页
* @param autoSheet 是否分工作表
* @return 返回生成的Excel文件下载路径
* @throws Exception
*/
private static String exportExcel(DataObject[] exportObjects,DataObject exportInfo,String templateFilename,boolean autoPagination,boolean autoSheet) throws Exception{
String filename=templateFilename;

if(filename.indexOf(".xls")==-1){
filename+=".xls";
}

//临时路径是服务器当前war下面的excel-config目录
String templateDir=ApplicationContext.getInstance().getWarRealPath()+ConfigurationUtil.getContributionConfig(UtilConfiguration.CONTRIBUTION_ABFRAME_UTILS,
UtilConfiguration.MODULE_ABFRAME,
UtilConfiguration.GROUP_EXCEL,
UtilConfiguration.EXCEL_TEMPLATE_PATH);
String excelExportMaxnum=ConfigurationUtil.getContributionConfig(UtilConfiguration.CONTRIBUTION_ABFRAME_UTILS,
UtilConfiguration.MODULE_ABFRAME,
UtilConfiguration.GROUP_EXCEL,
UtilConfiguration.EXCEL_EXPORT_MAXNUM);

if(!templateDir.endsWith("/")){
templateDir+="/";
}
String tempDir=templateDir+"temp/";
File file=new File(tempDir);
if(!file.exists()){
//创建临时目录
FileUtil.mkDir(tempDir);
//file.createNewFile();
}

String templateFile=templateDir+filename;
String outputFile=tempDir+generateOutputExcelFile(filename);

ExcelTemplate template=new ExcelTemplate(templateFile,outputFile);
template.setAutoPagination(autoPagination);
template.setAutoSheet(autoSheet);
int excelExportMaxnumInt = 0;
try{
excelExportMaxnumInt = Integer.parseInt(excelExportMaxnum);
}catch (Exception e){
e.printStackTrace();
}
template.setMaxRow(excelExportMaxnumInt);
template.generate(Arrays.asList(exportObjects),exportInfo);

return outputFile;
}

 

 

jsp上下载该Excel文件

<%@page pageEncoding="UTF-8"%>

<%@page import="javax.servlet.ServletOutputStream"%>
<%@page import="java.io.*"%>
<%@page import="com.eos.web.taglib.util.*" %><%
Object root= com.eos.web.taglib.util.XpathUtil.getDataContextRoot("request", pageContext);
String localFile=(String)XpathUtil.getObjectByXpath(root,"downloadFile");

String fileName = (String)XpathUtil.getObjectByXpath(root,"fileName");
System.out.println(localFile);
System.out.println(">>>>download file is "+ fileName);
byte[] buffer = new byte[512];
int size = 0;
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=\""+ java.net.URLEncoder.encode(fileName,"UTF-8") + "\"");
ServletOutputStream os = null;
FileInputStream in = null;
try {
os = response.getOutputStream();
File downloadFile=new File(localFile);
if(downloadFile!=null&&downloadFile.exists()){
in = new FileInputStream(new File(localFile));
while ((size = in.read(buffer)) != -1) {
os.write(buffer, 0, size);
}
out.clear();
out = pageContext.pushBody();
}else{
out.print("文件不存在!"); //"文件不存在!"
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(in!=null)in.close();
if(os!=null)os.close();
File file=new File(localFile);
if (file!=null&&file.isFile()&&file.exists()) {
file.delete();
}

} catch (IOException e) {
e.printStackTrace();
}
}
%>