EXCEL导出:是把要导出的数据填充到EXCEL模板中,生成EXCEL文件输出到前端。固首先需要配置EXCEL导出的一些配置信息。
下面是EXCEL导出配置信息说明:
EXCEL导出配置信息可在数据库中进行配置维护,也可以直接使用代码将配置信息写入,下面介绍使用数据库配置方式来进行配置。
EXCEL导出配置表:
EXPORT_CONFIG
内容如下:
EXPORT_CONFIG:
列名 | 字段说明 |
---|---|
id | 主键 |
template | EXCEL模板(二进制数据存储) |
async | 是否异步打印(0:否;1:是) |
dictionary_url | 获取数据字典controller的URL地址 |
file_name | 导出EXCEL的文件名 |
source_url | 获取打印数据controller的URL地址 |
page_size | 异步打印时,每次请求的记录条数 |
class_name | 打印数据的实体类名(全限定名) |
EXCEL导出后台提供一个访问接口(ExportController类[/export/excel]),一个处理EXCEL导出的Service服务类(ExportSupportService)
其中:ExportSupportService类已经在spring-excel-service.xml文件中定义好了,只需在ApplicationContext.xml引入就OK了)
<import resource="classpath*:spring-excel-service.xml"/>
ExportController类在spring-excel-controller.xml文件中定义好了,需在dispatcher-servlet.xml中引入
<import resource="classpath*:spring-excel-controller.xml"/>
a、各个业务系统需根据自己的需求,实现ExportService接口
以下给个示例实现:
package com.gillion.sebusiness.demo.service.exportExcel;
import com.gillion.excel.out.entity.ExportConfig;
import com.gillion.excel.out.service.ExportService;
import com.gillion.sebusiness.test.mapper.ExportInfoMapper;
import com.gillion.sebusiness.test.model.ExportInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.*;
@Service("exportService")
public class ExportServiceImpl implements ExportService{
@Autowired
private ExportInfoMapper exportInfoMapper;
/**
* 处理异步的excel结果
* @param fileName
* @param ou
*/
@Override
public void dealAsyncExport(String fileName, OutputStream ou) {
try{
String filePath = "D:\\excelexport\\"+fileName;
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
ByteArrayOutputStream out = (ByteArrayOutputStream)ou;
FileOutputStream fileout = new FileOutputStream(new File(filePath));
fileout.write(out.toByteArray());
fileout.flush();
fileout.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 得到导出EXCEL的配置信息
* @param key
* @return
*/
@Override
public ExportConfig getExportConfig(String key) {
ExportInfo exportInfo = exportInfoMapper.selectByPrimaryKey(key);
ExportConfig exportConfig = new ExportConfig();
exportConfig.setDictionaryUrl(exportInfo.getDictionaryUrl());
exportConfig.setClassName(exportInfo.getClassName());
exportConfig.setAsync(exportInfo.getAsync());
exportConfig.setFileName(exportInfo.getFileName());
exportConfig.setPageSize(exportInfo.getPageSize());
exportConfig.setPrintMode(exportInfo.getPrintMode());
exportConfig.setSourceUrl(exportInfo.getSourceUrl());
exportConfig.setTemplate(new ByteArrayInputStream(exportInfo.getTemplate()));
return exportConfig;
}
public ExportInfoMapper getExportInfoMapper() {
return exportInfoMapper;
}
public void setExportInfoMapper(ExportInfoMapper exportInfoMapper) {
this.exportInfoMapper = exportInfoMapper;
}
}
c、各个业务系统需根据自己的需求,编写打印时需要的数据字典Controller类:
以下给个示例实现:
package com.gillion.sebusiness.demo.controller.dictionary;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/dictionary")
public class DictionaryController{
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public Page returnDictionary( ) {
String result = "{\"name\":{\"小华\":\"华哥\"}}";
return result;
}
}
EXCEL导出前端为一个angularjs的服务,只需要在controller或指令中调用即可。
controller中的js示例代码
(function() {
define([
"angular",
"framework/exportexcel/GillionExportExcelModule",
'framework/datasource/DataSourceModule
], function(angular) {
var TestExportExcelModule;
TestExportExcelModule = angular.module("TestExportExcelModule",
['GillionExportExcelModule','DataSourceModule']);
TestExportExcelModule.controller("ExportExcelController",
function($scope,$exportExcelService){
$scope.export =function(){
var sourceName,key,url;
sourceName='exportExcel';
key="id1";
url="/gillion-web/export/excel";
isprintall = "false";
$exportExcelService.export(sourceName,key,isprintall,url);
}
});
});
}).call(this);
导出服务 $exportExcelService.export() 参数说明:
参数 | 说明 |
---|---|
sourceName | 数据源名称 |
key | 获取配置信息的查询条件 |
isprintall | 是否打印所有页面 |
url | 处理excel导出请求的controller地址: 后期会修改为使用前端统一配置(目前没有) |