@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void particularsDownloadExcel(String combination) throws BaseException {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse();
boolean flag;
try {
String[] reportGroups = combination.split(",");
String reportId = reportGroups[0];
String submitFreqID = reportGroups[2];
//产品品种
String productTypeName = reportGroups[3];
String cpCode = reportGroups[4];
String[] finPfIds = reportGroups[1].split("_");
Map workbookMap = new HashMap<>();
Map combinationMap;
ExecutorService pool = Executors.newCachedThreadPool();
long bt = System.currentTimeMillis();
map.put("submitFreqID", submitFreqID);
map.put("cpCode", cpCode);
map.put("reportId", reportId);
for (int i = 0; i < finPfIds.length; i++) {
combinationMap = new HashMap<>(16);
combinationMap.put("reportId", reportId);
combinationMap.put("FIN_PF_ID", finPfIds[i]);
combinationMap.put("submitFreqID", submitFreqID);
combinationMap.put("cpCode", cpCode);
Map tempMap = downloadExcel1(combinationMap, productTypeName);
for (String fileName : tempMap.keySet()) {
workbookMap.put(fileName, tempMap.get(fileName));
}
}
String path = PBCParticularsDownloadBiz.class.getClassLoader().getResource("").getFile() + "tempZip";
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//如果生成多个Excel压缩成一个zip文件 如果生成一个excel 直接导出
if (workbookMap.size() > 1) {
//压缩后的名字
String targetFile = "多个组合" + ".zip";
response.setContentType("application/octet-stream;charset=utf-8");
String fileName = URLEncoder.encode(targetFile, "utf-8");
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName + "; filename*=utf-8''" + fileName);
File zipFile = toZip(workbookMap, targetFile, path);
FileInputStream fis = new FileInputStream(zipFile);
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
out.write(buffer);
}
} else {
for (String targetFile : workbookMap.keySet()) {
Workbook workbook = workbookMap.get(targetFile);
// 设置response的Header
response.setContentType("application/-excel;charset=utf-8");
String fileName = URLEncoder.encode(targetFile + ".xls", "utf-8");
response.addHeader("Content-Disposition", "attachment; filename="
+ fileName + "; filename*=utf-8''" + fileName);
workbook.write(out);
}
}
out.flush();
out.close();
//删除生成后的临时zip文件
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
file.delete();
}
flag = true;
} catch (Exception ioe) {
log.error("导出失败", ioe);
flag = false;
}
}
/**
* @return void
* @throws RuntimeException 压缩失败会抛出运行时异常
* @Author
* @Description //压缩成ZIP 方法
* @Date 2019/2/16 20:18
* @Param [workbookMap, zipName]
**/
public static File toZip(Map workbookMap, String zipName, String tempFilePath) throws BaseException {
List srcFiles = new ArrayList<>();
for (String workbookName : workbookMap.keySet()) {
try {
File pathFile = new File(tempFilePath);
if (!pathFile.exists()) {
pathFile.mkdir();
}
File file = new File(tempFilePath + "/" + workbookName + ".xls");
OutputStream os = new FileOutputStream(file);
workbookMap.get(workbookName).write(os);
os.flush();
os.close();
srcFiles.add(file);
} catch (IOException e) {
e.printStackTrace();
}
}
File file = new File(tempFilePath + "/" + zipName);
OutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:" + (end - start) + " ms");
} catch (Exception e) {
throw new BaseException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
/**
* @return
* @Author zhanghaoxing
* @Description //查询字典的 填报机构名称,社会信用代码,金融机构编码
* @Date 2019/2/24 20:33
* @Param
**/
public Map selectReportForm() {
HashMap objectObjectHashMap = new HashMap<>();
List> linkedHashMaps = pBCDExcelMapper.selectReportForm();
for (int i = 0; i < linkedHashMaps.size(); i++) {
LinkedHashMap map1 = linkedHashMaps.get(i);
String keyId = null;
String keyName = null;
for (Map.Entry entry : map1.entrySet()) {
if (entry.getKey().equals("KEY_ID")) {
keyId = entry.getValue();
objectObjectHashMap.put(keyId, null);
} else if (entry.getKey().equals("KEY_NAME")) {
keyName = entry.getValue();
objectObjectHashMap.put(keyId, keyName);
}
}
}
return objectObjectHashMap;
}